def synth_image(a, tile_horiz=False, tile_vert=False, show=False): a_width = max(len(row) for row in a) a_height = len(a) i = gimp.Image(a_width * w, a_height * w) l = gimp.Layer(i, "Base", i.width, i.height) i.add_layer(l) mi = gimp.Image(i.width, i.height, gimpenums.GRAY) m = gimp.Layer(mi, "Mask", mi.width, mi.height, gimpenums.GRAY_IMAGE) mi.add_layer(m) # Copy up tiles that we have, and mask for y, row in enumerate(a): for x, tile in enumerate(row): # Mask t = tiles[tile] copy_tile(output_map, t[0], t[1], m, x, y) # Image if tile in result_dict: t = result_dict[tile] copy_tile(t[0], t[1], t[2], l, x, y) # Adjust selection to tiles we don't have # Also adjust selection in result, just # so users know what tiles are being generated pdb.gimp_selection_none(i) pdb.gimp_selection_none(result) for y, row in enumerate(a): for x, tile in enumerate(row): if tile in result_dict: continue if tile is EMPTY_TILE: continue pdb.gimp_image_select_rectangle(i, gimpenums.CHANNEL_OP_ADD, x * w, y * w, w, w) t = tiles[tile] pdb.gimp_image_select_rectangle(result, gimpenums.CHANNEL_OP_ADD, t[0] * w, t[1] * w, w, w) # Do the actual resynthesize call if True: pdb.plug_in_resynthesizer(i, l, int(tile_vert), int(tile_horiz), 1, source.ID, source_map.ID, m.ID, map_weight, autism, neighbourhood, trys) # Copy to result image for y, row in enumerate(a): for x, tile in enumerate(row): if tile in result_dict: continue t = tiles[tile] copy_tile(l, x, y, result_layer, t[0], t[1]) result_dict[tile] = (result_layer, t[0], t[1]) if show: # For debugging gimp.Display(i) gimp.Display(mi) else: pdb.gimp_image_delete(i) pdb.gimp_image_delete(mi)
def load_layers(layer_filenames, image=None, strip_file_extension=False): """ Load multiple layers to one image. Return the image. If `image` is None, create a new image. If `image` is not None, load the layers to the specified image. If `strip_file_extension` is True, remove the file extension from the names of the loaded layers (GIMP automatically names the layers by their basenames). """ create_new_image = image is None if create_new_image: image = gimp.Image(1, 1) for filename in layer_filenames: layer = pdb.gimp_file_load_layer(image, filename) pdb.gimp_image_insert_layer(image, layer, None, len(image.layers)) if strip_file_extension: layer.name = os.path.splitext(layer.name)[0] if create_new_image: pdb.gimp_image_resize_to_layers(image) return image
def draw_brush_example(brush_name): """ draws a nice single brush stroke, and a random brush stroke to show the brush in use. Returns an Image""" gimp.pdb.gimp_context_set_brush(brush_name) brush_info = gimp.pdb.gimp_brush_get_info(brush_name) print brush_info pdb.gimp_context_set_opacity(100) w, h, mbpp, cbpp = brush_info # a little leeway so we can make a stroke image_w = (w * 5) + 10 image_h = h * 2 img = gimp.Image(image_w, image_h, RGB) img.disable_undo() brush_layer = gimp.Layer(img, brush_name, image_w, image_h, RGBA_IMAGE, 100, NORMAL_MODE) img.add_layer(brush_layer, 0) # should make this right/black/configurable? pdb.gimp_edit_fill(brush_layer, BACKGROUND_FILL) # generate and paint a single brush stroke, and a random walk brush stroke stroke_list, strokes = make_strokes(image_w, image_h, w, h) pdb.gimp_paintbrush_default(brush_layer, strokes, stroke_list) stroke_list, strokes = make_single_stroke(w, h, w, h) pdb.gimp_paintbrush_default(brush_layer, strokes, stroke_list) img.enable_undo() return img, brush_layer, brush_name
def load_layers(filepaths, image=None, strip_file_extension=False): """ Load multiple layers to one image. Return the image. The layers are loaded at the end of the image. If `image` is `None`, create a new image. If `image` is not `None`, load the layers to the specified image. Layers names are basenames of the corresponding files. If `strip_file_extension` is `True`, remove the file extension from layer names. """ create_new_image = image is None if create_new_image: image = gimp.Image(1, 1) for filepath in filepaths: load_layer(filepath, image, strip_file_extension) if create_new_image: pdb.gimp_image_resize_to_layers(image) return image
def create_gui_textures(guis): IMAGE_SIZE = 256 ITEM_SLOT_SIZE = HOTBAR_HEIGHT = 18 ITEM_SLOT_BORDER_SIZE = 1 MAIN_INVENTORY_HEIGHT = 3 * ITEM_SLOT_SIZE HOTBAR_MARGIN_TOP = 4 GUI_BORDER_SIZE = 7 MARGIN_BORDER_TOP = 10 GUI_WIDTH = 176 COLORS = [ [0, 0, 0, 255], [55, 55, 55, 255], [85, 85, 85, 255], [139, 139, 139, 255], [198, 198, 198, 255], [255, 255, 255, 255] ] for gui_name, GUI_CUSTOM_SECTION_HEIGHT in guis.iteritems(): xcf_path = working_dir + "/util/gui-xcf/" + gui_name + ".xcf" png_path = assets_path + "/textures/gui/container/" + gui_name + ".png" if not (os.path.exists(xcf_path) or os.path.exists(png_path)): GUI_HEIGHT = 2 * GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT + HOTBAR_MARGIN_TOP + HOTBAR_HEIGHT image = gimp.Image(IMAGE_SIZE, IMAGE_SIZE, RGB) image.disable_undo() layers = { "background": gimp.Layer(image, "background", IMAGE_SIZE, IMAGE_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "edge-top-left": gimp.Layer(image, "top left edge", GUI_BORDER_SIZE, GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "edge-top-right": gimp.Layer(image, "top right edge", GUI_BORDER_SIZE, GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "edge-bottom-left": gimp.Layer(image, "bottom left edge", GUI_BORDER_SIZE, GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "edge-bottom-right": gimp.Layer(image, "bottom right edge", GUI_BORDER_SIZE, GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "border-top": gimp.Layer(image, "border top", GUI_WIDTH - 2 * GUI_BORDER_SIZE, GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "border-left": gimp.Layer(image, "border left", GUI_BORDER_SIZE, GUI_HEIGHT - 2 * GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "border-bottom": gimp.Layer(image, "border bottom", GUI_WIDTH - 2 * GUI_BORDER_SIZE, GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "border-right": gimp.Layer(image, "border right", GUI_BORDER_SIZE, GUI_HEIGHT - 2 * GUI_BORDER_SIZE, RGB_IMAGE, 100, NORMAL_MODE), "margin-border-top": gimp.Layer(image, "margin border top", GUI_WIDTH - 2 * GUI_BORDER_SIZE, MARGIN_BORDER_TOP, RGB_IMAGE, 100, NORMAL_MODE), "inner-gui": gimp.Layer(image, "inner GUI", GUI_WIDTH - 2 * GUI_BORDER_SIZE, GUI_CUSTOM_SECTION_HEIGHT, RGB_IMAGE, 100, NORMAL_MODE), "inventory": gimp.Layer(image, "inventory", GUI_WIDTH - 2 * GUI_BORDER_SIZE, MAIN_INVENTORY_HEIGHT, RGB_IMAGE, 100, NORMAL_MODE), "margin-hotbar-top": gimp.Layer(image, "margin hotbar top", GUI_WIDTH - 2 * GUI_BORDER_SIZE, HOTBAR_MARGIN_TOP, RGB_IMAGE, 100, NORMAL_MODE), "hotbar": gimp.Layer(image, "hotbar", GUI_WIDTH - 2 * GUI_BORDER_SIZE, HOTBAR_HEIGHT, RGB_IMAGE, 100, NORMAL_MODE) } for layer in layers.itervalues(): gimp.pdb["gimp-layer-add-alpha"](layer) gimp.pdb["gimp-image-insert-layer"](image, layer, None, 0) drawable = gimp.pdb["gimp-image-active-drawable"](image) gimp.pdb["gimp-selection-all"](image) gimp.pdb["gimp-edit-cut"](drawable) gimp.pdb["gimp-selection-none"](image) gimp.pdb["gimp-layer-set-offsets"](layers["edge-top-right"], GUI_WIDTH - GUI_BORDER_SIZE, 0) gimp.pdb["gimp-layer-set-offsets"](layers["edge-bottom-left"], 0, GUI_HEIGHT - GUI_BORDER_SIZE) gimp.pdb["gimp-layer-set-offsets"](layers["edge-bottom-right"], GUI_WIDTH - GUI_BORDER_SIZE, GUI_HEIGHT - GUI_BORDER_SIZE) gimp.pdb["gimp-layer-set-offsets"](layers["margin-border-top"], GUI_BORDER_SIZE, GUI_BORDER_SIZE) gimp.pdb["gimp-layer-set-offsets"](layers["border-top"], GUI_BORDER_SIZE, 0) gimp.pdb["gimp-layer-set-offsets"](layers["border-bottom"], GUI_BORDER_SIZE, GUI_HEIGHT - GUI_BORDER_SIZE) gimp.pdb["gimp-layer-set-offsets"](layers["border-left"], 0, GUI_BORDER_SIZE) gimp.pdb["gimp-layer-set-offsets"](layers["border-right"], GUI_WIDTH - GUI_BORDER_SIZE, GUI_BORDER_SIZE) gimp.pdb["gimp-layer-set-offsets"](layers["inner-gui"], GUI_BORDER_SIZE, GUI_BORDER_SIZE + MARGIN_BORDER_TOP) gimp.pdb["gimp-layer-set-offsets"](layers["inventory"], GUI_BORDER_SIZE, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT) gimp.pdb["gimp-layer-set-offsets"](layers["margin-hotbar-top"], GUI_BORDER_SIZE, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT) gimp.pdb["gimp-layer-set-offsets"](layers["hotbar"], GUI_BORDER_SIZE, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT + HOTBAR_MARGIN_TOP) gimp.set_foreground(tuple(COLORS[4])) gimp.pdb["gimp-edit-fill"](layers["margin-border-top"], FOREGROUND_FILL) gimp.pdb["gimp-edit-fill"](layers["inner-gui"], FOREGROUND_FILL) gimp.pdb["gimp-edit-fill"](layers["margin-hotbar-top"], FOREGROUND_FILL) gimp.pdb["gimp-image-add-vguide"](image, 0) gimp.pdb["gimp-image-add-vguide"](image, GUI_BORDER_SIZE) gimp.pdb["gimp-image-add-vguide"](image, GUI_WIDTH - GUI_BORDER_SIZE) gimp.pdb["gimp-image-add-vguide"](image, GUI_WIDTH) gimp.pdb["gimp-image-add-hguide"](image, GUI_BORDER_SIZE) gimp.pdb["gimp-image-add-hguide"](image, GUI_BORDER_SIZE + MARGIN_BORDER_TOP) gimp.pdb["gimp-image-add-hguide"](image, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT) gimp.pdb["gimp-image-add-hguide"](image, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT) gimp.pdb["gimp-image-add-hguide"](image, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT + HOTBAR_MARGIN_TOP) gimp.pdb["gimp-image-add-hguide"](image, GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT + HOTBAR_MARGIN_TOP + HOTBAR_HEIGHT) gimp.pdb["gimp-image-add-hguide"](image, GUI_HEIGHT) gimp.set_foreground(tuple(COLORS[3])) gimp.pdb["gimp-edit-fill"](layers["hotbar"], FOREGROUND_FILL) for i in xrange(0, 17): gimp.pdb["gimp-drawable-set-pixel"](layers["hotbar"], i, 0, 4, COLORS[1]) gimp.pdb["gimp-drawable-set-pixel"](layers["hotbar"], 0, i, 4, COLORS[1]) gimp.pdb["gimp-drawable-set-pixel"](layers["hotbar"], i + 1, 17, 4, COLORS[5]) gimp.pdb["gimp-drawable-set-pixel"](layers["hotbar"], 17, i + 1, 4, COLORS[5]) x = GUI_BORDER_SIZE y = GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT + MAIN_INVENTORY_HEIGHT + HOTBAR_MARGIN_TOP gimp.pdb["gimp-image-set-active-layer"](image, layers["hotbar"]) gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, x, y, ITEM_SLOT_SIZE, ITEM_SLOT_SIZE) gimp.pdb["gimp-edit-copy"](layers["hotbar"]) for i in xrange(1, 9): gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, x + i * ITEM_SLOT_SIZE, y, ITEM_SLOT_SIZE, ITEM_SLOT_SIZE) gimp.pdb["gimp-floating-sel-anchor"](gimp.pdb["gimp-edit-paste"](layers["hotbar"], True)) gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, x, y, ITEM_SLOT_SIZE * 9, ITEM_SLOT_SIZE) gimp.pdb["gimp-edit-copy"](layers["hotbar"]) gimp.pdb["gimp-image-set-active-layer"](image, layers["inventory"]) y = GUI_BORDER_SIZE + MARGIN_BORDER_TOP + GUI_CUSTOM_SECTION_HEIGHT for i in xrange(0, 3): gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, x, y + i * ITEM_SLOT_SIZE, GUI_WIDTH - 2 * GUI_BORDER_SIZE, ITEM_SLOT_SIZE) gimp.pdb["gimp-floating-sel-anchor"](gimp.pdb["gimp-edit-paste"](layers["inventory"], True)) for x in xrange(0, GUI_WIDTH - 2 * GUI_BORDER_SIZE): gimp.pdb["gimp-drawable-set-pixel"](layers["border-top"], x, 0, 4, COLORS[0]) gimp.pdb["gimp-drawable-set-pixel"](layers["border-bottom"], x, GUI_BORDER_SIZE - 1, 4, COLORS[0]) for y in xrange(1, 3): gimp.pdb["gimp-drawable-set-pixel"](layers["border-top"], x, y, 4, COLORS[5]) gimp.pdb["gimp-drawable-set-pixel"](layers["border-bottom"], x, GUI_BORDER_SIZE - 1 - y, 4, COLORS[2]) for y in xrange(3, 7): gimp.pdb["gimp-drawable-set-pixel"](layers["border-top"], x, y, 4, COLORS[4]) gimp.pdb["gimp-drawable-set-pixel"](layers["border-bottom"], x, GUI_BORDER_SIZE - 1 - y, 4, COLORS[4]) for y in xrange(0, GUI_HEIGHT - 2 * GUI_BORDER_SIZE): gimp.pdb["gimp-drawable-set-pixel"](layers["border-left"], 0, y, 4, COLORS[0]) gimp.pdb["gimp-drawable-set-pixel"](layers["border-right"], GUI_BORDER_SIZE - 1, y, 4, COLORS[0]) for x in xrange(1, 3): gimp.pdb["gimp-drawable-set-pixel"](layers["border-left"], x, y, 4, COLORS[5]) gimp.pdb["gimp-drawable-set-pixel"](layers["border-right"], GUI_BORDER_SIZE - 1 - x, y, 4, COLORS[2]) for x in xrange(3, 7): gimp.pdb["gimp-drawable-set-pixel"](layers["border-left"], x, y, 4, COLORS[4]) gimp.pdb["gimp-drawable-set-pixel"](layers["border-right"], GUI_BORDER_SIZE - 1 - x, y, 4, COLORS[4]) for x in xrange(0, 4): gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], x, 0, 4, COLORS[0]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], GUI_BORDER_SIZE - 1, GUI_BORDER_SIZE - 1 - x, 4, COLORS[0]) for y in xrange(1, 3): gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], x, y, 4, COLORS[5]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], GUI_BORDER_SIZE - 1 - y, GUI_BORDER_SIZE - 1 - x, 4, COLORS[2]) for y in xrange(3, 7): gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], x, y, 4, COLORS[4]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], GUI_BORDER_SIZE - 3, 2, 4, COLORS[4]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], GUI_BORDER_SIZE - 3, 1, 4, COLORS[0]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-right"], GUI_BORDER_SIZE - 2, 2, 4, COLORS[0]) gimp.pdb["gimp-image-set-active-layer"](image, layers["edge-top-right"]) gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, GUI_WIDTH - GUI_BORDER_SIZE, 0, GUI_BORDER_SIZE, GUI_BORDER_SIZE) gimp.pdb["gimp-edit-copy"](layers["edge-top-right"]) gimp.pdb["gimp-image-set-active-layer"](image, layers["edge-bottom-left"]) gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, 0, GUI_HEIGHT - GUI_BORDER_SIZE, GUI_BORDER_SIZE, GUI_BORDER_SIZE) gimp.pdb["gimp-floating-sel-anchor"](gimp.pdb["gimp-item-transform-flip"](gimp.pdb["gimp-edit-paste"](layers["edge-bottom-left"], True), 0, GUI_HEIGHT - GUI_BORDER_SIZE, GUI_BORDER_SIZE - 1, GUI_HEIGHT - 1)) for x in xrange(2, 7): gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], x, 0, 4, COLORS[0]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], 0, x, 4, COLORS[0]) for y in xrange(3, 7): gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], x, y, 4, COLORS[4]) for y in xrange(1, 3): gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], x, y, 4, COLORS[5]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], y, x, 4, COLORS[5]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], 1, 1, 4, COLORS[0]) gimp.pdb["gimp-drawable-set-pixel"](layers["edge-top-left"], 3, 3, 4, COLORS[5]) gimp.pdb["gimp-image-set-active-layer"](image, layers["edge-top-left"]) gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, 0, 0, GUI_BORDER_SIZE, GUI_BORDER_SIZE) gimp.pdb["gimp-edit-copy"](layers["edge-top-left"]) gimp.pdb["gimp-image-set-active-layer"](image, layers["edge-bottom-right"]) gimp.pdb["gimp-image-select-rectangle"](image, CHANNEL_OP_REPLACE, GUI_WIDTH - GUI_BORDER_SIZE, GUI_HEIGHT - GUI_BORDER_SIZE, GUI_BORDER_SIZE, GUI_BORDER_SIZE) gimp.pdb["gimp-floating-sel-anchor"](gimp.pdb["gimp-item-transform-flip"](gimp.pdb["gimp-edit-paste"](layers["edge-bottom-right"], True), GUI_WIDTH - GUI_BORDER_SIZE, GUI_HEIGHT, GUI_WIDTH, GUI_HEIGHT - GUI_BORDER_SIZE)) gimp.pdb["gimp-context-set-antialias"](False) gimp.pdb["gimp-context-set-sample-threshold"](0) gimp.pdb["gimp-image-select-color"](image, CHANNEL_OP_REPLACE, layers["edge-bottom-right"], tuple(COLORS[5])) gimp.set_foreground(tuple(COLORS[2])) gimp.pdb["gimp-edit-fill"](layers["edge-bottom-right"], FOREGROUND_FILL) gimp.pdb["gimp-selection-none"](image) gimp.pdb["gimp-image-set-active-layer"](image, layers["background"]) png_image = image.duplicate() png_layer = gimp.pdb["gimp-image-merge-visible-layers"](png_image, CLIP_TO_IMAGE) gimp.pdb["file-png-save"](png_image, png_layer, png_path, png_path, 0, 9, 1, 1, 1, 1, 1) try: os.mkdir(working_dir + "/util/gui-xcf") except OSError: pass gimp.pdb["gimp-xcf-save"](0, image, None, xcf_path, xcf_path)
def contact_sheet(file_type, location, all_subdirs, inc_filename, inc_extension, contact_name, contact_type, contact_location, contact_size, dpi, orient, num_col, num_rows, PageBorderLR, PageBorderTB, mmTHUMB_MARGIN, mmFONT_SIZE, Dump_Filename_list, Sorted_Images, Print_Contactsheet): # pdb.gimp_message (location) if os.path.exists(u'' + location): # pdb.gimp_message ("continuing1") # do nothing just set a variable xblot = 1 else: pdb.gimp_message(_("%s doesn't exist") % (location)) return if os.path.exists(u'' + contact_location): # pdb.gimp_message ("continuing2") # do nothing just set a variable xblot = 1 else: pdb.gimp_message(_("%s doesn't exist") % (contact_location)) return #collect 'all' images in the choosen directory and subdirs images = get_images(file_type, location, all_subdirs, Dump_Filename_list, Sorted_Images) num_images = len(images) #calculate number of images #if necessary make a txt file with image name and image directory if (Dump_Filename_list == True): Make_DirFile_List(images, contact_location, contact_name) #make a new drawing canvas of the correct size width, height = CalcPaperSize(contact_size, dpi) #dimensions in px #calculate the required size for the thumbs based on the number of images #per row. Sizes are in px and floored with maximum error of one px LEFT_PAGE_BORDER = (PageBorderLR / 25.4) * dpi RIGHT_PAGE_BORDER = (PageBorderLR / 25.4) * dpi BOTTOM_PAGE_BORDER = (PageBorderTB / 25.4) * dpi THUMB_MARGIN = (mmTHUMB_MARGIN / 25.4) * dpi FONT_SIZE = (mmFONT_SIZE / 25.4) * dpi TOP_PAGE_BORDER = (PageBorderTB / 25.4) * dpi + FONT_SIZE #include sheet .. of .. #number of rows is limited so is ThumbsPerSheet #Thumb sizes are in px, based on dpi setting if (orient == "port"): Thumb_width = ((width - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER) / num_col) - 2 * THUMB_MARGIN if (inc_filename == True): UsableRows = floor( (height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + FONT_SIZE + 2 * THUMB_MARGIN)) else: UsableRows = floor( (height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + 2 * THUMB_MARGIN)) else: Thumb_width = ((height - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER) / num_col) - 2 * THUMB_MARGIN if (inc_filename == True): UsableRows = floor((width - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + FONT_SIZE + 2 * THUMB_MARGIN)) else: UsableRows = floor((width - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER) / (Thumb_width + 2 * THUMB_MARGIN)) Thumb_height = Thumb_width #Number of chosen rows can never be bigger then usable rows if (num_rows > UsableRows): num_rows = UsableRows ThumbsPerSheet = int(num_col * num_rows) #added 'int' for python v2.6 img_no = 1 for sheetcount in range(int(ceil(num_images / float(ThumbsPerSheet)))): if (orient == "land"): sheetimg = gimp.Image(height, width, RGB) bklayer = gimp.Layer(sheetimg, "Background", height, width, RGB_IMAGE, 100, LAYER_MODE_NORMAL) else: sheetimg = gimp.Image(width, height, RGB) bklayer = gimp.Layer(sheetimg, "Background", width, height, RGB_IMAGE, 100, LAYER_MODE_NORMAL) sheetimg.disable_undo() sheetimg.add_layer(bklayer, 0) #set the image resolution sheetimg.resolution = (float(dpi), float(dpi)) #now calculate sizes; printable sheet dimensions are given #in px based on the dpi setting Canvas_width = sheetimg.width - LEFT_PAGE_BORDER - RIGHT_PAGE_BORDER Canvas_height = sheetimg.height - TOP_PAGE_BORDER - BOTTOM_PAGE_BORDER #print ("Canvas width %d height %d" % ( Canvas_width,Canvas_height)) #Log(str(sheetimg.resolution)) #now fill with white, How to fill with a other color????? bklayer.fill(FILL_WHITE) # gimp.set_background(bk_color) # gimp-bklayer-fill(bklayer, IMAGE-FILL-BG) # Log (str(bk_color)) # bklayer.fill(bk_color) # Log(str(gimp.get_background())) bklayer.flush() sheetdsp = gimp.Display(sheetimg) #print "sheet display" + str(sheetdsp) gimp.displays_flush() txtw, CalcTextHeight, txte, txtd = pdb.gimp_text_get_extents_fontname( _("Sheet %03d of %03d") % (sheetcount + 1, int(ceil(num_images / float(ThumbsPerSheet)))), FONT_SIZE, PIXELS, "Arial") ## txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, #only for me ## LEFT_PAGE_BORDER, TOP_PAGE_BORDER-CalcTextHeight, ## _("Sheet %03d of %03d"), ## contactsheet designed by R, Gilham (za) and E. Sullock Enzlin (nl)" ## % (sheetcount+1,int(ceil(num_images/float(ThumbsPerSheet)))), ## -1, False, FONT_SIZE, PIXELS, "Arial") txtfloat = pdb.gimp_text_fontname( sheetimg, sheetimg.active_layer, LEFT_PAGE_BORDER, TOP_PAGE_BORDER - CalcTextHeight, _("Sheet %03d of %03d") % (sheetcount + 1, int(ceil(num_images / float(ThumbsPerSheet)))), -1, False, FONT_SIZE, PIXELS, "Arial") pdb.gimp_floating_sel_anchor(txtfloat) CalcTextHeight = 0 txtw, txth, txte, txtd = (0, 0, 0, 0) if (inc_filename == True): txtw, CalcTextHeight, txte, txtd = pdb.gimp_text_get_extents_fontname( images[0]['base_name'], FONT_SIZE, PIXELS, "Arial") #print "CalcText Height %d " %(CalcTextHeight) files = images[sheetcount * ThumbsPerSheet:(sheetcount + 1) * ThumbsPerSheet] #now for each of the image files generate a thumbnail rcount = 0 ccount = 0 #generate thumb for file in files: thumbimg, x_size, y_size, valid_image = generate_thumb( file['image_file'], Thumb_width, Thumb_height) if valid_image == False: continue #next image cpy = pdb.gimp_edit_copy(thumbimg.active_layer) #center image within its minipage if (x_size > y_size): #landscape image, center vertical y_offset = (Thumb_width - y_size) / 2 x_offset = 0 else: #portrait image, center horizontal x_offset = (Thumb_height - x_size) / 2 y_offset = 0 gimp.delete(thumbimg) #now paste the new thumb into contact sheet newselect = pdb.gimp_edit_paste(sheetimg.active_layer, True) #print str(newselect) #print str(newselect.offsets) #positition in top left corner newselect.translate(-newselect.offsets[0], -newselect.offsets[1]) #now position in correct position, modified with x- and y-offset xpos = LEFT_PAGE_BORDER + ccount * ( Thumb_width + (2 * THUMB_MARGIN)) + THUMB_MARGIN + x_offset ypos = TOP_PAGE_BORDER + rcount * ( Thumb_height + (2 * THUMB_MARGIN) + CalcTextHeight) + THUMB_MARGIN + y_offset xpos = int( xpos ) #changed to int: on ubuntu type error integer expected got float. ypos = int(ypos) newselect.translate(xpos, ypos) pdb.gimp_floating_sel_anchor(newselect) if (inc_filename == True): if (inc_extension == True): ThumbName = file['base_name'] + file['extension'] else: ThumbName = file['base_name'] Size, txtwidth = CalcFontSize(ThumbName, "Arial", FONT_SIZE, CalcTextHeight, Thumb_width) #calculate text position, round the center of the image txt_xpos = xpos + (Thumb_width - txtwidth) / 2 - x_offset txt_ypos = ypos + Thumb_height + THUMB_MARGIN - y_offset txtfloat = pdb.gimp_text_fontname(sheetimg, sheetimg.active_layer, txt_xpos, txt_ypos, ThumbName, -1, False, Size, PIXELS, "Arial") pdb.gimp_floating_sel_anchor(txtfloat) ccount = ccount + 1 if (ccount >= num_col): ccount = 0 rcount = rcount + 1 gimp.displays_flush() #save contactsheet contact_filename = contact_name + "_%03d" % (sheetcount + 1) + contact_type contact_full_filename = os.path.join(contact_location, contact_filename) #print "File to save " + contact_full_filename if (contact_type == ".jpg"): save_jpeg(sheetimg, contact_full_filename, "") else: save_png(sheetimg, pdb.gimp_image_get_active_drawable(sheetimg), contact_full_filename, False) if (Print_Contactsheet == True): pdb.file_print_gtk(sheetimg) gimp.delete(sheetimg) pdb.gimp_display_delete(sheetdsp)
def compare_layers(layers, compare_alpha_channels=True, compare_has_alpha=False, apply_layer_attributes=True, apply_layer_masks=True): """ Return True if all layers are identical (i.e. have the same contents), False otherwise. Layer groups are also supported. The default values of the optional parameters correspond to how the layers are displayed in the image canvas. If `compare_alpha_channels` is True, perform comparison of alpha channels. If `compare_has_alpha` is True, compare the presence of alpha channels in all layers - if some layers have alpha channels and others don't, do not perform full comparison and return False. If `apply_layer_attributes` is True, take the layer attributes (opacity, mode) into consideration when comparing, otherwise ignore them. If `apply_layer_masks` is True, apply layer masks if they are enabled. If the masks are disabled or `apply_layer_masks` is False, layer masks are ignored. """ def _copy_layers(image, layers, parent=None, position=0): layer_group = pdb.gimp_layer_group_new(image) pdb.gimp_image_insert_layer(image, layer_group, parent, position) for layer in layers: copy_and_paste_layer(layer, image, parent=layer_group) for layer in layer_group.children: layer.visible = True return layer_group def _process_layers(image, layer_group, apply_layer_attributes, apply_layer_masks): for layer in layer_group.children: if pdb.gimp_item_is_group(layer): layer = merge_layer_group(layer) else: if layer.opacity != 100.0 or layer.mode != gimpenums.NORMAL_MODE: if apply_layer_attributes: layer = _apply_layer_attributes( image, layer, layer_group) else: layer.opacity = 100.0 layer.mode = gimpenums.NORMAL_MODE if layer.mask is not None: if apply_layer_masks and pdb.gimp_layer_get_apply_mask( layer): pdb.gimp_layer_remove_mask(layer, gimpenums.MASK_APPLY) else: pdb.gimp_layer_remove_mask(layer, gimpenums.MASK_DISCARD) def _is_identical(layer_group): layer_group.children[0].mode = gimpenums.DIFFERENCE_MODE for layer in layer_group.children[1:]: layer.visible = False for layer in layer_group.children[1:]: layer.visible = True histogram_data = pdb.gimp_histogram(layer_group, gimpenums.HISTOGRAM_VALUE, 1, 255) percentile = histogram_data[5] identical = percentile == 0.0 if not identical: return False layer.visible = False return True def _set_mask_to_layer(layer): pdb.gimp_edit_copy(layer.mask) floating_sel = pdb.gimp_edit_paste(layer, True) pdb.gimp_floating_sel_anchor(floating_sel) pdb.gimp_layer_remove_mask(layer, gimpenums.MASK_DISCARD) def _apply_layer_attributes(image, layer, parent_group): temp_group = pdb.gimp_layer_group_new(image) pdb.gimp_image_insert_layer(image, temp_group, parent_group, 0) pdb.gimp_image_reorder_item(image, layer, temp_group, 0) layer = merge_layer_group(temp_group) return layer if (not all(layers[0].width == layer.width for layer in layers[1:]) or not all(layers[0].height == layer.height for layer in layers[1:])): return False if compare_has_alpha and not all(layers[0].type == layer.type for layer in layers[1:]): return False image = gimp.Image(1, 1, gimpenums.RGB) layer_group = _copy_layers(image, layers) pdb.gimp_image_resize_to_layers(image) _process_layers(image, layer_group, apply_layer_attributes, apply_layer_masks) has_alpha = False for layer in layer_group.children: if pdb.gimp_drawable_has_alpha(layer): has_alpha = True # Extract alpha channel to the layer mask (to compare alpha channels). mask = pdb.gimp_layer_create_mask(layer, gimpenums.ADD_ALPHA_MASK) pdb.gimp_layer_add_mask(layer, mask) pdb.gimp_layer_set_apply_mask(layer, False) # Remove alpha channel pdb.gimp_layer_flatten(layer) identical = _is_identical(layer_group) if identical and compare_alpha_channels and has_alpha: for layer in layer_group.children: if layer.mask is not None: _set_mask_to_layer(layer) else: pdb.gimp_drawable_fill(layer, gimpenums.WHITE_FILL) identical = identical and _is_identical(layer_group) pdb.gimp_image_delete(image) return identical
def load_wavefile(filename, raw_filename): gimp.progress_init("Importing Wavefile ...") progress = 0.0 progress_offset = 0.05 # Read the file. buf = Buffer(filename) buf -= buf.getMean() buf.normalize() n_pixels = buf.getLength() # Read ID3v1 tag if available. t = ID3v1Tag() w = None h = None if t.read(filename, False) and t.title == "GIMP+Python+Nsound": try: temp = re.search("w=[0-9]+", t.comment).group(0) w = int(temp[2:]) temp = re.search("h=[0-9]+", t.comment).group(0) h = int(temp[2:]) except: w = None h = None # If we didn't read the tag, create some dimensions that are 4:3 if w == None: ratio = 4.0 / 3.0 w = 4.0 h = 3.0 while w * h < n_pixels: r = w / h if r < ratio: w += 1.0 else: h += 1.0 if w >= 1600: break w = int(w) h = int(h) gimp.progress_update(progress_offset) img = gimp.Image(w, h, gimpfu.GRAY) layer = gimp.Layer(img, "Wavefile", w, h, gimpfu.GRAYA_IMAGE, 100.0, gimpfu.NORMAL_MODE) layer.fill(gimpfu.TRANSPARENT_FILL) img.add_layer(layer, 0) # New mask mask = layer.create_mask(0) layer.add_mask(mask) (X, Y) = (layer.width, layer.height) progress_step = (1.0 - progress_offset) / float(Y) k = 0 for y in range(Y): gimp.progress_update(progress_offset + float(y) / float(Y)) # Allocate memory pixels = Buffer(Y) for x in range(X): pixels << 127.0 * buf[k] + 127.0 k += 1 if k >= n_pixels: while x < X: pixels << 127.0 x += 1 break setRow(layer, y, [pixels]) if k >= n_pixels: break gimp.progress_update(1.0) # Apply changes layer.remove_mask(gimpfu.MASK_APPLY) return img
def make_blob_output(source, source_map, output_map, map_weight, autism, neighbourhood, trys): w = int(output_map.width / 10) assert w * 10 == output_map.width assert w * 5 == output_map.height def copy_tile(src, src_x, src_y, dest, dest_x, dest_y): pdb.gimp_image_select_rectangle(src.image, gimpenums.CHANNEL_OP_REPLACE, src_x * w, src_y * w, w, w) pdb.gimp_edit_copy(src) l = pdb.gimp_edit_paste(dest, 0) l.set_offsets(dest_x * w, dest_y * w) pdb.gimp_floating_sel_anchor(l) result = gimp.Image(w * 10, w * 5) result_layer = gimp.Layer(result, "Result", result.width, result.height) result.add_layer(result_layer) # Give the user something to look at while we're working # The resynthesis is slow enough there's little to be gained from # not updating the UI result_display = gimp.Display(result) # These special tiles are the first ones to be synthesized # and are used as the seed for generating others SOLID_TILE = Tile(SOLID, SOLID, SOLID, SOLID) HORIZ_TILE = Tile(BLANK, BOTH, BLANK, BOTH) TL_TILE = Tile(BLANK, LEFT, RIGHT, BLANK) TR_TILE = Tile(BLANK, BLANK, LEFT, RIGHT) BL_TILE = Tile(LEFT, RIGHT, BLANK, BLANK) BR_TILE = Tile(RIGHT, BLANK, BLANK, LEFT) VERT_TILE = Tile(BOTH, BLANK, BOTH, BLANK) result_dict = {} # Creates a temporary image with tiles given in the 2d array # If a tile is missing, it is an indication it needs to be synthesized def synth_image(a, tile_horiz=False, tile_vert=False, show=False): a_width = max(len(row) for row in a) a_height = len(a) i = gimp.Image(a_width * w, a_height * w) l = gimp.Layer(i, "Base", i.width, i.height) i.add_layer(l) mi = gimp.Image(i.width, i.height, gimpenums.GRAY) m = gimp.Layer(mi, "Mask", mi.width, mi.height, gimpenums.GRAY_IMAGE) mi.add_layer(m) # Copy up tiles that we have, and mask for y, row in enumerate(a): for x, tile in enumerate(row): # Mask t = tiles[tile] copy_tile(output_map, t[0], t[1], m, x, y) # Image if tile in result_dict: t = result_dict[tile] copy_tile(t[0], t[1], t[2], l, x, y) # Adjust selection to tiles we don't have # Also adjust selection in result, just # so users know what tiles are being generated pdb.gimp_selection_none(i) pdb.gimp_selection_none(result) for y, row in enumerate(a): for x, tile in enumerate(row): if tile in result_dict: continue if tile is EMPTY_TILE: continue pdb.gimp_image_select_rectangle(i, gimpenums.CHANNEL_OP_ADD, x * w, y * w, w, w) t = tiles[tile] pdb.gimp_image_select_rectangle(result, gimpenums.CHANNEL_OP_ADD, t[0] * w, t[1] * w, w, w) # Do the actual resynthesize call if True: pdb.plug_in_resynthesizer(i, l, int(tile_vert), int(tile_horiz), 1, source.ID, source_map.ID, m.ID, map_weight, autism, neighbourhood, trys) # Copy to result image for y, row in enumerate(a): for x, tile in enumerate(row): if tile in result_dict: continue t = tiles[tile] copy_tile(l, x, y, result_layer, t[0], t[1]) result_dict[tile] = (result_layer, t[0], t[1]) if show: # For debugging gimp.Display(i) gimp.Display(mi) else: pdb.gimp_image_delete(i) pdb.gimp_image_delete(mi) synth_image([[EMPTY_TILE]], tile_vert=True, tile_horiz=True) synth_image([[SOLID_TILE]], tile_vert=True, tile_horiz=True) synth_image([[EMPTY_TILE, VERT_TILE, EMPTY_TILE]], tile_vert=True) synth_image([[EMPTY_TILE], [HORIZ_TILE], [EMPTY_TILE]], tile_horiz=True) synth_image([[EMPTY_TILE, EMPTY_TILE, EMPTY_TILE, EMPTY_TILE], [EMPTY_TILE, TL_TILE, TR_TILE, EMPTY_TILE], [EMPTY_TILE, BL_TILE, BR_TILE, EMPTY_TILE], [EMPTY_TILE, EMPTY_TILE, EMPTY_TILE, EMPTY_TILE]]) # We've now synth'd a tile representative of every possible # border, the rest can be done from these top_border = {LEFT: TL_TILE, RIGHT: TR_TILE, BOTH: VERT_TILE} right_border = {LEFT: TR_TILE, RIGHT: BR_TILE, BOTH: HORIZ_TILE} bottom_border = {LEFT: BR_TILE, RIGHT: BL_TILE, BOTH: VERT_TILE} left_border = {LEFT: BL_TILE, RIGHT: TL_TILE, BOTH: HORIZ_TILE} for border in (top_border, right_border, bottom_border, left_border): border[BLANK] = EMPTY_TILE border[SOLID] = SOLID_TILE for tile in tiles.keys(): if tile in result_dict: continue a = [[EMPTY_TILE, EMPTY_TILE, EMPTY_TILE], [EMPTY_TILE, EMPTY_TILE, EMPTY_TILE], [EMPTY_TILE, EMPTY_TILE, EMPTY_TILE]] a[1][1] = tile a[0][1] = top_border[tile.top] a[1][2] = right_border[tile.right] a[2][1] = bottom_border[tile.bottom] a[1][0] = left_border[tile.left] # Should really do corners here too # but the lack of them doesn't seem # to be hurting the visual quality synth_image(a)
def make_blob_output_pattern(tile_width, inset, outer_radius, inner_radius): w = tile_width h = w inner_radius = min(inner_radius, inset) outer_radius = min(outer_radius, (w - 2 * inset) / 2.0) img = gimp.Image(10 * w, 5 * h, gimpenums.GRAY) l = gimp.Layer(img, "Base", img.width, img.height, gimpenums.GRAY_IMAGE, 100, gimpenums.NORMAL_MODE) img.add_layer(l) def get_range(face, face_type): if face_type == BLANK: return None if face_type == LEFT: r = (inset, w - inset) if face_type == SOLID: r = (0, w) if face_type == RIGHT: r = (0, w - inset) if face_type == BOTH: r = (inset, w - inset * 2) if face == "top": r = (r[0], 0, r[1], inset) if face == "right": r = (w - inset, r[0], inset, r[1]) if face == "bottom": r = (w - r[0] - r[1], w - inset, r[1], inset) if face == "left": r = (0, w - r[0] - r[1], inset, r[1]) return r for tile, pos in tiles.items(): if tile is None: continue x = pos[0] * w y = pos[1] * w color = ((pos[0] + pos[1]) % 2) * 100 + 100 color = (color, color, color) color = (255, 255, 255) pdb.gimp_context_set_foreground(color) pdb.gimp_context_set_background((0, 0, 0)) # Draw the center of the filled tile cx = x + w / 2.0 cy = y + h / 2.0 b = (w - 2 * inset) / 2.0 draw_rect(l, x + inset, y + inset, w - 2 * inset, h - 2 * inset, color) if outer_radius > 0: if tile.top == BLANK and tile.left == BLANK: pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, cx - b, cy - b, outer_radius, outer_radius) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, cx - b, cy - b, 2 * outer_radius, 2 * outer_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) if tile.top == BLANK and tile.right == BLANK: pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, cx + b - outer_radius, cy - b, outer_radius, outer_radius) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, cx + b - 2 * outer_radius, cy - b, 2 * outer_radius, 2 * outer_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) if tile.bottom == BLANK and tile.right == BLANK: pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, cx + b - outer_radius, cy + b - outer_radius, outer_radius, outer_radius) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, cx + b - 2 * outer_radius, cy + b - outer_radius * 2, 2 * outer_radius, 2 * outer_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) if tile.bottom == BLANK and tile.left == BLANK: pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, cx - b, cy + b - outer_radius, outer_radius, outer_radius) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, cx - b, cy + b - outer_radius * 2, 2 * outer_radius, 2 * outer_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) # Connect the center to the edge for i, face in enumerate(("top", "right", "bottom", "left")): t = getattr(tile, face) r = get_range(face, t) if r is None: continue draw_rect(l, x + r[0], y + r[1], r[2], r[3], color) # Draw the inner corners if inner_radius > 0: def is_corner(t1, t2): return (t1 == BOTH or t1 == RIGHT) and (t2 == BOTH or t2 == LEFT) if is_corner(tile.left, tile.top): pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x, y, inset, inset) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x + inset - inner_radius, y + inset - inner_radius, inner_radius, inner_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, x + inset - inner_radius * 2, y + inset - inner_radius * 2, inner_radius * 2, inner_radius * 2) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) if is_corner(tile.top, tile.right): pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x + w - inset, y, inset, inset) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x + w - inset, y + inset - inner_radius, inner_radius, inner_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, x + w - inset, y + inset - inner_radius * 2, inner_radius * 2, inner_radius * 2) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) if is_corner(tile.right, tile.bottom): pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x + w - inset, y + h - inset, inset, inset) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x + w - inset, y + h - inset, inner_radius, inner_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, x + w - inset, y + h - inset, inner_radius * 2, inner_radius * 2) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) if is_corner(tile.bottom, tile.left): pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x, y + h - inset, inset, inset) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_image_select_rectangle(img, gimpenums.CHANNEL_OP_REPLACE, x + inset - inner_radius, y + h - inset, inner_radius, inner_radius) pdb.gimp_edit_fill(l, gimpenums.FOREGROUND_FILL) pdb.gimp_image_select_ellipse(img, gimpenums.CHANNEL_OP_INTERSECT, x + inset - inner_radius * 2, y + h - inset, inner_radius * 2, inner_radius * 2) pdb.gimp_edit_fill(l, gimpenums.BACKGROUND_FILL) pdb.gimp_selection_none(img) d = gimp.Display(img)
def makebuttons(self, filename=None, outdir=None, font=None, strcolor=None, transparency=False, bgcolor=None, glow=False, glowcolor=None, usepattern=False, pattern=None, buttoncolor=None, roundradius=None, padding=None, glowsize=None, bevelwidth=None, nova=False, novasparkles=None, novaradius=None, novacolor=None, writexcf=False, makeinactive=True, makeactive=True, makepressed=True, makejscript=True): # import used gimp pdb functions createtext = pdb['gimp_text_fontname'] selectionlayeralpha = pdb['gimp_selection_layer_alpha'] selectionfeather = pdb['gimp_selection_feather'] bucketfill = pdb['gimp_edit_bucket_fill'] selectionall = pdb['gimp_selection_all'] editclear = pdb['gimp_edit_clear'] rectselect = pdb['gimp_rect_select'] ellipseselect = pdb['gimp_ellipse_select'] selectionshrink = pdb['gimp_selection_shrink'] selectionnone = pdb['gimp_selection_none'] fill = pdb['gimp_edit_fill'] bumpmap = pdb['plug_in_bump_map'] novaplugin = pdb['plug_in_nova'] xcfsave = pdb['gimp_xcf_save'] gimp.progress_init() stringfile = open(filename) strings = [ line.strip() for line in stringfile.readlines() if self.toprocess(line) ] stringfile.close() t2nm = text_to_name_mapper(strings) (fontname, fontsize) = self.parsefont(font) (maxx, maxy) = self.getmaxextents(strings, fontsize, fontname) logging.debug("fontname: %s, fontsize: %d, maxx: %d, maxy: %d", fontname, int(fontsize), maxx, maxy) width = maxx + (padding * 4) height = maxy + (padding * 4) logging.debug("width: %d, height: %d", width, height) if roundradius > height / 2: roundradius = height / 2 - 1 if roundradius > width / 2: roundradius = width / 2 - 1 logging.debug("roundradius: %d", roundradius) for text in strings: image = gimp.Image(width, height, RGB) image.disable_undo() gimp.set_foreground(strcolor) textlayer = createtext(image, None, padding * 2, padding * 2, text, 0, 1, fontsize, 1, fontname) # center the text textlayer.set_offsets((image.width - textlayer.width) / 2 - 1, (image.height - textlayer.height) / 2 - 1) textlayer.lock_alpha = True textlayer.name = text if glow: texteffect = textlayer.copy(True) image.add_layer(texteffect, len(image.layers)) offs = texteffect.offsets texteffect.resize(image.width, image.height, offs[0], offs[1]) texteffect.lock_alpha = False image.active_layer = texteffect selectionlayeralpha(texteffect) selectionfeather(image, glowsize) gimp.set_foreground(glowcolor) bucketfill(texteffect, FG_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) btnlayer0 = gimp.Layer(image, "Background", width, height, RGBA_IMAGE, 100, NORMAL_MODE) image.add_layer(btnlayer0, len(image.layers)) selectionall(image) editclear(btnlayer0) offs = btnlayer0.offsets rectselect(image, offs[0] + roundradius, offs[1], btnlayer0.width - roundradius * 2, btnlayer0.height, CHANNEL_OP_REPLACE, 0, 0) rectselect(image, offs[0], offs[1] + roundradius, btnlayer0.width, btnlayer0.height - roundradius * 2, CHANNEL_OP_ADD, 0, 0) ellipseselect(image, offs[0], offs[1], roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0] + btnlayer0.width - roundradius * 2, offs[1], roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0], offs[1] + btnlayer0.height - roundradius * 2, roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) ellipseselect(image, offs[0] + btnlayer0.width - roundradius * 2, offs[1] + btnlayer0.height - roundradius * 2, roundradius * 2, roundradius * 2, CHANNEL_OP_ADD, False, 0, 0) selectionshrink(image, 1) selectionfeather(image, 2) if usepattern: pdb['gimp_context_set_pattern'](pattern) bucketfill(btnlayer0, PATTERN_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) else: gimp.set_background(buttoncolor) bucketfill(btnlayer0, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, True, 0, 0) selectionnone(image) selectionlayeralpha(btnlayer0) selectionfeather(image, 2) bumplayer = gimp.Layer(image, "Bumpmap", width, height, RGBA_IMAGE, 100, NORMAL_MODE) gimp.set_background(0, 0, 0) image.add_layer(bumplayer, 0) fill(bumplayer, BACKGROUND_FILL) for index in range(1, bevelwidth - 1): greyness = index * 255 / bevelwidth gimp.set_background(greyness, greyness, greyness) bucketfill(bumplayer, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionshrink(image, 1) gimp.set_background(255, 255, 255) bucketfill(bumplayer, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionnone(image) btnlayer1 = btnlayer0.copy(True) btnlayer2 = btnlayer0.copy(True) image.add_layer(btnlayer1, len(image.layers)) image.add_layer(btnlayer2, len(image.layers)) bumpmap(image, btnlayer1, bumplayer, 125, 45, 3, 0, 0, 0, 0, 0, 0, 1) bumpmap(image, btnlayer2, bumplayer, 125, 45, 3, 0, 0, 0, 0, 0, 1, 1) image.remove_layer(bumplayer) #gimp.delete(bumplayer) if nova: novalayer = gimp.Layer(image, "Nova", width, height, RGBA_IMAGE, 75, NORMAL_MODE) image.add_layer(novalayer, 0) selectionall(image) image.active_layer = novalayer editclear(novalayer) selectionnone(image) novaplugin(image, novalayer, width / 4, height / 4, novacolor, novaradius, novasparkles, 0) blackboard = gimp.Layer(image, "Blackboard", width, height, RGBA_IMAGE, 100, NORMAL_MODE) image.add_layer(blackboard, len(image.layers)) selectionall(image) if transparency: blackboard.lock_alpha = True editclear(blackboard) else: gimp.set_background(bgcolor) bucketfill(blackboard, BG_BUCKET_FILL, NORMAL_MODE, 100, 0, False, 0, 0) selectionnone(image) if writexcf: fname = t2nm.asfilename(text, 'xcf', dirname=outdir) xcfsave(0, image, textlayer, fname, fname) if makepressed: btnlayer0.visible = False btnlayer1.visible = False btnlayer2.visible = True if nova: novalayer.visible = True self.saveaspng(t2nm.asfilename(text, 'png', 'p_', outdir), image, transparency) if makeactive: btnlayer0.visible = False btnlayer1.visible = True btnlayer2.visible = False if nova: novalayer.visible = True self.saveaspng(t2nm.asfilename(text, 'png', 'a_', outdir), image, transparency) if makeinactive: btnlayer0.visible = True btnlayer1.visible = False btnlayer2.visible = False if nova: novalayer.visible = False self.saveaspng(t2nm.asfilename(text, 'png', 'i_', outdir), image, transparency) image.enable_undo() #gimp.Display(image) gimp.progress_update((strings.index(text) + 1) / len(strings)) gimp.delete(image) if makejscript: self.writejs(outdir, strings, width, height, t2nm) self.writecss(outdir, bgcolor) self.writehtml(outdir, strings, width, height, t2nm)