def __init__(self, *args): #message, image = args gimp.message(args[0]) dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE, message_format=args[0]) dlg.set_position(gtk.WIN_POS_CENTER) dlg.run() dlg.destroy() # house keeping try: gimp.pdb.gimp_context_pop() except RuntimeError: pass # no previous context push try: image = gimp.image_list()[0] gimp.pdb.gimp_image_undo_group_end(image) except RuntimeError: pass # no previous group start try: gimp.pdb.gimp_progress_end() except RuntimeError: pass # NOTE: this only in case, as unlike context & group, this seemingly doesn't get thrown if progress init wasn't called first gimp.pdb.gimp_displays_flush()
def _display_message_on_setting_value_error(exc_type, exc_value, exc_traceback): if issubclass(exc_type, setting.SettingValueError): gimp.message(str(exc_value).encode(GIMP_CHARACTER_ENCODING)) return True else: return False
def write_xdpi(img, layer, res_folder, image_basename, target_width, target_dpi, image_extension): ''' Resize and write images for all android density folders @param img: gimp image @param layer: gimp layer (or drawable) @param res_folder: output directory : basically res folder of your android project @param image_basename: basename of your image, ex: icon @param target_width: new width for your image @param target_dpi: reference density for your target width @param image_extension: output format ''' warnings = list() # reference density requested by the user target_density_ratio = dict(dpi_ratios).get(target_dpi) gimpfu.pdb.gimp_edit_copy_visible(img); #@UndefinedVariable for dpi_ratio in dpi_ratios: new_img = gimpfu.pdb.gimp_edit_paste_as_new(); #@UndefinedVariable # resize requested by the user resize_ratio = float(target_width) / float(new_img.width) target_res_folder = os.path.join(res_folder, dpi_ratio[0]) if (os.path.exists(res_folder) and not os.path.exists(target_res_folder)): os.makedirs(target_res_folder) target_res_filename = os.path.join(target_res_folder, image_basename+'.'+image_extension) # Compute new dimensions for the image density_ratio = dpi_ratio[1] new_width = round(float(new_img.width) / target_density_ratio * density_ratio * resize_ratio) new_height = round(float(new_img.height) / target_density_ratio * density_ratio * resize_ratio) print('%s : %f, %f, %f' % (dpi_ratio[0], target_density_ratio, density_ratio, resize_ratio)) if (new_width>new_img.width): warnings.append('Resource for %s has been upscaled by %0.2f' % (dpi_ratio[0], new_width/new_img.width)) # Save the new Image gimpfu.pdb.gimp_image_scale_full( #@UndefinedVariable new_img, new_width, new_height, gimpfu.INTERPOLATION_CUBIC) gimpfu.pdb.gimp_file_save( #@UndefinedVariable new_img, new_img.layers[0], target_res_filename, target_res_filename) gimpfu.pdb.gimp_image_delete(new_img) #@UndefinedVariable # Show warning message if warnings: warnings.append(UPSCALE_WARN_MESSAGE) gimp.message('\n'.join(warnings))
def copy(image, layer): """Create initial layer; it contains the original layer, plus infinitely smaller copies of that image.""" if gimpfu.pdb.gimp_selection_is_empty(image): gimp.message("Selection must not be empty") raise "Selection must not be empty" w, h = layer.width, layer.height x1, y1, x2, y2 = layer.mask_bounds gimpfu.pdb.gimp_selection_none(image) # Trim selection to match aspect ratio if (x2 - x1) / (y2 - y1) > (w / h): # x is too long, adjust for new x values newx = (w / h) * (y2 - y1) middle = x1 + ((x2 - x1) / 2) x1 = int(middle - newx / 2) x2 = int(middle + newx / 2) elif (x2 - x1) / (y2 - y1) < (w / h): # y is too long, adjust for new y values newy = (x2 - x1) / (w / h) middle = y1 + ((y2 - y1) / 2) y1 = int(middle - newy / 2) y2 = int(middle + newy / 2) cset = copier(w, h, x1, y1, x2, y2) for topleft, bottomright in cset: newlayer = layer.copy() image.add_layer(newlayer, 1) # I have no idea why this is transforming layer instead of newlayer layer.transform_scale(topleft[0], topleft[1], bottomright[0], bottomright[1], gimpfu.TRANSFORM_FORWARD, gimpfu.INTERPOLATION_CUBIC, False, # supersampling? 3, # level of recursion gimpfu.TRANSFORM_RESIZE_ADJUST) image.merge_visible_layers(gimpfu.CLIP_TO_IMAGE) return cset
def fail(msg): """Display an error message and quit""" gimp.message(msg) raise error, msg
def flush(self): gimp.message(self._message_buffer.encode(pgconstants.GIMP_CHARACTER_ENCODING)) self._message_buffer = self._message_prefix
def write_xdpi(img, layer, res_folder, image_basename, target, target_width_height, x_ldpi, x_mdpi, x_hdpi, image_extension): ''' Resize and write images for all android density folders @param img: gimp image @param layer: gimp layer (or drawable) @param res_folder: output directory : basically res folder of your android project @param image_basename: basename of your image, ex: icon @param target: new width for your image @param target_width_height: fixed misures @param target_dpi: reference density for your target width @param image_extension: output format ''' warnings = list() gimpfu.pdb.gimp_edit_copy_visible(img); #@UndefinedVariable dpi_ratios = (('', 1 ,x_ldpi), ('@2x', 2 ,x_mdpi), ('@3x', 3 ,x_hdpi)) for folder, ratio, export in dpi_ratios: if not export: continue new_img = gimpfu.pdb.gimp_edit_paste_as_new(); #@UndefinedVariable target_dp_width = 0 target_dp_height = 0 if target_width_height == "width": resize_ratio = float(target) / new_img.width target_dp_width = target target_dp_height = round(new_img.height * resize_ratio) if target_width_height == "height": resize_ratio = float(target) / new_img.height target_dp_width = round(new_img.width * resize_ratio) target_dp_height = target # Compute new dimensions for the image new_width = target_dp_width * ratio new_height = target_dp_height * ratio target_res_folder = os.path.join(res_folder, 'icon-' + image_basename) if (os.path.exists(res_folder) and not os.path.exists(target_res_folder)): os.makedirs(target_res_folder) target_res_filename = os.path.join(target_res_folder, image_basename + folder + '.' + image_extension) # Save the new Image gimpfu.pdb.gimp_image_scale_full( #@UndefinedVariable new_img, new_width, new_height, gimpfu.INTERPOLATION_CUBIC) gimpfu.pdb.gimp_file_save( #@UndefinedVariable new_img, new_img.layers[0], target_res_filename, target_res_filename) gimpfu.pdb.gimp_image_delete(new_img) #@UndefinedVariable # Show warning message if warnings: warnings.append(UPSCALE_WARN_MESSAGE) gimp.message('\n'.join(warnings))
def python_message(image, drawable, message): gimp.message(message)
def _display_message_on_setting_value_error(exc_type, exc_value, exc_traceback): if issubclass(exc_type, settings_.SettingValueError): gimp.message(str(exc_value).encode(pgconstants.GIMP_CHARACTER_ENCODING)) return True else: return False
def warning_normal(text): currenth = pdb.gimp_message_get_handler() pdb.gimp_message_set_handler(0) # box gimp.message("Warning: " + text) print("Warning: " + text) pdb.gimp_message_set_handler(currenth) # current
def error_box(text): currenth = pdb.gimp_message_get_handler() pdb.gimp_message_set_handler(2) # box gimp.message("ERROR: " + text) print("ERROR: " + text) pdb.gimp_message_set_handler(currenth) # current
def flush(self): gimp.message(self._message_buffer.encode()) self._message_buffer = self._message_prefix
def _write(self, data): if data.strip(): gimp.message(str(data).encode())
def write_xdpi(img, layer, res_folder, folder_prefix, image_basename, target_width, x_ldpi, x_mdpi, x_hdpi, x_xhdpi, x_xxhdpi, x_xxxhdpi, allow_upscale, image_extension): ''' Resize and write images for all android density folders @param img: gimp image @param layer: gimp layer (or drawable) @param res_folder: output directory : basically res folder of your android project @param folder_prefix: android mipmap or drawable folder @param image_basename: basename of your image, ex: icon @param target_width: new width for your image @param target_dpi: reference density for your target width @param allow_upscale: whether to create upscaled images @param image_extension: output format ''' warnings = list() gimpfu.pdb.gimp_edit_copy_visible(img); #@UndefinedVariable dpi_ratios = (('ldpi', 0.75 ,x_ldpi), ('mdpi', 1 ,x_mdpi), ('tvdpi', 1.33 ,False), ('hdpi', 1.5 ,x_hdpi), ('xhdpi', 2 ,x_xhdpi), ('xxhdpi', 3 ,x_xxhdpi), ('xxxhdpi', 4 ,x_xxxhdpi)) for folder, ratio, export in dpi_ratios: if not export: continue new_img = gimpfu.pdb.gimp_edit_paste_as_new(); #@UndefinedVariable resize_ratio = float(target_width) / new_img.width target_dp_width = target_width target_dp_height = round(new_img.height * resize_ratio) # Compute new dimensions for the image new_width = target_dp_width * ratio new_height = target_dp_height * ratio print('%s : %dx%d' % (folder, new_width, new_height)) if (new_width>new_img.width): if not allow_upscale: warnings.append('Not creating resource for %s upscaled by %0.2f' % (folder, new_width/new_img.width)) continue else: warnings.append('Resource for %s has been upscaled by %0.2f' % (folder, new_width/new_img.width)) target_res_folder = os.path.join(res_folder, folder_prefix + '-' + folder) if (os.path.exists(res_folder) and not os.path.exists(target_res_folder)): os.makedirs(target_res_folder) target_res_filename = os.path.join(target_res_folder, image_basename + '.' + image_extension) # Save the new Image gimpfu.pdb.gimp_image_scale_full( #@UndefinedVariable new_img, new_width, new_height, gimpfu.INTERPOLATION_CUBIC) gimpfu.pdb.gimp_file_save( #@UndefinedVariable new_img, new_img.layers[0], target_res_filename, target_res_filename) gimpfu.pdb.gimp_image_delete(new_img) #@UndefinedVariable # Show warning message if warnings: warnings.append(UPSCALE_WARN_MESSAGE) gimp.message('\n'.join(warnings))
def _write_with_prefix(self, data): if data.strip(): gimp.message(self.message_prefix + str(data).encode())
def write_xdpi(img, layer, res_folder, image_basename, target_width, x_ldpi, x_mdpi, x_hdpi, x_xhdpi, x_xxhdpi, x_xxxhdpi, image_extension): ''' Resize and write images for all android density folders @param img: gimp image @param layer: gimp layer (or drawable) @param res_folder: output directory : basically res folder of your android project @param image_basename: basename of your image, ex: icon @param target_width: new width for your image @param target_dpi: reference density for your target width @param image_extension: output format ''' warnings = list() gimpfu.pdb.gimp_edit_copy_visible(img); #@UndefinedVariable dpi_ratios = (('drawable-ldpi', 0.75 ,x_ldpi), ('drawable-mdpi', 1 ,x_mdpi), ('drawable-tvdpi', 1.33 ,False), ('drawable-hdpi', 1.5 ,x_hdpi), ('drawable-xhdpi', 2 ,x_xhdpi), ('drawable-xxhdpi', 3 ,x_xxhdpi), ('drawable-xxxhdpi', 4 ,x_xxxhdpi)) for folder, ratio, export in dpi_ratios: if not export: continue new_img = gimpfu.pdb.gimp_edit_paste_as_new(); #@UndefinedVariable resize_ratio = float(target_width) / new_img.width target_dp_width = target_width target_dp_height = round(new_img.height * resize_ratio) target_res_folder = os.path.join(res_folder, folder) if (os.path.exists(res_folder) and not os.path.exists(target_res_folder)): os.makedirs(target_res_folder) target_res_filename = os.path.join(target_res_folder, image_basename+'.'+image_extension) # Compute new dimensions for the image new_width = target_dp_width * ratio new_height = target_dp_height * ratio print('%s : %dx%d' % (folder, new_width, new_height)) if (new_width>new_img.width): warnings.append('Resource for %s has been upscaled by %0.2f' % (folder, new_width/new_img.width)) # Save the new Image gimpfu.pdb.gimp_image_scale_full( #@UndefinedVariable new_img, new_width, new_height, gimpfu.INTERPOLATION_CUBIC) gimpfu.pdb.gimp_file_save( #@UndefinedVariable new_img, new_img.layers[0], target_res_filename, target_res_filename) gimpfu.pdb.gimp_image_delete(new_img) #@UndefinedVariable # Show warning message if warnings: warnings.append(UPSCALE_WARN_MESSAGE) gimp.message('\n'.join(warnings))
def arrows_creator(image, layer) : """ This is the procedure that is registered with GIMP """ global AC_argmenu, GIMP_version # GIMP version from the PDB, but tuple as from pygimp pdb_version = pdb.gimp_version().split('.') # here version is a string pdb_version = tuple([int(item) for item in pdb_version]) if pdb_version != GIMP_version and (pdb_version < (2, 8, 14) or pdb_version > (2, 11)) : gimp.message(_("WARNING: your PDB version is '%s', so different from the pygimp one '%s'")\ % (str(pdb_version), str(GIMP_version)) + _(" and a miss-match!")) # officially GIMP_version = pdb_version ## next was for the case of a size change by explanation of the label? #wdth = image.width #hght = image.height # with gimpshelf avoid duplicate launch (comment out 'shelf' for dev.) if shelf.has_key(whoiamName) and shelf[whoiamName] : gimp.message(_("ERROR: an 'arrows creator' instance is already running!")) return ##1) Preparations # ******************************************** shelf[whoiamName] = True # the context antialias is set by default in GIMP! # instability of GIMP-2.6 core with 'image.undo_group' here but work in 2.8 image.disable_undo() # initial paths? vectors_new = [] init_paths = image.vectors if init_paths : for p in init_paths : # if "AC_vectors" there keep it visible if p.name == _("AC_vectors") : p.visible = True pdb.gimp_image_lower_item_to_bottom(image, p) pdb.gimp_image_set_active_vectors(image, p) else : p.visible = False # configure pattern for the head previous_pattern = pdb.gimp_context_get_pattern() pdb.gimp_context_set_pattern(pdb.gimp_patterns_get_list('')[1][AC_argmenu[4]]) # set brush for the shaft explanation of the label previous_brush = pdb.gimp_context_get_brush() pdb.gimp_context_set_brush("2. Hardness 100") pdb.gimp_context_set_dynamics("Dynamics Off") ##2) Main event: GUI in 'ArrowsModule.py' # ******************************************** objct = ArrowWindow(image, AC_argmenu[3], AC_argmenu[4], GIMP_version) gtk.main() ##3) Closing # ******************************************** # restore some context pdb.gimp_context_set_brush(previous_brush) pdb.gimp_context_set_pattern(previous_pattern) if image in gimp.image_list() : # cleanup buffer path if len(image.vectors)>2 : buf_name = pdb.gimp_item_get_name(image.vectors[1]) if buf_name == _("AC buffer") : pdb.gimp_image_remove_vectors(image, vectors_new) Lgroup = pdb.gimp_image_get_layer_by_name(image, _("AC_group")) if Lgroup : image.active_layer = Lgroup # quitting go here also layer = pdb.gimp_image_get_layer_by_name(image, _("AC_element")) if objct.arrow_done and layer : position = pdb.gimp_image_get_item_position(image, layer) if position == 0 : image.merge_down(layer, 1) elif not (objct.arrow_done) and not (objct.layer_miss) : # cleanup layer, if close before the arrow is drawn image.remove_layer(Lgroup.children[0]) # remove empty AC group layer empty_group = Lgroup and Lgroup.children == [] # empty group if empty_group : image.remove_layer(Lgroup) image.enable_undo() # permitting the user to keep tab on measuring arrow(s), not if last before quitting if objct.measurements : cur_name = image.name mess_txt = _("MEASURING ARROW in %s: \n\n Nr Size") % cur_name\ + _(" Direction (clockwise +)\n") for arrow in objct.measurements : mess_txt += " %s %.1f px \t %.1f°\n" % (str(arrow[0]) + '.' +\ str(arrow[1]), arrow[2], arrow[3]) gimp.message(mess_txt) shelf[whoiamName] = False
def fail(msg): '''Display and error message and quit''' gimp.message(msg) raise error, msg