2

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.

Morgoth
  • 4,935
  • 8
  • 40
  • 66
user2452537
  • 43
  • 1
  • 9

3 Answers3

5

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

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

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

please save with .jpg extention eg:

im.save("g.jpg")
user3872486
  • 97
  • 1
  • 2
  • 10