Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from gimpfu import *
- import tempfile, subprocess, os
- def plugin_main(timg, tdrawable, amount):
- sfx = '.png'
- pfx = 'gimp_convert_to_paletted_plugin'
- tmpifn = tempfile.mktemp(sfx, pfx)
- tmpofn = tempfile.mktemp(sfx, pfx)
- tmppfn = tempfile.mktemp(sfx, pfx)
- pal = pdb.gimp_context_get_palette()
- num_colors, colors = pdb.gimp_palette_get_colors(pal)
- tempimage = pdb.gimp_image_duplicate(timg)
- tempdrawable = pdb.gimp_layer_new_from_visible(tempimage, tempimage, "visible")
- pdb.gimp_file_save(tempimage, tempdrawable, tmpifn, "")
- pdb.gimp_image_delete(tempimage)
- tempimage = pdb.gimp_image_new(num_colors, 1, 0)
- tempdrawable = pdb.gimp_layer_new(tempimage, num_colors, 1, 1, "pal", 100, 0)
- for x,c in zip(xrange(num_colors), colors):
- pdb.gimp_drawable_set_pixel(tempdrawable, x, 0, 4, c)
- pdb.gimp_file_save(tempimage, tempdrawable, tmppfn, "")
- pdb.gimp_image_delete(tempimage)
- cmd = 'convert %s -dither floyd-steinberg -define dither:diffusion-amount=%i%% -remap %s %s' % \
- (tmpifn, amount, tmppfn, tmpofn)
- p = subprocess.Popen(cmd.split(), shell=False)
- p.communicate()
- layer = pdb.gimp_file_load_layer(timg, tmpofn)
- pdb.gimp_image_insert_layer(timg, layer, None, -1)
- for fn in [tmpifn, tmpofn, tmppfn]:
- os.remove(fn)
- register(
- "python_fu_convert_to_paletted",
- "Convert to paletted",
- "Convert to paletted",
- "Anonymous",
- "Anonymous",
- "2019",
- "<Image>/Image/Convert to paletted...",
- "RGB*",
- [(PF_SLIDER, "diffusion_amount", "Diffusion Amount, %:", 50, [0, 100, 1])],
- [],
- plugin_main)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement