uclouvain / openjpeg

Official repository of the OpenJPEG project
Other
970 stars 455 forks source link

Color chanel swapping for some JPEG2000 pictures #1382

Closed nicoinn closed 2 years ago

nicoinn commented 2 years ago

I believe there is a bug in the colour channel interpretation for the photo attached.

dg2img-3f096fe9d00296bc3734ac4f01b5bf60.j2k.zip

I'm trying to open it from Python and it ends up with swapped color channels --- Pillow or OpenCV will give the same result, both use OpenJPEG under the hood.

Screenshot 2021-10-20 at 10 48 41

To be noted that other JPEG2000 photos open correctly, and that this photo open correctly in many other applications (Preview, Safari).

This photo comes from a Belgian passport (stored in the biometric chip), and all Belgian passports I could get access to have the same issue. In case anyone worries about personal information sharing, this photo is from my passport --- and I hereby grant right to anyone to play with it for the purpose of debugging this issue and nothing else ;)

szukw000 commented 2 years ago

@nicoinn ,

1. Which openjpeg library do you use?

2. Which program viewer did you use?

3. My viewer shows a correct image with version openjpeg2-2021-09-15 .

4. opj_decompress shows correct images (PNG,BMP,TGA).

5. I tried ValZapod proposals: same result (ffmpeg-4.4).

winfried

nicoinn commented 2 years ago
  1. Which openjpeg library do you use?

=> Not 100% sure. I used Pillow==8.3.2 and opencv-python==4.5.3.56, both fail. OpenCV fails since long ago, but some older versions of Pillow were working fine. Recent ones behave exactly as OpenCV, thus I believe both rely on the same OpenJPEG lib by now.

  1. Which program viewer did you use? The viewers that work: Apple Preview, Safari,... many others. The blue photo you are looking at was displayed in a Jupyter notebook, but opening with PIL/OpenCV and saving in JPEG/PNG/BMP results in a blue photo. i.e. the blue is not a display bug.

3-4-5: well, can't say much, I haven't tried that. I'm mainly interested in opening that in Python. If you think the bug is in PIL and OpenCV, I can move there. I just thought that two completely independent implementation having the same issue may be a sign of a problem in the underlying library (?)

@ValZapod I can confirm that photos from Russian passports open just fine, with correct colors.

The image data is raw. We read the chip with our app (svipe.com), locate the photo datastream as per the ICAO standard specs and the dg2img is a direct extraction from the passport bytes for bytes. No conversion involved.

I have a few other Belgian passport photos I could share for debugging (my kids), but I'd prefer to not post these publicly here.

I can also share the entire datagroup 2 of my passport if you think some of the metadata could help :)

nicoinn commented 2 years ago

And in case anyone with the same problem googled his or her way up to here, a workaround this issue is to use wand.image . Open, convert to bitmap, send blob to PIL, done... Just very inelegant.

nicoinn commented 2 years ago

I found that both openjpeg 2.1 and 2.4 get installed as dependencies. I suppose each of PIL/OpenCV wants its how version...