有没有一种明显的方式来做到这一点,我失踪了?我只是想制作缩略图。如何使用PIL调整图像大小并保持其宽高比?
回答
定义最大尺寸。 然后,通过取min(maxwidth/width, maxheight/height)
来计算调整比例。
正确的尺寸是oldsize*ratio
。
当然还有一个库方法可以做到这一点:方法Image.thumbnail
。
以下是来自PIL documentation的(编辑)示例。
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
** [Randy Syring]评论(http://stackoverflow.com/users/182111/randy-syring)**: 将`Image.ANTIALIAS`添加到`thumbnail()`调用。这是强烈建议在文档中,这是最好的答案,应遵循最佳实践。换句话说:用`im.thumbnail(size,Image.ANTIALIAS)`替换`im.thumbnail(size)`。 – Anne 2011-12-07 21:50:40
就像它说的那样,这个例子来自pil文档,那个例子(仍然)不使用antialias标志。因为这可能是大多数人想要的,但是,我添加了它。 – gnud 2011-12-08 08:29:50
PIL将新图像的高度设置为给定的尺寸(此处为128),并计算宽度以保持纵横比。有没有办法修复宽度而不是高度?也许我会在单独的问题中提出这个问题。 – eugene 2012-12-13 08:22:32
如果您试图保持相同的宽高比,那么您是否不会调整原始大小的某个百分比?
例如,原来的一半大小
half = 0.5
out = im.resize([int(half * s) for s in im.size])
该脚本使用PIL(Python图像库)为300个像素的宽度和正比于新的宽度的高度将调整的图像(somepic.jpg) 。它通过确定300像素是原始宽度(img.size [0])的百分比,然后将原始高度(img.size [1])乘以该百分比来实现。将“基础宽度”更改为任何其他数字以更改图像的默认宽度。
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')
我还推荐使用PIL的缩略图方法,因为它可以消除所有的比例麻烦。
一个重要的暗示,虽然:在默认情况下
im.thumbnail(size,Image.ANTIALIAS)
更换
im.thumbnail(size)
,PIL使用Image.NEAREST过滤器的大小调整导致性能好,但质量较差。
PIL已经裁剪图像
img = ImageOps.fit(img, size, Image.ANTIALIAS)
我的丑例子的选项。
函数获取如下文件:“pic [0-9a-z]。[扩展名]”,将它们的大小调整为120x120,将部分移动到中心并保存到“ico [0-9a-z]”。[延伸]”,可与纵向和横向:
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:])
基于在@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)
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
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
- 1. 如何重新调整图像大小并保持长宽比
- 2. 调整图像大小,同时保持宽高比的Java
- 3. 调整图像大小保持高宽比白线边框
- 4. App Engine图像大小调整不保持高宽比?
- 5. 调整图像大小 - 保持宽高比
- 6. 如何在调整上传图像大小时保持宽高比
- 7. 调整图像大小保持比例
- 8. javascript将图像调整为全屏并保持宽高比
- 9. 保持图像宽高比,并使用宽度百分比
- 10. 将图像大小调整为平方div保持长宽比
- 11. 使用相同宽高比调整大小的多个图像
- 12. AppEngine Images API仅在宽度上调整大小并保持宽高比
- 13. 如何在JavaScript中缩放(调整大小)图片保持宽高比
- 14. 调整大小时保持div宽高比
- 15. 在保持宽高比的同时调整窗体大小
- 16. 调整图像大小(image_lib),保持宽高比,但缩放到最小可能超过宽度和高度
- 17. 调整图像大小并保持纵横比
- 18. 使用窗口宽度调整图像大小,但保持比例
- 19. 如何调整图像大小,但保持与JavaScript的比例?
- 20. Python PIL调整大小是否保持纵横比?
- 21. 在Flex中调整表的大小,同时保持宽高比,如何?
- 22. 宽高比图像调整大小上的excanvas错误
- 23. 在调整窗口大小时调整DIV大小但保持长宽比
- 24. 如何使用grunt将图像大小调整为其大小的百分比?
- 25. 如何在保持宽高比的同时在网页上显示调整大小的图像?
- 26. Jquery,通过保持高度不变的宽度来调整图像大小
- 27. 保持图像的宽高比?
- 28. React Native保持宽高比图像ios
- 29. 如何调整移动设备上的CSS图像,同时保持宽高比
- 30. 使用PHP调整大小(宽度/高度)图像
由于这个问题是很老,但有用的,枕头是相当首选,对于基于枕教程看看这个:HTTP ://pillow.readthedocs.org/en/latest/handbook/tutorial.html#create-jpeg-thumbnails – Wtower 2015-03-26 10:27:53
我创建了一个用于调整图像大小的小型库,它可以提供任何帮助:https://github.com/ charlesthk/python-resize-image – Charlesthk 2015-04-28 14:49:12