简体   繁体   English

如何使用PIL调整大小或在python上正确缩放图像

[英]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 我想知道你们是否知道如何使用PIL方法正确缩放图像,我已经读过,并且知道:im.resize(size_tuple),但是一旦缩放后,我只会使图像不完整,这是我的代码,结果我的照片,希望能对我有所帮助,谢谢

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. 我怀疑您的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. 我会尝试重新安装(或尝试其他版本)您的PIL模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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