pillow - Django. Download Image with requests - edit with PIL - save to model -
i'm having trouble process: want download image using requests
, crop pil
, save model imagefield
.
this have now:
from django.core.files.base import contentfile import requests stringio import stringio pil import image def get_img_url(url_img): answer = requests.get(url_img) image = image.open(stringio(answer.content)) new_image = image.crop((22, 44, 221, 165)) return image class formnewcard(forms.modelform): .... def save(self, **kwargs): url_image = self.cleaned_data['imagen'] pil_img = get_img_url(url_img) stringio_obj = stringio() try: pil_img.save(stringio_obj, format="jpg") final_image = stringio_obj.getvalue() self.image = contentfile(final_image) finally: stringio_obj.close()
i error:
pil_img.save(stringio_obj, format="jpg") keyerror @ /hisoka/new_card/ 'jpg'
i know error purely pil problem, i'm not sure how solve (tried several times), also, i'd know if i'm doing correct or if there better way achieve this.
since, keyerror
key being 'jpg'
, it's pillow doesn't recognize it.
but if have @ docs, you'll notice spelled jpeg
. hence, set format='jpeg'
.
Comments
Post a Comment