1. 问题描述
- 将博客https://blog.csdn.net/ShadowN1ght/article/details/78571187的代码运行与不同训练集时报以下错误:
“if int(img.getpixel((w, h))) < 190:
**if int(img.getpixel((w, h))) < 190:
TypeError: int() argument must be a string, a bytes-like object or a number, not ‘tuple’
**
” - 在使用PIL.Image的getpixel()方法时,因为输入图片不同返回的是tuple 或者int类型的数据
2. 原因分析
- getpixel()函数在输入图片时三通道的时候,输出时一个 'tuple’格式,在输入图片时单通道的时候,输出时一个 'int’格式
- 原博客使用的数据集是单通道,而我的训练集是三通道
3. 处理方法
- 将输入的图片转换为单通道即可
- 实现的代码
img = Image.open(path)
img = img.convert("I") # 将三通道变成了单通道