Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPEG decoding inconsistent with other (non-Rust libraries) #2256

Closed
hanslovsky opened this issue Jun 6, 2024 · 1 comment
Closed

JPEG decoding inconsistent with other (non-Rust libraries) #2256

hanslovsky opened this issue Jun 6, 2024 · 1 comment

Comments

@hanslovsky
Copy link

I am exploring Rust for image processing and am working on a small toy project. As a sanity check, I compare results with Python, which I am more familiar with. I noticed that decoding a JPEG with the image crate yields slightly different results than using Python's imageio. It looks like those are generally off by one. If this is to be expected with the JPEG algorithm (numerical instability, rounding), please disregard and close.

Expected

This is what the pixel values are in Python with imageio:

In [78]: import imageio as iio

In [79]: iio.v3.imread('tests-tiny.jpg').reshape(-1, 3)
Out[79]: 
array([[152, 146, 114],
       [149, 143, 111],
       [161, 155, 123],
       [159, 153, 121]], dtype=uint8)

Actual behaviour

This is what I get in Rust, iterating over and printing all pixels:

Rgba([151, 147, 113, 255]) 
Rgba([148, 144, 110, 255]) 
Rgba([160, 156, 122, 255]) 
Rgba([158, 154, 120, 255])

Reproduction steps

Example image

tests-tiny.jpg

Rust code

use image::GenericImageView;
use image::io::Reader;

fn main() {
    let reader = match Reader::open("tests-tiny.jpg") {
        Ok(r) => r,
        Err(error) => panic!("Unable to open image: {:?}", error)
    };
    let image = match reader.decode() {
        Ok(i) => i,
        Err(error) => panic!("unable to decode image: {:?}", error)
    };
    
    for y in 0 .. image.height() {
        for x in 0 .. image.width() {
            println!("{:?} ", image.get_pixel(x, y));
        }
    }
}

Python Code

In [78]: import imageio as iio

In [79]: iio.v3.imread('tests-tiny.jpg').reshape(-1, 3)
Out[79]: 
array([[152, 146, 114],
       [149, 143, 111],
       [161, 155, 123],
       [159, 153, 121]], dtype=uint8)
@etemesi254
Copy link

hi, author of jpeg decoder, yes this is to be expected. Jpeg is lossy and decoders are free to do what they want to a certain degreee so as long as thr deviation isn't big, this is normal

@fintelia fintelia closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants