Feb-03-2024, 06:30 PM
Dear community,
I found a code at stackoverflow which shall get the image's color count very fast.
https://stackoverflow.com/questions/7139...n-an-image
I'm trying to get the second function working:
def count_colors_2(cv_img: np.array) -> list: # no need to give colors
The situation is the following:
I've got a pdf-file ("t2.pdf"), which I convert to the bmp-file "bmpImage.bmp" (line 23).
Then I open the image with openCV2 (line 25).
I don't know why "colors_count_list" is NoneType (line 14).
Here is my attempt:
Many thanks...
I found a code at stackoverflow which shall get the image's color count very fast.
https://stackoverflow.com/questions/7139...n-an-image
I'm trying to get the second function working:
def count_colors_2(cv_img: np.array) -> list: # no need to give colors
The situation is the following:
I've got a pdf-file ("t2.pdf"), which I convert to the bmp-file "bmpImage.bmp" (line 23).
Then I open the image with openCV2 (line 25).
I don't know why "colors_count_list" is NoneType (line 14).
Here is my attempt:
import time import numpy as np from PIL import Image from pdf2image import convert_from_path import cv2 colors_count_list = [] def count_colors_2(cv_image: np.array) -> list: # no need to give colors pil_image = Image.fromarray(cv_image) colors_count_list = pil_image.getcolors() print('count_colors time elapsed: {:.10f}s'.format(time.time() - start_time)) for count, c_bgr in colors_count_list: print('\tcolor {} appeared {} times'.format(c_bgr, count)) return colors_count_list if __name__ == '__main__': start_time = time.time() # save pdf to bmp pages = convert_from_path("t2.pdf", 300) pages[0].save("bmpImage.bmp", "BMP") # Open image using openCV2 opencv_image = cv2.imread("bmpImage.bmp") colors_count_list = count_colors_2(opencv_image) print(colors_count_list)
Error:Traceback (most recent call last):
File "D:\Daten\aktuell\testOpenCVColorCount\main.py", line 26, in <module>
colors_count_list = count_colors_2(opencv_image)
File "D:\Daten\aktuell\testOpenCVColorCount\main.py", line 14, in count_colors_2
for count, c_bgr in colors_count_list:
TypeError: 'NoneType' object is not iterable
Process finished with exit code 1
Please be so kind and help me...Many thanks...