58
votes

How do I read an image file and decode it using Python?

1
Please specify what you mean with "read", what is your purpose? - adamse
Please specify what you mean with "decode", what do you want to do with the resulting image? - reinierpost
Install Pillow. - Antti Haapala
You can read as binary, aka using the rb flags. in order to transmit the raw image data to another program via python. Python itself would need an image viewer to present that data as visual non-binary content. - NuclearPeon

1 Answers

53
votes

The word "read" is vague, but here is an example which reads a jpeg file using the Image class, and prints information about it.

from PIL import Image
jpgfile = Image.open("picture.jpg")

print(jpgfile.bits, jpgfile.size, jpgfile.format)