at my models.py i got a class named Post with and ImageField called postcover. I want to save every image in PNG format which is working fine so far but i have no idea how i could keep the actual image aspectio ratio after processing the image because currently i staticaly convert it into 4:3 format ratio while saveing it as 500 by 375 pixels.
def save(self, *args, **kwargs):
super(Post, self).save(*args, **kwargs)
if self.postcover:
if os.path.exists(self.postcover.path):
imageTemproary = Image.open(self.postcover)
outputIoStream = BytesIO()
imageTemproaryResized = imageTemproary.resize((500, 375))
imageTemproaryResized.save(outputIoStream, format='PNG')
outputIoStream.seek(0)
self.postcover = InMemoryUploadedFile(outputIoStream, 'ImageField',
"%s.png" % self.postcover.name.split('.')[0], 'image/png',
sys.getsizeof(outputIoStream), None)
super(Post, self).save(*args, **kwargs)
is there any way how i could set a max width and height while i keep the format?
UPDATE:
if i try it like this (see post below):
def save(self, *args, **kwargs):
super(Post, self).save(*args, **kwargs)
if self.postcover:
if os.path.exists(self.postcover.path):
imageTemproary = Image.open(self.postcover)
outputIoStream = BytesIO()
baseheight = 500
hpercent = (baseheight / float(self.postcover.size[1]))
wsize = int((float(self.postcover.size[0]) * float(hpercent)))
imageTemproaryResized = imageTemproary.resize((wsize, baseheight))
imageTemproaryResized.save(outputIoStream, format='PNG')
outputIoStream.seek(0)
self.postcover = InMemoryUploadedFile(outputIoStream, 'ImageField',
"%s.png" % self.postcover.name.split('.')[0], 'image/png',
sys.getsizeof(outputIoStream), None)
super(Post, self).save(*args, **kwargs)
i just get the error:
'int' object is not subscriptable