裁剪图像 - Image.crop 功能不起作用 [英] Cropping image - Image.crop function not working

查看:84
本文介绍了裁剪图像 - Image.crop 功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用于图像裁剪的代码行

I have following line of code for image cropping

im = Image.open('path/to/image.jpg')

outfile = "path/to/dest_img.jpg"
im.copy()

im.crop((0, 0, 500, 500))
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "JPEG")

但它似乎没有裁剪图像.我有更大的图像尺寸,例如2048 x 1536 像素.

But it doesn't seems cropping image. I have bigger image size e.g. 2048 x 1536 px.

[已编辑]

这里也有解决方案,我自己无法回答这个问题,所以在这里添加答案.

Here's solution too, I could not answer this question myself so adding answer here.

实际上是使用新处理程序裁剪返回图像,我意识到我哪里出错了.我应该在新的处理程序中分配作物,如下

Actually crop return image with new handler, I realized where I make mistake. I should have assign crop in new handler, as bellow

crop_img = im.crop((0, 0, 500, 500))

完整代码如下:

im = Image.open('path/to/image.jpg')

outfile = "path/to/dest_img.jpg"
im.copy()

crop_img = im.crop((0, 0, 500, 500))
crop_img.thumbnail(size, Image.ANTIALIAS)
crop_img.save(outfile, "JPEG")

注意,在crop line之后,使用了crop_img handler.

Notice, after crop line, there is crop_img handler being used.

推荐答案

您忘记在某些语句中分配返回值.

You have forgotten to assign the return values in some statements.

im = Image.open('path/to/image.jpg')

outfile = "path/to/dest_img.jpg"

im = im.crop((0, 0, 500, 500))
im = im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "JPEG")

这篇关于裁剪图像 - Image.crop 功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆