Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wizard advice requested: Struggling to write plug in that lists color data
#2
Here's how I'd do it:
Code:
from gimpfu import *
import pygtk
import gtk
#import sys
#sys.stderr = open('C:/temp/python-fu-output.txt','a')
#sys.stdout=sys.stderr # So that they both go to the same file

def kmullinax(image,drawable):
  palette_name = pdb.gimp_context_get_palette()   # get the name of the currently selected palette
  num_colours, colours = pdb.gimp_palette_get_colors(palette_name)  # get the list of colours to search
  Report = 'Using Palette: "%s" with %d colours\n' % (palette_name, num_colours)
  TotalCount = 0
  colour_num=0
  pdb.gimp_context_set_sample_threshold(0.0)    # only select exact colours
 
  for this_colour in colours: # for each colour in the palette
    colour_name = pdb.gimp_palette_entry_get_name(palette_name, colour_num) # find out what this colour's name is
    pdb.gimp_image_select_color(image, 2, drawable, this_colour)  # select this colour
    if pdb.gimp_selection_is_empty(image) :  # If there isn't any of this colour present, the histogram count returns all of the pixels in the image
      count = 0
    else:
      mead, std_dev, median, pixels, count, percentile = pdb.gimp_drawable_histogram(drawable, 0, 0.0, 1.0) # get the number of pixels of this colour
    TotalCount += count
    Report += "%s, %d\n" % (colour_name, count)
    colour_num += 1
  Report += "Total count=%d\n" % (TotalCount)
  pdb.gimp_message(Report)
 
# menu registration
register(
    "python-fu-kmullinax",
    "Get pixel counts for a Palette",  # Tooltip
    "Get pixel counts for a Palette",
    "me",
    "me",
    "2021.02.01",
    "Get pixel counts for a Palette",  # Menu label
    "*",
    [
      (PF_IMAGE,      "image",       "Input image", None),
      (PF_DRAWABLE,   "drawable", "Input drawable", None),
    ],
    [],
    kmullinax,
    menu="<Image>/kmullinax"
    )

main()

Which makes this output on the error console:
   
Reply


Messages In This Thread
RE: Wizard advice requested: Struggling to write plug in that lists color data - by Kevin - 02-01-2021, 02:42 PM

Forum Jump: