Python Forum
Count image's colors very fast
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count image's colors very fast
#1
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:

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...
Reply


Messages In This Thread
Count image's colors very fast - by flash77 - Feb-03-2024, 06:30 PM
RE: Count image's colors very fast - by deanhystad - Feb-03-2024, 08:25 PM
RE: Count image's colors very fast - by Pedroski55 - Feb-04-2024, 02:43 PM
RE: Count image's colors very fast - by flash77 - Feb-04-2024, 05:56 PM
RE: Count image's colors very fast - by deanhystad - Feb-04-2024, 06:42 PM
RE: Count image's colors very fast - by Pedroski55 - Feb-05-2024, 12:22 PM
RE: Count image's colors very fast - by flash77 - Feb-05-2024, 07:32 PM
RE: Count image's colors very fast - by deanhystad - Feb-05-2024, 08:07 PM
RE: Count image's colors very fast - by Pedroski55 - Feb-06-2024, 01:38 PM
RE: Count image's colors very fast - by deanhystad - Feb-06-2024, 02:06 PM
RE: Count image's colors very fast - by flash77 - Feb-06-2024, 08:36 PM
RE: Count image's colors very fast - by Pedroski55 - Feb-07-2024, 02:43 PM
RE: Count image's colors very fast - by deanhystad - Feb-07-2024, 05:14 PM
RE: Count image's colors very fast - by Pedroski55 - Feb-08-2024, 07:11 AM
RE: Count image's colors very fast - by flash77 - Feb-08-2024, 06:18 PM
RE: Count image's colors very fast - by deanhystad - Feb-08-2024, 06:26 PM
RE: Count image's colors very fast - by kumaransh - Feb-26-2024, 05:14 PM
RE: Count image's colors very fast - by flash77 - Mar-05-2024, 04:38 PM
RE: Count image's colors very fast - by deanhystad - Mar-05-2024, 06:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  can openpyxl read font colors mperemsky 3 1,790 May-09-2023, 11:18 AM
Last Post: MindKeeper
  ANSI not working for change of text colors BliepMonster 10 3,474 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
  Row Count and coloumn count Yegor123 4 1,381 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  How to do bar graph with positive and negative values different colors? Mark17 1 5,238 Jun-10-2022, 07:38 PM
Last Post: Mark17
  Plot Back Ground Colors JoeDainton123 0 2,217 Aug-19-2020, 11:09 PM
Last Post: JoeDainton123
  How to fill between the same area with two different colors Staph 0 1,503 Jul-08-2020, 07:01 PM
Last Post: Staph
  How do I map a list of values to specified colors? larkypython 4 2,609 Nov-05-2019, 09:22 AM
Last Post: larkypython
  after using openpyxl to add colors to script, black shows up white online in excel Soundtechscott 1 3,716 Jun-08-2019, 10:33 PM
Last Post: Soundtechscott
  How to plot two list on the same graph with different colors? Alberto 2 28,861 Jul-18-2017, 09:20 AM
Last Post: Alberto
  How to print in colors JohnNo 2 3,200 Apr-01-2017, 02:14 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020