梦殇空城 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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决