How to save generated file content to ImageField
in Django?
My current code is:
from django.contrib.contenttypes.models import ContentType
image = create_screenshot(url)
print type(image) # <type 'str'>
screenshot = ScreenshotModel(...)
screenshot.image.save('filename.jpg', ContentFile(image), save=True)
This produces no errors while saving, but gives error while trying to show image (using sorl.thumbnail
):
IOError: cannot identify image file <cStringIO.StringI object at 0x7fda5322c140>
File is being saved on disk properly, but it is not image and I cannot display it. I have checked file mime type and it is application/octet-stream
.
Function create_screenshot
generates image using PhantomJS
and returns raw output of phantomjs
script - when I use it to generate PDF documents and saving it to FileField
everything is ok.