Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from gimpfu import *
  4. import tempfile, subprocess, os
  5.  
  6. def plugin_main(timg, tdrawable, amount):
  7.     sfx = '.png'
  8.     pfx = 'gimp_convert_to_paletted_plugin'
  9.     tmpifn = tempfile.mktemp(sfx, pfx)
  10.     tmpofn = tempfile.mktemp(sfx, pfx)
  11.     tmppfn = tempfile.mktemp(sfx, pfx)
  12.    
  13.     pal = pdb.gimp_context_get_palette()
  14.     num_colors, colors = pdb.gimp_palette_get_colors(pal)
  15.    
  16.     tempimage = pdb.gimp_image_duplicate(timg)
  17.     tempdrawable = pdb.gimp_layer_new_from_visible(tempimage, tempimage, "visible")
  18.     pdb.gimp_file_save(tempimage, tempdrawable, tmpifn, "")
  19.     pdb.gimp_image_delete(tempimage)
  20.    
  21.     tempimage = pdb.gimp_image_new(num_colors, 1, 0)
  22.     tempdrawable = pdb.gimp_layer_new(tempimage, num_colors, 1, 1, "pal", 100, 0)
  23.     for x,c in zip(xrange(num_colors), colors):
  24.         pdb.gimp_drawable_set_pixel(tempdrawable, x, 0, 4, c)
  25.     pdb.gimp_file_save(tempimage, tempdrawable, tmppfn, "")
  26.     pdb.gimp_image_delete(tempimage)
  27.  
  28.     cmd = 'convert %s -dither floyd-steinberg -define dither:diffusion-amount=%i%% -remap %s %s' % \
  29.           (tmpifn, amount, tmppfn, tmpofn)
  30.    
  31.     p = subprocess.Popen(cmd.split(), shell=False)
  32.     p.communicate()
  33.    
  34.     layer = pdb.gimp_file_load_layer(timg, tmpofn)
  35.     pdb.gimp_image_insert_layer(timg, layer, None, -1)
  36.    
  37.     for fn in [tmpifn, tmpofn, tmppfn]:
  38.         os.remove(fn)
  39.  
  40. register(
  41.         "python_fu_convert_to_paletted",
  42.         "Convert to paletted",
  43.         "Convert to paletted",
  44.         "Anonymous",
  45.         "Anonymous",
  46.         "2019",
  47.         "<Image>/Image/Convert to paletted...",
  48.         "RGB*",
  49.         [(PF_SLIDER, "diffusion_amount",  "Diffusion Amount, %:", 50, [0, 100, 1])],
  50.         [],
  51.         plugin_main)
  52.  
  53. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement