- April 7, 2008
- image pil resize
- 1 (after 3 ratings)
This snippet is extracted from my Photo model. I use it to ensure that any uploaded image is constrained to a specified size (resized on save).
In my case, I don't need to maintain a "thumbnail" and "fullsize" version, so I just store the resized version to save space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
from PIL import Image class Photo(models.Model): source = models.ImageField(upload_to='images') def save(self, size=(400, 300)): """ Save Photo after ensuring it is not blank. Resize as needed. """ if not self.id and not self.source: return super(Photo, self).save() filename = self.get_source_filename() image = Image.open(filename) image.thumbnail(size, Image.ANTIALIAS) image.save(filename)
More like this
- Function to create resized versions of an image from a URL and saving it to a local path by obeattie 8 years, 2 months ago
- Async PIL resize of images by FarmKing 3 years, 2 months ago
- Image resize on demand by VidJa 6 years, 4 months ago
- Download an image using requests, resize it using PIL, and save it to a model ImageField. by stewx 1 month ago
- Image Standarization by garcia_marc 7 years ago