3

I am very new in Python and this is going to be a very basic question.I have a website which is image based and i am developing it using Django.Now i want to resize the image or you can say i want to minimize the size of the images.There are different size of images are avaible,some images are largest in width,some images are largest in height and i want to resize images without changing there shape.

Here are some example what dimensions images are using in my website.

enter image description here

enter image description here

Here the First image is largest in width and the second image is largest in height and they are really big in Dimension.so they need to be resized or rather these images are need to be minimized in size.So i have used the PIL as below.

from PIL import Image,ImageDraw, ImageFont, ImageEnhance

def image_resize(request,image_id):
    photo = Photo.objects.get(pk=image_id)
    img = Image.open(photo.photo.file)
    image = img.resize((1000, 560), Image.ANTIALIAS)
    image.save()

so this function returns all the images with width of 1000 and height of 560.But i don't want to resize all the images with same width and height,rather i want to resize each images maintaining there own shape. That is there shape will be same but the images will be resized.How can i do this? i am really new in python.

Md. Tanvir Raihan
  • 4,075
  • 9
  • 37
  • 70
  • 1. [Get the size of the image](http://stackoverflow.com/q/6444548/3001761); 2. Calculate the new size, retaining the same ratio height/width; 3. Resize to the new size. So far you haven't even attempted steps 1 and 2. – jonrsharpe Aug 05 '14 at 11:13
  • 1
    A very similar question is answered in detail here: http://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio – VBB Aug 05 '14 at 11:15
  • I want to resize the images maintaining the largest side.That is if the largest side is the width of the image,then width will be unchanged and height will be resized and vise-versa.is it possible? – Md. Tanvir Raihan Aug 05 '14 at 11:23
  • Yes, of course it's possible, but this isn't a code-writing service. However, I don't think that makes sense - you want to keep the *ratio* between width and height, to avoid "squashing" the image, and will have to reduce height, width or both to fix within your maximum. Keeping one fixed while changing the other will distort the image. – jonrsharpe Aug 05 '14 at 11:25

2 Answers2

3

Do you want to have all images with same width 1000? Try this code. It will resize to at most 1000 as width (if the image's width is less than 1000, nothing changes)

def image_resize(request,image_id):
    photo = Photo.objects.get(pk=image_id)
    image = Image.open(photo.photo.file)
    (w,h) = image.size
    if (w > 1000):
        h = int(h * 1000. / w)
        w = 1000
    image = image.resize((w, h), Image.ANTIALIAS)
    image.save()
John Pang
  • 2,403
  • 25
  • 25
0

I recall doing this sometime back without any problem except that I used thumbnail method rather than resize. Try it. You need not assign img to image. You can process img and save the same.

# open img
img.thumbnail((1000,560), Image.ANTIALIAS)
# save img
coder.in.me
  • 1,048
  • 9
  • 19