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

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')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用