Python 属性错误 : 'NoneType' object has no attribute 'save' when trying to save a cropped image

标签 python python-imaging-library

我正在尝试使图像的一部分变得模糊。最终我想模糊脸部,但我不能只模糊一部分。我正在尝试裁剪图像的一部分,然后将其粘贴回原始图像。我可以裁剪它,但是当我去保存粘贴裁剪区域的图像时,我收到一个“AttributeError:‘NoneType’对象没有属性‘save’”

这是我使用的代码:

import Image, ImageFilter

picture = Image.open("picture1.jpg")

#finds width and height of picture
width, height = picture.size

#crops the picture
box = (20, 20, width/2, height/2)
ic = picture.crop(box)

#blurs the cropped part of the picture
ic = ic.filter(ImageFilter.GaussianBlur(radius=20))

#pastes the image back    
blurredPic = picture.paste(ic, box)

#saves the new image and the cropped image
blurredPic.save("BlurredPic.jpg")
ic.save("cropPic.jpg")

非常感谢您的帮助。

最佳答案

picture.paste(ic, box) 就地改变 picture 并返回 None

即。

#blurs the cropped part of the picture
ic = ic.filter(ImageFilter.GaussianBlur(radius=20))

#pastes the image back    
picture.paste(ic, box)

#saves the new image and the cropped image
picture.save("BlurredPic.jpg")
ic.save("cropPic.jpg")

关于Python 属性错误 : 'NoneType' object has no attribute 'save' when trying to save a cropped image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508074/

相关文章:

python - 为什么 ImageGrab.grab 无法捕获整个屏幕?

python - 计算不同颜色的像素 - Python

python - 坏值错误 : Indexed value message_text must be at most 1500 bytes

python - ReadFromKafka 抛出 ValueError : Unsupported signal: 2

python - 如何使用python中的tkinter上的保存按钮保存图像

python - 如何在 Python 中改进水印图像?

python - 使用 Pillow 在 Python 中将数组转换为图像 (tif)

python - 我们如何获得 __repr__() 的默认行为?

Python读写2个DB

python - 询问数字或字母时如何获得有效答案