Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "autopy"
version = "4.0.1"
edition = "2021"
edition = "2024"
authors = ["Michael Sanders <[email protected]>"]

[lib]
Expand All @@ -16,7 +16,7 @@ version = "0.4.0"
version = "0.23.5"

[dependencies.image]
version = "0.22.4"
version = "0.25.6"

[dependencies.either]
version = "1.15.0"
Expand Down
60 changes: 27 additions & 33 deletions src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use autopilot::geometry::{Point, Rect, Size};
use image::Pixel;
use image::{ImageOutputFormat, ImageResult, Rgba};
use image::{ImageFormat, ImageResult, Rgba};
use crate::internal::{rgb_to_hex, hex_to_rgb, FromImageError};
use pyo3::basic::CompareOp;
use pyo3::prelude::*;
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Bitmap {
return Err(PyBufferError::new_err("Object is not writable"));
}

let bytes = &self.bitmap.image.raw_pixels();
let bytes = &self.bitmap.image.as_bytes();

unsafe {
(*view).buf = bytes.as_ptr() as *mut libc::c_void;
Expand Down Expand Up @@ -103,20 +103,12 @@ impl Bitmap {
.or(Path::new(path).extension().and_then(|x| x.to_str()))
.unwrap_or("");
let fmt = image_output_format_from_extension(format);
match fmt {
AutoPyImageFormat::Unsupported => Err(pyo3::exceptions::PyValueError::new_err(format!(
"Unknown format {}",
format
))),
_ => {
let ref mut buffer = File::create(path)?;
self.bitmap
.image
.write_to(buffer, fmt)
.map_err(FromImageError::from)?;
Ok(())
}
}
let ref mut buffer = File::create(path)?;
self.bitmap
.image
.write_to(buffer, ImageFormat::try_from(fmt)?)
.map_err(FromImageError::from)?;
Ok(())
}

/// Copies image to pasteboard. Currently only supported on macOS.
Expand Down Expand Up @@ -169,8 +161,8 @@ impl Bitmap {
)))
} else {
let rgb = self.bitmap.get_pixel(point);
let (r, g, b, _) = rgb.channels4();
Ok(rgb_to_hex(r, g, b))
let channels = rgb.channels();
Ok(rgb_to_hex(channels[0], channels[1], channels[2]))
}
}

Expand Down Expand Up @@ -418,29 +410,31 @@ enum AutoPyImageFormat {
Unsupported,
}

impl From<AutoPyImageFormat> for ImageOutputFormat {
fn from(format: AutoPyImageFormat) -> ImageOutputFormat {
use image::ImageOutputFormat::*;
impl TryFrom<AutoPyImageFormat> for ImageFormat {
type Error = pyo3::PyErr;

fn try_from(format: AutoPyImageFormat) -> Result<ImageFormat, Self::Error> {
use image::ImageFormat::*;
match format {
AutoPyImageFormat::BMP => BMP,
AutoPyImageFormat::GIF => GIF,
AutoPyImageFormat::JPEG => JPEG(100),
AutoPyImageFormat::PNG => PNG,
AutoPyImageFormat::BMP => Ok(Bmp),
AutoPyImageFormat::GIF => Ok(Gif),
AutoPyImageFormat::JPEG => Ok(Jpeg),
AutoPyImageFormat::PNG => Ok(Png),
AutoPyImageFormat::Unsupported => {
Unsupported("This image format is unsupported by AutoPy".to_string())
Err(pyo3::exceptions::PyValueError::new_err("This image format is unsupported by AutoPy"))
}
}
}
}

impl From<ImageOutputFormat> for AutoPyImageFormat {
fn from(format: ImageOutputFormat) -> AutoPyImageFormat {
use image::ImageOutputFormat::*;
impl From<ImageFormat> for AutoPyImageFormat {
fn from(format: ImageFormat) -> AutoPyImageFormat {
use image::ImageFormat::*;
match format {
BMP => AutoPyImageFormat::BMP,
GIF => AutoPyImageFormat::GIF,
JPEG(_) => AutoPyImageFormat::JPEG,
PNG => AutoPyImageFormat::PNG,
Bmp => AutoPyImageFormat::BMP,
Gif => AutoPyImageFormat::GIF,
Jpeg => AutoPyImageFormat::JPEG,
Png => AutoPyImageFormat::PNG,
_ => AutoPyImageFormat::Unsupported,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// https://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use image::ImageError;
use image::error::{ImageError, LimitErrorKind};
use pyo3::prelude::*;

pub struct FromImageError(ImageError);
Expand All @@ -30,8 +30,8 @@ impl From<ImageError> for FromImageError {
impl From<FromImageError> for PyErr {
fn from(err: FromImageError) -> PyErr {
match err.0 {
ImageError::DimensionError => {
pyo3::exceptions::PyValueError::new_err(format!("{}", err.0))
ImageError::Limits(limit_err) if matches!(limit_err.kind(), LimitErrorKind::DimensionError) => {
pyo3::exceptions::PyValueError::new_err(format!("{}", limit_err))
}
_ => pyo3::exceptions::PyIOError::new_err(format!("{}", err.0)),
}
Expand Down
4 changes: 2 additions & 2 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn is_point_visible(x: f64, y: f64) -> PyResult<bool> {
fn get_color(x: f64, y: f64) -> PyResult<u32> {
let point = Point::new(x, y);
let rgb = autopilot::screen::get_color(point).map_err(FromImageError::from)?;
let (r, g, b, _) = rgb.channels4();
Ok(rgb_to_hex(r, g, b))
let channels = rgb.channels();
Ok(rgb_to_hex(channels[0], channels[1], channels[2]))
}

/// This module contains functions for working with the screen.
Expand Down
Loading