这种裁剪算法的更优雅的改写是什么? (Python)

发布于 2024-07-16 17:46:02 字数 648 浏览 13 评论 0原文

我想在 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)

你有什么想法吗?

编辑:解释x,y

I want to crop a thumbnail image in my Django application, so that I get a quadratic image that shows the center of the image. This is not very hard, I agree.

I have already written some code that does exactly this, but somehow it lacks a certain ... elegance. I don't want to play code golf, but there must be a way to express this shorter and more pythonic, I think.

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)

Do you have any ideas, SO?

edit: explained x, y

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝天 2024-07-23 17:46:02

我认为这应该可以。

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)

I think this should do.

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)
青春如此纠结 2024-07-23 17:46:02

PIL 中的 fit() 函数 ImageOps 模块执行您想要的操作:

ImageOps.fit(image, (min(*image.size),) * 2, Image.ANTIALIAS, 0, (.5, .5))

The fit() function in the PIL ImageOps module does what you want:

ImageOps.fit(image, (min(*image.size),) * 2, Image.ANTIALIAS, 0, (.5, .5))
萌能量女王 2024-07-23 17:46:02
width, height = image.size
if width > height:
    crop_box = # something 1
else:
    crop_box = # something 2
image = image.crop(crop_box)
image.thumbnail([x, x], Image.ANTIALIAS)   # explicitly show "square" thumbnail
width, height = image.size
if width > height:
    crop_box = # something 1
else:
    crop_box = # something 2
image = image.crop(crop_box)
image.thumbnail([x, x], Image.ANTIALIAS)   # explicitly show "square" thumbnail
穿越时光隧道 2024-07-23 17:46:02

我想要对 jepg 图像进行内容分析。 我希望采用 251 x 261 的 jpeg imafe,并通过算法将其裁剪为 96 x 87。这个程序可以像编写智能裁剪算法一样做到这一点,并提示重新调整图像。

I want to a content analysis of a jepg image. I wish to take a jpeg imafe say 251 x 261 and pass it through an algorithm to crop it to say 96 x 87. Can this program do that like t write an intelligent cropping algorithm, with a prompt to rezie the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文