Python Forum
identify not white pixels in bmp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
identify not white pixels in bmp
#10
You are mistaken about pandas and numpy. Pandas uses numpy, so at best pandas can almost be as fast as numpy. Usually pandas is much slower. If pandas is doing something faster, it just means you are not using the right numpy functions. In a recent post about why is pandas running so much slower in python 3.11 than it was in python 3.9 (Issue is related to pandas versions, not python), I made a numpy solution that runs 5000 times faster than the pandas solution in the post. Speed is more about HOW you do things that what tools you are using.

It takes 3 seconds for my primary_color_ratio(image) to process a a 1920 x 1080 image. Over a minute to process 1 second of film. Yuck!

I found it takes almost a second to get the pixels from the image using getdata(), so you should go back to using "pixels = np.array(image).reshape(-1, 3)" which takes less than 0.01 seconds.

Thinking that unique is taking a long time because we are looking for unique arrays, I converted the RGB arrays to a 24bit color.
from PIL import Image
import numpy as np
from time import time


def primary_color_ratio(filename):
    """Return ratio of pixels that are the "background" color."""
    image = Image.open(filename)
    rgb = np.array(image).reshape(-1, 3)
    b24 = rgb[:, 0] * 65535 + rgb[:, 1] * 256 + rgb[:, 2]
    _, counts = np.unique(b24, return_counts=True)
    return max(counts) / max(len(b24), 1)


start = time()
print(primary_color_ratio("test.jpg"))
print(time() - start)
Output:
0.1281328125 0.23400020599365234
It takes twice as long if I replace numpy.unique with pandas.groupby.

6 seconds to process 1 second of film if you are doing it in HD. That's not terrible. You could probably do better if you wrote this in C. You wouldn't have to process the entire frame to decide if it was blank.

That's an interesting idea. Instead of processing every pixel, process one in 100 pixels or 1 in 4 rows or something like that. This uses every 100'th pixel.
from PIL import Image
import numpy as np
from time import time


def primary_color_ratio(filename):
    """Return ratio of pixels that are the "background" color."""
    image = Image.open(filename)
    rgb = np.array(image).reshape(-1, 3)[::100]
    b24 = rgb[:, 0] * 65535 + rgb[:, 1] * 256 + rgb[:, 2]
    _, counts = np.unique(b24, return_counts=True)
    return max(counts) / max(len(b24), 1)


start = time()
print(primary_color_ratio("test.jpg"))
print(time() - start)
Output:
0.1255425347222222 0.05700945854187012
The result is slightly different, but it is also 5x faster. 1.4 seconds to process 1 second of film. The time to read the image and convert to pixels begins to be significant, and further reducing the number of pixels provides smaller and smaller speed gains. For example, processing every 1000'th pixel takes 0.055 seconds.

Because none of the frames depend on other frames, processing multiple frames in parallel would be a simple task.
Reply


Messages In This Thread
identify not white pixels in bmp - by flash77 - Sep-24-2023, 10:07 AM
RE: identify not white pixels in bmp - by DPaul - Sep-24-2023, 12:50 PM
RE: identify not white pixels in bmp - by flash77 - Sep-24-2023, 02:28 PM
RE: identify not white pixels in bmp - by ALIII - Sep-24-2023, 02:51 PM
RE: identify not white pixels in bmp - by flash77 - Oct-04-2023, 04:47 AM
RE: identify not white pixels in bmp - by flash77 - Oct-05-2023, 05:16 PM
RE: identify not white pixels in bmp - by deanhystad - Oct-05-2023, 07:08 PM
RE: identify not white pixels in bmp - by flash77 - Oct-05-2023, 07:52 PM
RE: identify not white pixels in bmp - by flash77 - Oct-07-2023, 04:58 PM
RE: identify not white pixels in bmp - by flash77 - Oct-07-2023, 07:48 PM
RE: identify not white pixels in bmp - by flash77 - Oct-08-2023, 09:21 PM
RE: identify not white pixels in bmp - by flash77 - Oct-09-2023, 06:18 AM
RE: identify not white pixels in bmp - by flash77 - Nov-10-2023, 09:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help to identify CNA for automation qatester 0 133 May-31-2024, 09:24 AM
Last Post: qatester
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,106 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Need to identify only files created today. tester_V 5 4,840 Feb-18-2021, 06:32 AM
Last Post: tester_V
  pillow reversing the order of pixels after every row johnEmScott 4 3,236 May-27-2020, 09:42 AM
Last Post: scidam
  Need to identify sheet color in excel workbook chewy1418 2 2,610 Feb-14-2020, 03:26 PM
Last Post: chewy1418
  Convert 400 grayscale pixels into RGB python420 1 2,526 Jan-02-2020, 04:19 PM
Last Post: Clunk_Head
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 2,010 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  White spaces kdiba 1 2,035 Oct-08-2019, 06:52 PM
Last Post: Aurthor_King_of_the_Brittons
  including the white space parts in str.split() Skaperen 6 3,412 Jun-20-2019, 06:03 PM
Last Post: Skaperen
  replace white space with a string, is this pythonic? Skaperen 1 2,073 Jun-18-2019, 11:36 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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