python - Django image resizing and convert before upload -
i searched lot on subject couldn't find need. i'll explain problem :
on website, user can upload image. need resize image , convert jpeg before uploading it.
i saw solutions using method in views.py, i'd overriding save() method in models.py. problem is, solutions found resizing image in save method after saving first time (calling super fonction), means it'd utilize bandwidth (if utilize cdn) nil (am right on point?).
thank help
first, it's best found right language. django , python exist on server side. therefore, manipulate, save, or otherwise use, has first sent server. if django or python manage photo, user must upload photo server first. 1 time photo uploaded, django free create changes before storing file.
if concern bandwidth, , don't want big files beingness uploaded, have resize , reformat photo on client side. if web application, can done using javascript, can not done python, since python not operate on client side application yours.
if concern not bandwidth, you're free have user "upload" file, have django resize , reformat before saving.
you right want override save function photo object. recommend using library handle resizing , reformatting, such sorl.
from sorl.thumbnail import imagefield, get_thumbnail class myphoto(models.model): image = imagefield() def save(self, *args, **kwargs): if self.image: self.image = get_thumbnail(self.image, '500x600', quality=99, format='jpeg') super(myphoto, self).save(*args, **kwargs)
sorl library confident , familiar with, takes tuning , configuration. can check out pillow or instead, , replace line overriding self.image
.
i found similar question here.
edit: saw update comment response above. note if webserver handling django, , files beingness saved database on cdn, method work. image resized on webserver before beingness uploaded cdn (assuming configuration i'm assuming).
hope helps!
python django django-models
No comments:
Post a Comment