Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert image captured from online MJPG streamer by CV2, to Pillow format

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What is the right way to convert the CV2 captured image to Pillow format?

Thank you.

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'

>Solution :

Updated Answer

There’s no need to use PIL here, you already have a Numpy array (which is what OpenCV uses for images) and you are using np.unique() to count the colours and that is also Numpy array-based.

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)

colours, counts = np.unique(img_cv2.reshape(-1,3), axis=0, return_counts=1)

print(len(counts))

Also, if you just want the number of colours, there’s no real need to convert from BGR to RGB because the number of colours will be the same in either colourspace.


Original Answer

You already have a PIL Image, no need to open another:

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)

colours, counts = np.unique(img_pil.reshape(-1,3), axis=0, return_counts=1)

print(len(counts))

Also, if you just want the number of colours, there’s no real need to convert from BGR to RGB because the number of colours will be the same in either colourspace.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading