RSS/Atom feed Twitter
Site is read-only, email is disabled

Python

This discussion is connected to the gimp-developer-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.

This is a read-only list on gimpusers.com so this discussion thread is read-only, too.

3 of 3 messages available
Toggle history

Please log in to manage your subscriptions.

Python Samuel 29 Sep 12:10
  Python Sven Neumann 29 Sep 12:30
  Python David Gowers 29 Sep 12:42
Samuel
2008-09-29 12:10:00 UTC (about 16 years ago)

Python

I have been at the GIMP IRC channel and they told me that C is faster than Python if I want many pixel ops. I wrote a test plugin in Python which copies a layer pixel per pixel to another one and it needs several minutes for a vga picture. I put it here:
http://pastebin.com/m1e61c74a

Regards Samuel

Hi,

On Mon, 2008-09-29 at 11:35 +0200, Samuel wrote:

I got, what I wanted (pixel ops), but I found out that it's too slow. I'll try to write in C now.

Pixel operations in Python, if done correctly, are not very much slower than doing the same in C.

But please keep this discussion on the mailing-list. I am not going to reply to further mails unless you move this discussion back to the list.

Sven

Sven Neumann
2008-09-29 12:30:29 UTC (about 16 years ago)

Python

Hi,

On Mon, 2008-09-29 at 12:10 +0200, Samuel wrote:

I have been at the GIMP IRC channel and they told me that C is faster than Python if I want many pixel ops. I wrote a test plugin in Python which copies a layer pixel per pixel to another one and it needs several minutes for a vga picture.
I put it here:
http://pastebin.com/m1e61c74a

What you are doing there is quite ineffecient. Your plug-in does not allocate a tile cache. And since you are reading a pixel from one tile, then writing a pixel to another tile, the tiles have to be re-requested from the core over and over again. There are several ways you can improve this:

(1) Use a tile-cache with two tiles: gimp.tile_cache_ntiles(2).

(2) Change your access pattern to iterate over the layer tile-by-tile instead of doing it row-by-row. If you want to keep row-by-row access, then you should increase your tile cache further so that it can hold 2 rows of tiles.

(3) Instead of reading and writing single pixels, you could read and write arrays of pixels. For example rows of GIMP_TILE_WIDTH. This would reduce the overhead you have to do for each pixel.

Sven

David Gowers
2008-09-29 12:42:25 UTC (about 16 years ago)

Python

Hi Samuel,

On Mon, Sep 29, 2008 at 7:40 PM, Samuel wrote:

I have been at the GIMP IRC channel and they told me that C is faster than Python if I want many pixel ops. I wrote a test plugin in Python which copies a layer pixel per pixel to another one and it needs several minutes for a vga picture.

Sven has covered how to access tiles faster. There is also the following approach, which does not
require any knowledge of tiles and is quite simple. If you are copying large areas (eg 1024x1024), a tile based approach may be more appropriate.

# create a pixel region:

pr = sourcelayer.get_pixel_rgn (0, 0, sourcelayer.width, sourcelayer.height, False, False)

# then you read all pixels from that region

pixels = pr[:,:]

# create another region on the destination

pr2 = destlayer.get_pixel_rgn (0, 0, destlayer.width, destlayer.height, True, False)

# and write the stored pixels

pr2[:,:] = pixels

Every pixel has now been copied (on my machine, this takes less than a second for a VGA picture). Note that the above assumes certain similarities between the layers -- ie. they are of the same dimensions,and they either both have an alpha channel or they both lack an alpha channel. It also assumes they are of the same type (as in, both belonging to an RGB image, or both belonging to an Indexed image, or both belonging to a Greyscale image)

HTH, David