0

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 Answers4

1

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.

0

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().

Hardyk Mahendru
  • 320
  • 3
  • 11
  • It may increase the performance, thanks. But how can I get the difference between two images in terms of bytes, etc.? I need to get screen changes. I get screenshots repeatedly in order to do that. I do not need to show the images on the GUI. – pythonlearner9001 Mar 31 '21 at 18:39
  • if the screenshots are similar the bytes will be the same most of the time. Do you want to detect the changes on them? – Ulises Mar 31 '21 at 20:47
  • @Ulises Martínez Elías, I am not interested in visual differences between the images. I want to detect screen changes in order to measure the frame rate. It is interesting that bytes of the two images are the same even if visuals on the screen changes very much. Do you have any different idea to get the frame rate by using only GTK and python (without any external dependencies)? – pythonlearner9001 Apr 01 '21 at 04:13
  • Do you mean fps? get the count of screenshots captured per second, but i'm not sure if it is a reliable measurement – Ulises Apr 01 '21 at 04:29
  • @Ulises Martínez Elías, what do you suggest to do that by using python or gtk without using external dependencies?? – pythonlearner9001 Apr 01 '21 at 16:17
  • you can use glib library from gi repository. docs: https://lazka.github.io/pgi-docs/#GLib-2.0/classes/Timer.html – Ulises Apr 02 '21 at 03:54
0

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.

Hardyk Mahendru
  • 320
  • 3
  • 11
0

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.

Ulises
  • 539
  • 3
  • 9