调整图像维护宽高比并使纵向和横向图像的大小相同? [英] Resize image maintaining aspect ratio AND making portrait and landscape images exact same size?

查看:287
本文介绍了调整图像维护宽高比并使纵向和横向图像的大小相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用:

  os.chdir(album.path)
images = glob.glob '*。*')

#thumbs size
size = 80,80

图像中的图像:
#create thumb
file,ext = os.path.splitext(image)
im = Image.open(os.path.join(album.path,image))
im.thumbnail(size,Image.ANTIALIAS)
thumb_path = os.path.join(album.path,'thumbs',file +.thumb+.jpeg)
im.save(thumb_path)

尽管如此,我最终会得到不同大小的图片(有些是纵向的,有的是横向的),但是我想让所有的图片都有一个确切的大小。可能是一个明智的裁剪?



更新:



我不介意裁剪图像的一小部分。当我说出明智的裁剪时,我的意思是像这样的algorythm:

 如果图像是人像:
make width 80px
裁剪高度(将超过80像素)
如果图像是横向,则
使高度为80像素
裁剪宽度为80像素(将超过80像素)


解决方案

这是我做一个填充适合的图像:

 #/ usr / bin / env python 

from PIL import Image,ImageChops

F_IN =/path/to/image_in.jpg
F_OUT =/path/to/image_out.jpg

size =(80,80)

image = Image.open(F_IN)
image.thumbnail(size,Image.ANTIALIAS)
image_size = image.size

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)
thumb.save(F_OUT)

它首先使用缩略图操作将图像降低到原始范围内,并保留该方面。然后,它将它退回以实际填充边界的大小(因为除非原始图像是正方形,现在将变小),并且我们找到适当的偏移以使图像居中。图像偏移到中心,所以最终黑色填充,但没有图像裁剪。



除非您可以在正确的中心裁剪上做出明智的猜测,而不会在边缘丢失可能的重要图像数据,否则填充合适的方法将会更好地工作。

更新



这是一个可以做中心裁剪或贴合的版本。 p>

 #!/ usr / bin / env python 

from PIL import Image,ImageChops,ImageOps

def makeThumb(f_in,f_out,size =(80,80),pad = False):

image = Image.open(f_in)
image.thumbnail ,Image.ANTIALIAS)
image_size = image.size

如果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:
thumb = ImageOps.fit(image,size,Image.ANTIALIAS ,(0.5,0.5))

thu mb.save(f_out)


source =/path/to/source/image.JPG

makeThumb(source,/ path / to / source / image_padded.JPG,pad = True)
makeThumb(source,/path/to/source/image_centerCropped.JPG,pad = False)


Currently I am using:

    os.chdir(album.path)
    images = glob.glob('*.*')

    # thumbs size
    size = 80,80

    for image in images:
        #create thumb
        file, ext = os.path.splitext(image)
        im = Image.open(os.path.join(album.path,image))
        im.thumbnail(size, Image.ANTIALIAS)
        thumb_path = os.path.join(album.path, 'thumbs', file + ".thumb" + ".jpeg")
        im.save(thumb_path)

Although this works, I end up with different sizes images (some are portrait and some are landscape), but I want all of the images to have an exact size. Maybe a sensible cropping?

UPDATE:

I don't mind cropping a small portion of the image. When I said sensible cropping I mean something like this algorythm:

if image is portrait:
    make width 80px
    crop the height (will be more than 80px)
else if image is landscape:
    make height 80px
    crop the width to 80px (will be more than 80px)

解决方案

Here is my take on doing a padded fit for an image:

#!/usr/bin/env python

from PIL import Image, ImageChops

F_IN = "/path/to/image_in.jpg"
F_OUT = "/path/to/image_out.jpg"

size = (80,80)

image = Image.open(F_IN)
image.thumbnail(size, Image.ANTIALIAS)
image_size = image.size

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)
thumb.save(F_OUT)

It first uses the thumbnail operation to bring the image down to within your original bounds and preserving the aspect. Then it crops it back out to actually fill the size of your bounds (since unless the original image was square, it will be smaller now), and we find the proper offset to center the image. The image is offset to the center, so you end up with black padding but no image cropping.

Unless you can make a really sensible guess at a proper center crop without losing possible important image data on the edges, a padded fit approach will work better.

Update

Here is a version that can do either center crop or pad fit.

#!/usr/bin/env python

from PIL import Image, ImageChops, ImageOps

def makeThumb(f_in, f_out, size=(80,80), pad=False):

    image = Image.open(f_in)
    image.thumbnail(size, Image.ANTIALIAS)
    image_size = image.size

    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:
        thumb = ImageOps.fit(image, size, Image.ANTIALIAS, (0.5, 0.5))

    thumb.save(f_out)


source = "/path/to/source/image.JPG"

makeThumb(source, "/path/to/source/image_padded.JPG", pad=True)
makeThumb(source, "/path/to/source/image_centerCropped.JPG", pad=False)

这篇关于调整图像维护宽高比并使纵向和横向图像的大小相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆