View Single Post
Old 04-10-2020, 07:37 AM   #13
carmenchu
Groupie
carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.carmenchu ought to be getting tired of karma fortunes by now.
 
Posts: 193
Karma: 266070
Join Date: Dec 2010
Location: Spain
Device: Win10,Win11,Ubuntu,PockbookLux44
Many thanks, every body!
Attached is a new version of the plug-in, doing the same, but with:
- an improved (short!) report
- use of 'preferences', per PrefsExampleSimple_v0.0.2--edit them for options.
--------
And now, some grumbling: adding a very simple GUI, with a frame, an editable text field and a couple of buttons, still seems (to me!) a headache.
I sorely miss in Sigil the Gimp (traditional) approach, which looks in code:
Spoiler:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###################################################
# GIMP plugin to export the visible canvas/selection to image as indixed PNG 
#	(NEVER put a multi-layer RGB/GRAY image in INDIXED mode: the results can be AWFUL)
# (c) carmen 2019
#
##### released under GNU General Public License v2
###################################################

import sys,os,os.path

sys.stderr = open('G:/Tests/Gimp_plug/python-fu-output.txt','a')
sys.stdout=sys.stderr # So that they both go to the same file
# From hint in https://www.gimp-forum.net/Thread-Debugging-python-fu-scripts-in-Windows
from gimpfu import *

def export_visible_as_indexed(image,GS,crop,numCols,WWW,ditherType,alphaDither):
	# common tasks
	name,_=os.path.splitext(image.filename) # _ stands for the extension: not wanted
	postFix='_xcf.png'
	# if GS: postfix='-GS' + postfix
	# if crop: postfix='-crop' + postfix
	# theName=name + postfix
	theName=name
	if GS: theName += '-GS'
	if crop: theName += '-crop'
	theName += postFix
	if os.path.exists(theName):
		pdb.gimp_message(theName + ' already exists: Nothing doing!')
		# does away with 'frozen green progress bar' in UI
	else:
		pdb.gimp_edit_copy_visible(image) # action: returns True if successful: Copy from the projection o selection.
		precision = pdb.gimp_image_get_precision(image)
		if precision > 150:
			# pdb.gimp_message('Precision too high: Nothing doing!')
			theImage=pdb.gimp_image_new_with_precision(pdb.gimp_image_width(image),pdb.gimp_image_height(image),pdb.gimp_image_base_type(image),150) # Precision needed for conversion to indexed
			layer=pdb.gimp_layer_new_from_visible(image,theImage,'Pasted layer')
			# layer = pdb.gimp_layer_new_from_visible(image, dest_image, name)
			selection = pdb.gimp_image_get_selection(image)
			theImage.insert_layer(layer,position=-1)
			# pdb.gimp_image_insert_layer(theImage,layer,0,0) #### TypeError: wrong parameter type -> Bug
			# pdb.gimp_image_insert_layer(image, layer, parent, position)
			pdb.plug_in_autocrop_layer(theImage,selection)
		else:
			theImage=pdb.gimp_edit_paste_as_new_image()# (void): Paste buffer to a new image.
			layer=theImage.layers[0]
		# type=pdb.gimp_image_base_type(theImage)
		# print 'Initial base_type is "%s" ' % pdb.gimp_image_base_type(theImage)
		if GS and pdb.gimp_image_base_type(theImage) != 1: pdb.gimp_image_convert_grayscale(theImage)
		# print 'Resulting base_type is "%s" ' % pdb.gimp_image_base_type(theImage)
		if crop: pdb.plug_in_autocrop(theImage,layer)
		paletteType=2 if WWW else 0
		pdb.gimp_image_convert_indexed(theImage,ditherType,paletteType,numCols,alphaDither,0,0)
		# fails if precision > 150
		pdb.file_png_save(theImage,layer,theName,theName,0,9,0,0,0,0,0)
		pdb.gimp_image_delete(theImage)


register(
		"export_visible_as_indexed",
		"Export visible canvas/selection as indexed PNG",
		"Export visible canvas/selection as indexed PNG",
		"carmen",
		"carmen",
		"2019",
		"Export visible as indexed PNG...", 
		"RGB*, GRAY*",
		[
			(PF_IMAGE, 'image', 'Input image', None),
			(PF_BOOL, 'GS', 'Make Grayscale?', False),
			(PF_TOGGLE, 'crop', 'Autocrop image?', False),
			(PF_SPINNER, 'numCols', 'Number of colors:', 256,(1, 256, 1)),
			(PF_BOOL, 'WWW', 'WWW optimized?', False),
			(PF_OPTION, 'ditherType',    'Dither colors:',    0, ['No dithering','Floyd-Steinberg','Floyd-Steinberg + reduced bleeding','Fixed']),
			(PF_BOOL, 'alphaDither', 'Dither transparency?', False)
		],
		[],
		export_visible_as_indexed,
		menu='<Image>/Export As/',
)

main()

(notice the register() at bottom: thatīs the plug-in GUI) and runs as:
Click image for larger version

Name:	Clipboard01.png
Views:	683
Size:	9.1 KB
ID:	178226
the pop-up dialog being created by the PF_... entries, which also gather them as variables for the function in the script.
The main point is that one can focus on the code, and leave the main program to extrude a 'standard' dialog (advanced plug-in coders can also implement their own, non-standard dialogs).
I suspect that one should be able to do something of the kind in Sigil through 'class instances' (above my head!) as
'Seven classes for the seven options under the sky,
one class to bind them all and under Sigil run them...'
For wizards, of course!

Last edited by carmenchu; 04-11-2020 at 03:34 PM.
carmenchu is offline   Reply With Quote