梦殇空城 2020-03-13 14:28 采纳率: 0%
浏览 304
已采纳

python程序报错缓冲读取器没有convert属性

这是报错信息

Traceback (most recent call last):
  File "E:\web\test\image_processing.py", line 25, in <module>
    to_black_white("test1.jpg",100)
  File "E:\web\test\image_processing.py", line 7, in to_black_white
    img = img.convert('L')
AttributeError: '_io.TextIOWrapper' object has no attribute 'convert'
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "E:\web\test\image_processing.py"]
[dir: E:\web\test]
[path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\梦殇空城\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\梦殇空城\AppData\Local\Programs\Python\Python36-32\;C:\Users\梦殇空城\AppData\Local\Microsoft\WindowsApps;D:\PyCharm 2018.3.5\bin;]

这是源码

from PIL import Image, ImageFilter

def to_black_white(file_name,threshold):
    #打开图片
    img = open(file_name)
    # 转灰度
    img = img.convert('L')

    #锐化
    img = img.filter(ImageFilter.SHARPEN)

    #遍历像素
    for y in range(height):
        for x in range(width):
            pixel = img.getpixel((x,y))
            if pixel > threshold:
                img.putpixel((x,y),(255))
            else:
                img.putpixel((x,y),(0))

    # 保存图片
    img.save('BW_'+file_name)


to_black_white("test1.jpg",100)

求大神解决

  • 写回答

1条回答 默认 最新

  • 陈年椰子 2020-03-13 14:51
    关注

    代码抄错了吧 , 应该是这样吧?

    
    #打开图片
        img =  Image.open(file_name)
        # 转灰度
        img = img.convert('L')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?