0

I have an image (jpeg). I get the bytes from it simply using open('img.jpg', 'rb'). And for instance I send that bytes to my friend. So which way using Python can he get conversely operation - from bytes the image? How to decode it?

  1. the way if he knows the format - JPEG for example.
  2. the way if he doesn't know the format. Is there any ways?
  • Are you asking how to save the bytes to a file? Or how to display them in image form? Or something else? – Aran-Fey May 14 '22 at 13:20
  • I mean how to save it to file with appropriate format so friend can open it and see the image. – Ruslan Mansurov May 14 '22 at 20:50
  • You mean like `open('img.jpg', 'wb').write(data)`? – Aran-Fey May 14 '22 at 21:15
  • oh, yes! it is what I mean. but it works only if I know format of data (jpg). And what is I don't know one? Only know that it is an image data. – Ruslan Mansurov May 14 '22 at 21:28
  • To be clear, the file extension isn't necessary for the data to be a valid image. Even if you save the image with a wrong extension, like `img.bmp`, you'll still be able to open the image without any problems. There are libraries that take binary data as input and detect the file type, like the [`imghdr`](https://docs.python.org/3.8/library/imghdr.html) module. Or you can open the image with PIL as shown [here](https://stackoverflow.com/questions/32679589/how-to-get-the-format-of-image-with-pil). Or you can just send the file extension to your friend along with the image data. – Aran-Fey May 14 '22 at 21:48

3 Answers3

7

Use the PIL module. More information in answers here: Decode image bytes data stream to JPEG

from PIL import Image
from io import BytesIO


with open('img.jpg', 'rb') as f:
    data = f.read()

    # Load image from BytesIO
    im = Image.open(BytesIO(data))

    # Display image
    im.show()

    # Save the image to 'result.FORMAT', using the image format
    im.save('result.{im_format}'.format(im_format=im.format))
PythonMCSJ
  • 133
  • 5
3

If you don't want to use an external library, you can use the byte signature - which is the first few bytes of the file - to determine the image compression type.

Here are some common image formats.

img_types = {
    b'\xFF\xD8\xFF\xDB': 'jpg',
    b'\xFF\xD8\xFF\xE0': 'jpg',
    b'\xFF\xD8\xFF\xEE': 'jpg',
    b'\xFF\xD8\xFF\xE1': 'jpg',
    b'\x47\x49\x46\x38\x37\x61': 'gif',
    b'\x47\x49\x46\x38\x39\x61': 'gif',
    b'\x42\x4D': 'bmp',
    b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A': 'png'
}

with open('path/to/image', 'rb') as fp:
    img_bytes = fp.read()

for k, v in img_types.items():
    if img_bytes.startswith(k):
        img_type = v
        break
else:
    img_type = None
James
  • 32,991
  • 4
  • 47
  • 70
0

Did you check this question and this one? They are very similar with your case. Also AFAIK original format of the image does not make any difference if you convert them into bytes. So linked questions/answers should be fine. However, if they are not working, please update your question.

herdogan
  • 101
  • 6