user2452537
user2452537

Reputation: 53

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.

Upvotes: 3

Views: 8993

Answers (3)

user3872486
user3872486

Reputation: 97

please save with .jpg extention eg:

im.save("g.jpg")

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121514

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.

Upvotes: 6

unutbu
unutbu

Reputation: 879331

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

Upvotes: 1

Related Questions