How to resize an image while uploading and saving it automaticly django?
<p>I'm unable to save the image that is either being resized or uploading directly through my <code>admin</code> panel. I want to resize it through <code>PLP</code> or any other way!</p>

<pre><code>def get_product_image_folder(instance, filename):

return "static/images/product/%s/base/%s" %(instance.product_id, filename)
product_image  = StringIO.StringIO(i.read())
imageImage = Image.open(product_image)

thumbImage = imageImage.resize((100,100))

thumbfile = StringIO()
thumbImage.save(thumbfile, "JPEG")

thumbcontent = ContentFile(thumbfile.getvalue())

newphoto.thumb.save(filename, thumbcontent)
new_photo.save()
</code></pre>

<p>This can be done either in the save method of the model, the save_model method of the admin, or the save method of the form.</p>

<p>I recommend the last one, since it lets you decouple form/validation logic from the model and admin interface. </p>

<p>This could look something like the following:</p>

<pre><code>class MyForm(forms.ModelForm):
    model = MyModel

    ...
    def save(self, *args, **options):
        if self.cleaned_data.get("image_field"):
            image = self.cleaned_data['image_field']
            image = self.resize_image(image)
            self.cleaned_data['image_field'] = image
        super(MyForm, self).save(*args, **options)

    def resize_image(self, image):
        filepath = image.file.path
        pil_image = PIL.Image.open(filepath)
        resized_image = # **similar steps to what you have in your question
        return resized_image
</code></pre>

<p>You can either put this new image in the cleaned_data dictionary so that it saves itself, or you can save it to a new field (something like "my_field_thumbnail") that has editable=False on the model.</p>

<p>More info on the actual process of resizing an image with PIL can be found in other SO questions, eg:
<a href="http://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio">How do I resize an image using PIL and maintain its aspect ratio?</a></p>

python django