Generate in-memory image for Django testing

Jason's accepted answer is not working for me in Django 1.5

Assuming the generated file is to be saved to a model's ImageField from within a unit test, I needed to take it a step further by creating a ContentFile to get it to work:

from PIL import Image
from StringIO import StringIO

from django.core.files.base import ContentFile

image_file = StringIO()
image = Image.new('RGBA', size=(50,50), color=(256,0,0))
image.save(image_file, 'png')
image_file.seek(0)

django_friendly_file = ContentFile(image_file.read(), 'test.png')

To generate a 200x200 test image of solid red:

import Image
size = (200,200)
color = (255,0,0,0)
img = Image.new("RGBA",size,color)

To convert it to a file-like object, then:

import StringIO
f = StringIO.StringIO(img.tostring())

http://effbot.org/imagingbook/image.htm