相关疑难解决方法(0)

如何使用PIL调整图像大小并保持其纵横比?

是否有一种显而易见的方法可以解决这个问题?我只是想制作缩略图.

python image thumbnails python-imaging-library

394
推荐指数
13
解决办法
60万
查看次数

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

目前我正在使用:

    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)
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但我最终得到了不同尺寸的图像(有些是肖像,有些是风景),但我希望所有图像都具有精确的尺寸.也许是一个明智的裁剪?

更新:

我不介意裁剪一小部分图像.当我说明智的裁剪时,我的意思是这样的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)
Run Code Online (Sandbox Code Playgroud)

python django image thumbnails python-imaging-library

11
推荐指数
1
解决办法
1万
查看次数