python save函数生产的图片比原来大_python - 如何使用PIL调整图像大小并保持其宽高比?...

本文介绍了如何使用Python的PIL库来调整图像大小,并保持其原始的宽高比。通过设置最大尺寸,利用Image.thumbnail()方法和Image.ANTIALIAS选项,可以创建高质量的缩略图。示例代码展示了具体的操作步骤。
摘要由CSDN通过智能技术生成

python - 如何使用PIL调整图像大小并保持其宽高比?

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

11个解决方案

374 votes

定义最大尺寸。然后,通过取Image.thumbnail计算调整大小比率。

适当的大小是Image.thumbnail。

当然还有一种库方法:方法Image.thumbnail。

以下是PIL文档中的(已编辑)示例。

import os, sys

import Image

size = 128, 128

for infile in sys.argv[1:]:

outfile = os.path.splitext(infile)[0] + ".thumbnail"

if infile != outfile:

try:

im = Image.open(infile)

im.thumbnail(size, Image.ANTIALIAS)

im.save(outfile, "JPEG")

except IOError:

print "cannot create thumbnail for '%s'" % infile

gnud answered 2019-02-06T07:59:39Z

174 votes

此脚本将使用PIL(Python成像库)调整图像(somepic.jpg)的大小,宽度为300像素,高度与新宽度成比例。 它通过确定300像素的原始宽度(img.size [0])然后将原始高度(img.size [1])乘以该百分比来实现。 将“basewidth”更改为任何其他数字以更改图像的默认宽度。

from PIL import Image

basewidth = 300

img = Image.open('somepic.jpg')

wpercent = (basewidth/float(img.size[0]))

hsize = int((float(img.size[1])*float(wpercent)))

img = img.resize((basewidth,hsize), Image.ANTIALIAS)

img.save('sompic.jpg')

tomvon answered 2019-02-06T08:00:02Z

48 votes

我还建议使用PIL的缩略图方法,因为它消除了你的所有比例麻烦。

但有一个重要提示:替换

im.thumbnail(size)

im.thumbnail(size,Image.ANTIALIAS)

默认情况下,PIL使用Image.NEAREST过滤器进行大小调整,从而获得良好的性能,但质量很差。

Franz answered 2019-02-06T08:00:38Z

24 votes

总部设在@tomvon,我完成了以下工作:

调整宽度:

new_width = 680

new_height = new_width * height / width

调整高度:

new_height = 680

new_width = new_height * width / height

然后就是:

img = img.resize((new_width, new_height), Image.ANTIALIAS)

muZk answered 2019-02-06T08:01:19Z

14 votes

PIL已经可以选择裁剪图像

img = ImageOps.fit(img, size, Image.ANTIALIAS)

Cícero Verneck Corrêa answered 2019-02-06T08:01:41Z

10 votes

如果您尝试保持相同的宽高比,那么您不会调整原始大小的某个百分比吗?

例如,原始尺寸的一半

half = 0.5

out = im.resize( [int(half * s) for s in im.size] )

Chris Cameron answered 2019-02-06T08:02:10Z

10 votes

from PIL import Image

img = Image.open('/your iamge path/image.jpg') # image extension *.png,*.jpg

new_width = 200

new_height = 300

img = img.resize((new_width, new_height), Image.ANTIALIAS)

img.save('output image name.png') # format may what u want ,*.png,*jpg,*.gif

Mohideen ibn Mohammed answered 2019-02-06T08:02:25Z

2 votes

from PIL import Image

from resizeimage import resizeimage

def resize_file(in_file, out_file, size):

with open(in_file) as fd:

image = resizeimage.resize_thumbnail(Image.open(fd), size)

image.save(out_file)

image.close()

resize_file('foo.tif', 'foo_small.jpg', (256, 256))

我用这个库:

pip install python-resize-image

guettli answered 2019-02-06T08:02:47Z

1 votes

我丑陋的例子。

函数获取文件如:“pic [0-9a-z]。[extension]”,将它们调整为120x120,将部分移动到中心并保存到“ico [0-9a-z]。[extension]”,使用纵向 和景观:

def imageResize(filepath):

from PIL import Image

file_dir=os.path.split(filepath)

img = Image.open(filepath)

if img.size[0] > img.size[1]:

aspect = img.size[1]/120

new_size = (img.size[0]/aspect, 120)

else:

aspect = img.size[0]/120

new_size = (120, img.size[1]/aspect)

img.resize(new_size).save(file_dir[0]+'/ico'+file_dir[1][3:])

img = Image.open(file_dir[0]+'/ico'+file_dir[1][3:])

if img.size[0] > img.size[1]:

new_img = img.crop( (

(((img.size[0])-120)/2),

0,

120+(((img.size[0])-120)/2),

120

) )

else:

new_img = img.crop( (

0,

(((img.size[1])-120)/2),

120,

120+(((img.size[1])-120)/2)

) )

new_img.save(file_dir[0]+'/ico'+file_dir[1][3:])

Nips answered 2019-02-06T08:03:15Z

1 votes

一种保持约束比率并传递最大宽度/高度的简单方法。 不是最漂亮,但完成工作并且易于理解:

def resize(img_path, max_px_size, output_folder):

with Image.open(img_path) as img:

width_0, height_0 = img.size

out_f_name = os.path.split(img_path)[-1]

out_f_path = os.path.join(output_folder, out_f_name)

if max((width_0, height_0)) <= max_px_size:

print('writing {} to disk (no change from original)'.format(out_f_path))

img.save(out_f_path)

return

if width_0 > height_0:

wpercent = max_px_size / float(width_0)

hsize = int(float(height_0) * float(wpercent))

img = img.resize((max_px_size, hsize), Image.ANTIALIAS)

print('writing {} to disk'.format(out_f_path))

img.save(out_f_path)

return

if width_0 < height_0:

hpercent = max_px_size / float(height_0)

wsize = int(float(width_0) * float(hpercent))

img = img.resize((max_px_size, wsize), Image.ANTIALIAS)

print('writing {} to disk'.format(out_f_path))

img.save(out_f_path)

return

这是一个使用此函数运行批量图像大小调整的python脚本。

AlexG answered 2019-02-06T08:03:44Z

0 votes

只需使用更现代的包装器更新此问题这个库封装了Pillow(PIL的一个分支)[https://pypi.org/project/python-resize-image/]

允许你这样做: -

from PIL import Image

from resizeimage import resizeimage

fd_img = open('test-image.jpeg', 'r')

img = Image.open(fd_img)

img = resizeimage.resize_width(img, 200)

img.save('test-image-width.jpeg', img.format)

fd_img.close()

在上面的链接中添加了更多示例。

Shanness answered 2019-02-06T08:04:19Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值