【问题标题】:Python AttributeError: 'NoneType' object has no attribute 'save' when trying to save a cropped imagePython AttributeError:尝试保存裁剪图像时,“NoneType”对象没有“保存”属性
【发布时间】:2013-10-22 10:41:25
【问题描述】:

我正在尝试使图像的一部分模糊。最终我想模糊脸部,但我不能只模糊一部分。我正在尝试裁剪图像的一部分,然后将其粘贴回原始图像。我可以裁剪它,但是当我去保存粘贴了裁剪区域的图像时,我收到一个“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")

非常感谢您的帮助。

【问题讨论】:

    标签: python python-imaging-library


    【解决方案1】:

    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")
    

    【讨论】:

      【解决方案2】:

      我不确定这是否是您正在使用的库,但我在这里找到了 Image 库的一些示例:http://www.riisen.dk/dop/pil.html。看一下 Transpose 的例子,好像粘贴功能就到位了。

      当您调用picture.paste(ic, box) 时,您所做的是更新图片,而不是返回任何内容。结果blurredPic的值为None,自然没有“save”这个属性。

      我会将最后 3 行更改为:

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

      如果这不是正确的库,或者不能解决您的问题,请告诉我。

      【讨论】:

        猜你喜欢
        • 2015-07-31
        • 2020-10-27
        • 2019-10-18
        • 1970-01-01
        • 1970-01-01
        • 2020-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多