31๐
โ
I assume youโre using PIL (Python Imaging Library). You need to replace your last line with (for example, if you want to serve a PNG image):
response = HttpResponse(mimetype="image/png")
img.save(response, "PNG")
return response
See here for more information.
๐คVinay Sajip
3๐
Iโm relatively new to Django myself. I havenโt been able to find anything in Django itself, but I have stumbled upon a project on Google Code that may be of some help to you:
๐คgeowa4
- [Django]-NoReverseMatch at /rest-auth/password/reset/
- [Django]-How can I exclude South migrations from coverage reports using coverage.py
- [Django]-Make django model field read only or disable in admin while saving the object first time
3๐
I was looking for a solution of the same problem
And for me this simple approach worked fine:
from django.http import FileResponse
def dyn_view(request):
response = FileResponse(open("image.png","rb"))
return response
๐คtolazytosignup
- [Django]-Django rest framework โ filtering for serializer field
- [Django]-Django: how does manytomanyfield with through appear in admin?
- [Django]-What's the proper way to test token-based auth using APIRequestFactory?
1๐
Another way is to use BytesIO. BytesIO is like a buffer. So one can save the image (which is fast enough than writing to disk) in that buffer.
from PIL import Image, ImageDraw
import io
def chart(request):
img = Image.new('RGB', (240, 240), color=(250,160,170))
draw = ImageDraw.Draw(img)
draw.text((20, 40), 'some_text')
buff = io.BytesIO()
img.save(buff, 'jpeg')
return HttpResponse(buff.getvalue(), content_type='image/jpeg')
๐คJayanta
- [Django]-What is the best AJAX library for Django?
- [Django]-How to automatically login a user after registration in django
- [Django]-Django โ Get ContentType model by model name (Generic Relations)
Source:stackexchange.com