调整图像大小,保持纵横比,使纵向和横向图像大小完全相同?

2024-05-14 18:09:33 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我正在使用:

    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)

虽然这是可行的,但我最终得到了不同大小的图像(有些是肖像,有些是风景),但我希望所有的图像都有一个准确的大小。也许是合理的种植?

更新:

我不介意剪掉图像的一小部分。当我说合理的裁剪时,我的意思是这样的算法:

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)

Tags: path图像imagesizealbumifosglob
3条回答

显然,您需要裁剪或填充图像。可以执行以下操作,根据缩略图的纵横比(未测试)获得最大的居中裁剪:

aspect = lambda size: float(size[0]) / float(size[1])
sa = aspect(size)
if aspect(im.size) > sa:
    width = int(sa * im.size[1])
    left = (im.size[0] - width) / 2
    im = im.crop((left, 0, left + width, im.size[1]))
else:
    height = int(im.size[0] / sa)
    top = (im.size[1] - height) / 2
    im = im.crop((0, top, im.size[0], top + height))
im.thumbnail(size, Image.ANTIALIAS)

如果使用简单的缩略图,则需要将crop设置为True,并将upscale设置为True,以始终填充空间(具有完全相同的尺寸)。

例:使图像符合图像尺寸:

    thumbnailer = get_thumbnailer(image_2)
    thumbnail = thumbnailer.generate_thumbnail(thumbnail_options={
        'crop': True,
        'upscale': True,
        'size': image_1.size
    })
    image_2 = thumbnail.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)

它首先使用缩略图操作将图像降到原来的范围内并保留外观。然后它将它裁剪出来,以实际填充边界的大小(因为除非原始图像是正方形的,否则它现在会变小),然后我们找到适当的偏移量来使图像居中。图像会偏移到中心,因此最终会使用黑色填充,但不会剪切图像。

除非你能在不丢失边缘重要图像数据的情况下,对一个合适的中心裁剪做出一个非常合理的猜测,否则填充拟合方法会更好地工作。

更新

这是一个版本,可以做中心作物或垫适合。

#!/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)

相关问题 更多 >

    热门问题