Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Pillow not recognize the JPEG format?

Trying out this simple code to write text on an image:

import ImageFont
import Image
import ImageDraw

font = ImageFont.truetype("arial.ttf", 16)
img=Image.new("RGB", (200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)

img.save("C:/Users/User/Desktop/test","jpeg")

and I get this error:

File "C:\Users\User\Anaconda\lib\site-packages\PIL\Image.py", line 1456, in save  
   save_handler = SAVE[format.upper()] # unknown format
KeyError: 'JPEG'

Any idea of how to fix this? I am using Python 2.7.5 Anaconda version in Windows 7 with Eclipse Kepler and PyDev plugin. I also tried img.save("test.jpeg") and img.save("test.png") resulting in the same error.

like image 707
Cartesian Theater Avatar asked Jan 15 '14 02:01

Cartesian Theater


2 Answers

Turned out that Eclipse was using PIL rather than Pillow: I just deleted the PIL library reference in Eclipse and made sure Pillow was being used instead and it ran fine.

like image 128
Cartesian Theater Avatar answered Oct 05 '22 01:10

Cartesian Theater


Try just running

img.save("test.jpg")

and see if that does the trick. You shouldn't have to specify the type of file if you also supply a valid extension.

like image 45
MattDMo Avatar answered Oct 05 '22 01:10

MattDMo