Requirement
Image object in Pillow to File Object in Django and save them as FileField and FieldFile
Methods
From Stackoverflow
ref: link
import StringIO
from django.core.files.uploadedfile import InMemoryUploadedFile
# Create a file-like object to write thumb data (thumb data previously created
# using PIL, and stored in variable 'thumb')
thumb_io = StringIO.StringIO()
thumb.save(thumb_io, format='JPEG')
# Create a new Django file-like object to be used in models as ImageField using
# InMemoryUploadedFile. If you look at the source in Django, a
# SimpleUploadedFile is essentially instantiated similarly to what is shown here
thumb_file = InMemoryUploadedFile(thumb_io, None, 'foo.jpg', 'image/jpeg',
thumb_io.len, None)
# Once you have a Django file-like object, you may assign it to your ImageField
# and save.
What is InMemoryUploadedFile?
Method I used in KSAFlyer project
def convert_pdf_to_image(page):
images = convert_from_bytes(page.pdf.read())
image_storage = BytesIO()
image = images[0]
image.save(image_storage, format='png')
page.image.save(name=f"{page.name}.png", content=File(image_storage))
page.is_converted_to_image = True
page.save()