GTK - Python Getting Difference Between Pixbuf Images

177 Views Asked by At

I need to get the difference between two GTK 3 pixbuf images. Screenshot is captured in order to get images. get_byte_length gives the same value for different images. How could I get the difference?

Here is the code:

    window = Gdk.get_default_root_window()
    x, y, width, height = window.get_geometry()

    pb = Gdk.pixbuf_get_from_window(window, x, y, width, height)
    length1 = pb.get_byte_length()


Python 3.8, GTK 3.
4

There are 4 best solutions below

6
Hardyk Mahendru On

There a performance difference in rendering the Pixbuf through retrieving a Cairo context through gtk.gdk.CairoContext vs converting the Pixbuf into a Cairo. Use this code:

cr.set_source(imgSurface)

cr.rectangle(0, 0, imgSurface.get_width(), imgSurface.get_height())

cr.fill()

It guess it also involves

.set_source_pixbuf() and .get_source().get_surface().

0
Hardyk Mahendru On

The General idea is that

Option 1: Load both images as arrays (scipy.misc.imread) and calculate an element-wise (pixel-by-pixel) difference. Calculate the norm of the difference.

Option 2: Load both images. Calculate some feature vector for each of them (like a histogram). Calculate distance between feature vectors rather than images.

0
Ulises On

You can use glib get_real_time() function to measure processes per second.

get_real_time() gives the result in microseconds(1 million per second)

import gi
from gi.repository import GLib

count = 0
time = GLib.get_real_time()

limit = time + 1000000

while GLib.get_real_time() < limit :
    #take screenshots here
    count += 1

print (count)

This function will count how many times the cpu can capture the screen in one second.

0
codecode1985 On

I am not advanced Gtk or python programmer, but as far as I know, pure python and GTK3 have no direct capabilities for capturing FPS. You may use some ways to achieve that, but finding differences between images by analysing images maybe hard in terms of CPU usage because of the process that sometimes have to be repeated about 60 times or more in a second. You may try getting size of the images and compare them, but maybe CPU usage maybe high for a FPS counter (I am not sure for that). Also you have to find a way to compare the sizes of the images without saving them on disk as you asked in your question. Saving on disk operations also consume CPU sources when repeated very frequently in a second.

Update: This topic may give some information about getting memory size of a python object.