这是报错信息
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)
求大神解决