win*_*ith 4 python image crop python-imaging-library
我想在我的Django应用程序中裁剪缩略图,以便我得到一个显示图像中心的二次图像.我同意,这不是很难.
我已经写了一些代码来完成这个,但不知怎的,它缺乏某种优雅.我不想打代码高尔夫球,但我认为必须有一种方法来表达这种更短更多的pythonic.
x = y = 200 # intended size
image = Image.open(filename)
width = image.size[0]
height = image.size[1]
if (width > height):
crop_box = ( ((width - height)/2), 0, ((width - height)/2)+height, height )
image = image.crop(crop_box)
elif (height > width):
crop_box = ( 0, ((height - width)/2), width, ((height - width)/2)+width )
image = image.crop(crop_box)
image.thumbnail([x, y], Image.ANTIALIAS)
Run Code Online (Sandbox Code Playgroud)
你有什么想法吗?
编辑:解释x,y
我认为应该这样做.
size = min(image.Size)
originX = image.Size[0] / 2 - size / 2
originY = image.Size[1] / 2 - size / 2
cropBox = (originX, originY, originX + size, originY + size)
Run Code Online (Sandbox Code Playgroud)
fit()PIL ImageOps模块中的功能可以满足您的需求:
ImageOps.fit(image, (min(*image.size),) * 2, Image.ANTIALIAS, 0, (.5, .5))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |