Sagar Rathod
Sagar Rathod

Reputation: 552

How to decode this binary image file if none of image processing python libraries (Pillow and cv2) are unable to read?

I am trying to read this file using Pillow and cv2 in python. However, both libraries are failing to do so.

File is here on google drive.

Pillow raises an "OSError: cannot identify image file 0.jpg" cv2 returns None

This binary file has the extension ".jpg" and can be easily viewed using Window`s Photos viewer.

enter image description here

However, I want to read and decode this binary file as an image in python.

Any idea?

Upvotes: 2

Views: 853

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

Your file is actually a JXR file, you can see the first few bytes are:

49 49 BC

if you load it into a hex editor like http://hexed.it

The signature is IANA registered, see here.


You can read it with imagecodecs like this:

import imagecodecs
from pathlib import Path

# Slurp entire image data as binary
jxr = Path('0.jpg').read_bytes()

# Make into Numpy array
na = imagecodecs.jpegxr_decode(jxr)

print(na.shape)        # prints (256, 512, 3)

Alternatively, you can convert JXR to something less Microsofty with ImageMagick in your Terminal with:

magick input.jxr output.jpg

Or, if you have old v6 ImageMagick, use:

convert input.jxr output.jpg

Upvotes: 4

Related Questions