如何使用 python PIL 在处理后的图像中获取一组颜色
How to get set of colours in an processed image using python PIL
我有一张图片,但在处理它时,我无法获得颜色。
这是我的代码:
import cv2
from PIL import Image
im = Image.open ('imag.jpg')
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
from collections import defaultdict
by_color = defaultdict(int)
for pixel in thresh1.getdata():
by_color[pixel] += 1
print (by_color)
如果我 运行 只是原始图像 (im
),效果很好。但是当我运行处理图像(thresh1
)时,出现以下错误:
Traceback (most recent call last):
File "file.py", line 62, in <module>
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
TypeError: Expected cv::UMat for argument 'src'
对于这个函数:
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
要工作,您应该使用 cv2.imread (returns numpy.array
) rather than PIL.Image.open(returns Image
对象)读取图像。
我有一张图片,但在处理它时,我无法获得颜色。 这是我的代码:
import cv2
from PIL import Image
im = Image.open ('imag.jpg')
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
from collections import defaultdict
by_color = defaultdict(int)
for pixel in thresh1.getdata():
by_color[pixel] += 1
print (by_color)
如果我 运行 只是原始图像 (im
),效果很好。但是当我运行处理图像(thresh1
)时,出现以下错误:
Traceback (most recent call last):
File "file.py", line 62, in <module>
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
TypeError: Expected cv::UMat for argument 'src'
对于这个函数:
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
要工作,您应该使用 cv2.imread (returns numpy.array
) rather than PIL.Image.open(returns Image
对象)读取图像。