python - Save Uploaded Image as PNG -
in django app, users can upload images. want save every uploaded image png. i've been trying pil it's not working. still seems saving original image type (whatever user uploaded as). doing wrong?
if form.cleaned_data['dataset_image']: # dataset.dataset_image = form.cleaned_data['dataset_image'] name = 'dataset%s.png' % (dataset.id) size = (200, 200) try: im = image.open(form.cleaned_data['dataset_image']) im.save(name, 'png') print "saved file: ", im except ioerror: # dont' save image pass
when upload jpg (that want convert png), print statement gives this: saved file: <pil.jpegimageplugin.jpegimagefile image mode=rgb size=628x419 @ 0x107354f80>
the print statement refers image
object when opened file. used jpegimageplugin
class because jpeg image. however, when call method save()
, save image based on extension of requested filename, in case, png.
try if aren't convinced.
try: im = image.open(form.cleaned_data['dataset_image']) im.save(name) png_img = image.open(name) print "saved file: ", png_img # it's more clean print `png_img.format`
by way, calling im.save(name)
in case, since name
has extension , method figure out want in format.
Comments
Post a Comment