58

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

Nimmy
  • 5,051
  • 7
  • 33
  • 32

1 Answers1

53

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)
divibisan
  • 11,659
  • 11
  • 40
  • 58
TheGentleOne
  • 1,735
  • 2
  • 11
  • 7
  • 1
    I want to read the jpg/png image & decode it . – Nimmy Sep 20 '10 at 12:27
  • 17
    If I run that code in the same directory as a file named "picture.jpg", I get the error: `Traceback (most recent call last): File "test.py", line 2, in import Image ImportError: No module named Image`. The os, sys imports are extraneous; you probably meant `from PIL import Image`, which requires running `easy_install PIL`, or if you're not feeling lucky (PIL requires a substantial amount of luck to install), `easy_install pillow`. – chbrown Jul 20 '14 at 21:33
  • What's the `import os,sys` used for? – caird coinheringaahing Apr 07 '17 at 15:38
  • 1
    I think it should be image, not Image, – Mohanad Kaleia Apr 20 '17 at 20:42