Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python imaging library save function

I have just done some image processing using the python image library (PIL) and I can't get the save function to work. the whole code works fine but it just wont save the resulting image. The code is below:

im=Image.new("rgb",(200,10),"#ddd")
draw=Image.draw.draw(im)
draw.text((10,10),"run away",fill="red")
im.save("g.jpeg")

Saving gives an error as unknown extension and even removing the dot doesn't help.

like image 970
user2452537 Avatar asked Nov 25 '25 01:11

user2452537


2 Answers

Use .jpg:

im.save("g.jpg")

The image library determines what encoder to use by the extension, but in certain versions of PIL the JPEG encoder do not register the .jpeg extension, only .jpg.

Another possibility is that your PIL installation doesn't support JPEG at all; try saving the image as a PNG, for example.

like image 120
Martijn Pieters Avatar answered Nov 26 '25 13:11

Martijn Pieters


Replace

draw=Image.draw.draw(im)

with

draw = ImageDraw.Draw(im)

and make sure the height of the new image is tall enough to accomodate the text.

import Image
import ImageDraw

im = Image.new("RGB", (200, 30), "#ddd")
draw = ImageDraw.Draw(im)
draw.text((10, 10), "run away", fill="red")
im.save("g.jpeg")

yields

enter image description here

like image 35
unutbu Avatar answered Nov 26 '25 13:11

unutbu