[Tutor] Speeding up my Python program

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon Feb 7 21:31:36 EST 2022


On Mon, 7 Feb 2022 23:30:39 +0000, Alan Gauld via Tutor <tutor at python.org>
declaimed the following:

>On 07/02/2022 19:53, aj0358 at web.de wrote:
>>    I am looking for a way to speed up my python program. 

>For example you are grabbing the whole screen, it might be better to
>only grab the area you need. That will reduce the work Pillow has
>to do processing it.
>

	Which may imply passing a "bounding box" to the .grab() call.

>In particular note the warning on the PixelAccess class documentation page:
>
>"""
>Accessing individual pixels is fairly slow. If you are looping over all
>of the pixels in an image, there is likely a faster way using other
>parts of the Pillow API.
>"""
>
	Well, the OP looks to only be grabbing three specific pixels, not
looping, but...

"""
Image.getpixel(xy)
    Returns the pixel value at a given position.

    Parameters
        xy – The coordinate, given as (x, y). See Coordinate System.

    Returns
        The pixel value. If the image is a multi-layer image, this method
returns a tuple.
"""


>Also, do you need to call load() explicitly? If not it may be that
>Pillow is calling it internally, thus doubling the work done.
>
And... the first thing the source shows for that (.getpixel()) call is: 
		self.load()
so doing an explicit .load() first is likely futile. Especially when one
looks at
"""
PIL.ImageGrab.grab(bbox=None, include_layered_windows=False,
all_screens=False, xdisplay=None)[source]¶

<SNIP>
    Parameters
            bbox – What region to copy. Default is the entire screen. Note
that on Windows OS, the top-left point may be negative if all_screens=True
is used.
<SNIP>
    Returns
        An image
"""
in conjunction with
"""
 Image.load()¶

    Allocates storage for the image and loads the pixel data. In normal
cases, you don’t need to call this method, since the Image class
automatically loads an opened image when it is accessed for the first time.
"""
(under file handling)
"""
Any Pillow method that creates a new image instance based on another will
internally call load() on the original image and then read the data. The
new image instance will not be associated with the original image file.
"""

>
>>        start_time=time.time()
>>        rgb1 = ImageGrab.grab().load()[1185,561]
>>        rgb2 = ImageGrab.grab().load()[1260,568]
>>        rgb3 = ImageGrab.grab().load()[1331,571]

	I'd suggest the first optimization would be to only grab the screen
ONCE. Then access the three pixels from the single grabbed screen.

	Something like:

		scn = ImageGrab.grab()
		rgb1 = scn.getpixel((1185, 561))
		rgb2 = scn...

>>        print(rgb1)
>>        print(rgb2)
>>        print(rgb3)
>>        end_time=time.time()-start_time

	Technically, "end_time" is what time.time() returned. "duration" is
what is being computed here.

>>        print(end_time)
>>     
>>    The goal is to analyse every frames of a video (60 fps) and to get the
>>    pixel colors of three pixels. Unfortunately, at the moment, the programm
>>    only analyses one frame every (about) 0.5 seconds. 
>
>You may be asking more of Pillow than it is capable of, it isn't really
>designed for real-time video processing so far as I'm aware. There are
>some video libraries around that may do this better, eg. cgkit might do
>what you want.
>

	I'm pretty certain it isn't designed for real-time video processing...
Even dedicated NLEs (like the old Vegas product) don't promise realtime. As
I recall, on my computer it /previews/ video at around 20fps, and depending
upon operations being performed, can render at less than 1fps (many years
ago I was applying a trapezoidal correction to part of a video* -- a 5-10
minute video took something like 10-15 hours to process)



* the variety show at a FurCon -- my camera was set up with a fairly good
view of the stage, but they had a few segments that were shown on large
monitors to left and right; those monitors were angled such that I was
recording them as 
/____\
shaped.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list