This works:
from PIL import Image, ImageFont, ImageDraw
def create_image_file(name='test.jpeg', ext='jpeg', size=(500, 500), color=(5, 179, 200)):
file_obj = open(name, 'w')
image = Image.new("RGBA", size=size, color=color)
usr_font = ImageFont.truetype(
"/Users/myuser/ENV/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf", 59)
d_usr = ImageDraw.Draw(image)
d_usr = d_usr.text((105, 280), "Test Image", (0, 0, 0), font=usr_font)
image.save(file_obj, ext)
file_obj.close()
if __name__ == '__main__':
f = create_image_file()
But if I change the arguments to:
def create_image_file(name='test.jpg', ext='jpg', ...)
An exception is raised:
File "/Users/myuser/project/venv/lib/python2.7/site-packages/PIL/Image.py", line 1681, in save
save_handler = SAVE[format.upper()]
KeyError: 'JPG'
And I need to work with user uploaded images that have .jpg
as the extension. Is this a Mac specific problem? Is there something I can do to add the format data to the Image lib?