Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get list of distinct pixel RGB values
#2
I would like to see you code, because even though the colorxhtml script isn't that optimized, it cannot be that bad.

This code:
Code:
image=gimp.image_list()[0]
layer=image.active_layer
region=layer.get_pixel_rgn(0, 0, 400,400)

from collections import defaultdict
colors=defaultdict(int)
for x in range(400):
   for y in range(400):
       colors[region[x,y]]+=1
len(colors)

runs in 5 seconds, and report the same number of colors as the color cube analysis.

   

It runs even faster (a couple of seconds) with an image reduced to 256 colors...

The colors you obtain are a 3-byte string such as
Code:
> region[0,0]
'x\x89|'

which means :
  • Red is 0x78 = 120 ("x"in ASCII)
  • Green is 0x89 = 137
  • Blue is 0x7C = 124 ("|"in ASCII)
   

(to have a representative number of colors, my test layer was a gray (0x80) to which I added some low RGB noise).

You can of course write into the pixel region and return it as a new layer.

If you want really fast processing you can use numpy so iterations are done by C code) but of course it isn't part of your regular Python runtime and adding it to the Gimp python runtime on Windows may be an ordeal for your prospective users.
Reply


Messages In This Thread
RE: Get list of distinct pixel RGB values - by Ofnuts - 03-17-2023, 01:33 PM

Forum Jump: