I would like to resize uploaded images to fit in a (MAX_WIDTH,
MAX_HEIGHT) rectangle when creating model instances. I can use method
Image.resize() from PIL OK in Python, but I don't understand how to
use it in Django. I'm trying to write code like this:
class Item(models.Model):
...
image = models.ImageField(upload_to='images', blank=True,
height_field='height',
width_field='width')
height = models.IntegerField(blank=True, null=True)
width = models.IntegerField(blank=True, null=True)
def save(self):
if self.image:
self.image = resize(self.image, MAX_WIDTH, MAX_HEIGHT)
super(Message, self).save()
And I'm trying to define the function resize() as follows:
def resize(image, width, height):
"""
Return a copy of the image resized proportionally to fit in a
(width, height) rectangle.
"""
if image and (image.width > width or image.height > height):
# print "Resizing image..."
ratio = image.width / image.height
if width / height > ratio:
width = height * ratio
else:
height = width / ratio
im = Image.open(image)
image = im.resize((width, height))
return image
But this doesn't work because Image.open() expects a filename, not a
Django field value, and probably the image returned by im.resize() is
not a Django field value.
So can someone please tell me how I should be writing this code?
Many thanks in advance...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---