Search code examples
pythonpython-3.xjpegbinary-datacompression

JPEG decompression in python3


I am currently creating a TIFF file reader. For that, I need to implement JPEG decompression, so I can display the JPEG-Compressed image. I have been looking for libraries for python3 to do this, but I couldn't seem to find anything. This is my code:

zipped_image_data = read(image_tags[279][0]) #compressed image data
comp = image_tags[259][0] #compression type
width = image_tags[256][0] #image width
height = image_tags[257][0] #image height
channels = len(image_tags[258]) #amount of color channels

image_data = bytes()
if comp == 1:
    image_data = zipped_image_data
if comp == 8:
    image_data = zlib.decompress(zipped_image_data)
if comp == 6:
    print("jpeg decompress")

There are lots of ways to decompress a JPEG image file, but I need some way to decompress only the data as bytes.

Any help is appreciated!


Solution

  • I managed to solve this issue by using the io module, which lets me access the bytes as if they were a file. Then I can decode this "file" and get the uncompressed data:

    like = io.BytesIO(zipped_image_data)
    image_data = imageio.imread(like)