PIL open.image & convert problems

PIL Image.open.convert (L) gives me a strange result:

from PIL import Image test_img = Image.open('test.jpg').convert('L') imshow(test_img) show() 
  • he rotates the image (?)
  • it does not convert it to L (?)

(sorry, I'm new, so I can not send images as a demonstration)

Why (if you have an idea)?

+4
source share
2 answers

Rotation occurs because PIL and matplotlib do not use the same conventions. If you execute test_img.show (), it will not rotate the image. Alternatively, you can convert your image to a numpy array before displaying with matplotlib:

 imshow(np.asarray(test_img)) 

Regarding the .convert ('L') method, it works for me:

 test_img = Image.open('test.jpg').convert('L') print test_img.mode # 'L' 
+2
source

Your image rotates due to inconsistencies in the origin of bewteen Image and pylab . If you use this fragment, the image will not be flipped in the reverse order.

 import pylab as pl import Image im = Image.open('test.jpg').convert('L') pl.imshow(im, origin='lower') pl.show() 

However, the image will not be displayed in black and white. To do this, you need to specify a color code for shades of gray:

 import pylab as pl import Image import matplotlib.cm as cm im = Image.open('test.jpg').convert('L') pl.imshow(im, origin='lower', cmap=cm.Greys_r) pl.show() 

Et voilà!

+2
source

All Articles