简体   繁体   中英

How to properly scale an Image with PIL resize or transform on python

I would like to know if you guys know how to properly scale an Image with PIL methods, Iǘe read and I know with: im.resize(size_tuple) but I just keep getting my image incomplete once it is scale, here is my code and my pictures as result, hope you can help me, thanks

if image_size[0] <  120 and image_size[1] <  155:
     image = image.resize((120,155),Image.ANTIALIAS)
     image.save(f_out)

the code saves my resized image but it shows a black line at the bottom and the image is crop and not complete, any ideas how to solve this?, thanks in advance

UPDATE: this is the complete code I'm using

def makeThumb(f_in, f_out, size=(120,155), pad=False):
    image = Image.open(f_in)
    avatar_size = (120,155)
    image_size = image.size
    method = Image.NEAREST if image_size == avatar_size else Image.ANTIALIAS

    if pad:
        thumb = image.crop( (0, 0, size[0], size[1]) )
        offset_x = max( (size[0] - image_size[0]) / 2, 0 )
        offset_y = max( (size[1] - image_size[1]) / 2, 0 )
        thumb = ImageChops.offset(thumb, offset_x, offset_y)

    else:
        if image_size[0] <  120 and image_size[1] <  155:
            image = image.resize((120,155),Image.ANTIALIAS)
            image.save(f_out)          
        else:
            thumb = ImageOps.fit(image, size, method,0.05,(0.5, 0.5))

I suspect you have a bad version of PIL. Your code exactly, run on your image (downloaded from your link) works for me. That is: resizes filling the frame with no black bar. I would try re-installing (or try another version) of your PIL module.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM