From an image that captured from an online MJPG streamer by CV2, I want to count its colors by Pillow.
What I tried:
import cv2
from PIL import Image
url = "http://link-to-the-online-MJPG-streamer/mjpg/video.mjpg"
cap = cv2.VideoCapture(url)
ret, original_image = cap.read()
img_cv2 = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
img_pil = Image.fromarray(img_cv2)
im = Image.open(img_cv2).convert('RGB')
na = np.array(im)
colours, counts = np.unique(na.reshape(-1,3), axis=0, return_counts=1)
print(len(counts))
However it shows an error.
What is the right way to convert the CV2 captured image to Pillow format?
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages \PIL\Image.py", line 3231, in open
fp.seek(0)
AttributeError: 'numpy.ndarray' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python\Script.py", line 17, in <module>
im = Image.open(img_cv2).convert('RGB')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages \PIL\Image.py", line 3233, in open
fp = io.BytesIO(fp.read())
AttributeError: 'numpy.ndarray' object has no attribute 'read'