[Answered ]-"ValueError: not enough image data" from Pillow when saving to a Django ImageField

1👍

It took a lot of trial and error, but this seems to work:

import io
import requests
from django.core.files.base import ContentFile
from .models import Website

website = Website.objects.get(pk=1)
response = requests.get("https://www.blogger.com/favicon.ico", stream=True)

img = Image.open(io.BytesIO(response.content))

# Convert image to a PNG
buffer = io.BytesIO()
img.save(fp=buffer, format="PNG")

website.favicon.save("favicon.png", ContentFile(buffer.getvalue()))

Leave a comment