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 add_layer(opacity=100, mode=0): image = gimp.image_list()[0] new_layer = gimp.Layer(image, "worms", image.width, image.height, 0, opacity, mode) pdb.gimp_image_add_layer(image, new_layer, 0) pdb.gimp_image_set_active_layer(image, new_layer) drawable = pdb.gimp_image_active_drawable(image)
def jetkiller(img, colormap, ignore_gray): img.undo_group_start() gimp.progress_init("Executing Jet Killer ...") src_layer = img.active_layer w = src_layer.width h = src_layer.height dst_layer = gimp.Layer(img, src_layer.name + " - Jet Killer", w, h, gimpenums.RGBA_IMAGE) dst_layer.set_offsets( *src_layer.offsets) # at the same position as the source img.add_layer(dst_layer) # on top by default input_cmap = get_colormap("jet") output_cmap = get_colormap(colormap) def raw_convert_pixel(px): # Get nearest color from input colormap and return # corresponding color in output colormap dr = input_cmap[:, 0] - ord(px[0]) dg = input_cmap[:, 1] - ord(px[1]) db = input_cmap[:, 2] - ord(px[2]) dist = dr * dr + dg * dg + db * db idx = np.argmin(dist) return "".join([chr(j) for j in output_cmap[idx]]) cache = {} def convert_pixel(px): if px not in cache: cache[px] = raw_convert_pixel(px) return cache[px] gimp.tile_cache_ntiles(_tile_cache_size) src_rgn = src_layer.get_pixel_rgn(0, 0, src_layer.width, src_layer.height, False, False) dst_rgn = dst_layer.get_pixel_rgn(0, 0, dst_layer.width, dst_layer.height, True, True) for x in xrange(src_layer.width): for y in xrange(src_layer.height): pixel = src_rgn[x, y] if not ignore_gray or (pixel[0] != pixel[1] or pixel[1] != pixel[2]): new_rgb_pixel = convert_pixel(pixel[0:3]) if len(pixel) == 4: # has an alpha channel new_rgba_pixel = new_rgb_pixel + pixel[3] else: new_rgba_pixel = new_rgb_pixel + chr(255) dst_rgn[x, y] = new_rgba_pixel gimp.progress_update(float(x) / src_layer.width) # update progress bar dst_layer.flush() dst_layer.merge_shadow() dst_layer.update(0, 0, dst_layer.width, dst_layer.height) gimp.pdb.gimp_progress_end() # important for non-interactive mode gimp.displays_flush() img.undo_group_end()
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 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 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 makeLayer(self, timg, tdrawable, lmode, up, inner, width, height, shape, prenoise, azimuth, elevation, depth, postblur, opacity, gloss, ialpha): if timg.base_type is RGB: ltype = RGBA_IMAGE else: ltype = GRAYA_IMAGE if type(self.activelayer) == gimp.Layer: pdb.gimp_image_set_active_layer(timg, self.activelayer) activename = self.activelayer.name else: activename = "" gimp.context_push() timg.undo_group_start() #create the bevel layer if inner: lname = "Inner Bevel: " + activename else: lname = "Outer Bevel: " + activename newlayer = gimp.Layer(timg, lname, timg.width, timg.height, ltype, opacity, lmode) timg.add_layer(newlayer, -1) #save current selection tmpchan2 = pdb.gimp_selection_save(timg) #intersect with alpha? if ialpha and (type(self.activelayer) == gimp.Layer): pdb.gimp_selection_layer_alpha(self.activelayer) tmpchan = pdb.gimp_selection_save(timg) pdb.gimp_channel_combine_masks(tmpchan, tmpchan2, 3, 0, 0) pdb.gimp_selection_load(tmpchan) else: tmpchan = pdb.gimp_selection_save(timg) #set fg and bg colours and fill the layer with black pdb.gimp_context_set_foreground((0, 0, 0)) pdb.gimp_context_set_background((255, 255, 255)) newlayer.fill(FOREGROUND_FILL) #fill the selection with white and apply blur pdb.gimp_edit_fill(newlayer, BACKGROUND_FILL) #at this point, select all... pdb.gimp_selection_all(timg) #blurs pdb.plug_in_gauss_rle(timg, newlayer, width, 1, 0) pdb.plug_in_gauss_rle(timg, newlayer, height, 0, 1) #apply shape curve if shape != 0: pdb.gimp_curves_spline(newlayer, 0, 6, (0, 0, int(127 + (shape / 10)), (shape + 127), 255, 255)) #invert colours of whole layer if down if not up: pdb.gimp_invert(newlayer) #prenoise if prenoise > 0: pdb.plug_in_hsv_noise(timg, newlayer, 4, 0, 0, prenoise) #emboss the selection pdb.plug_in_emboss(timg, newlayer, azimuth, elevation, depth, 1) #gloss if gloss > 0: y1 = int(1.0 * gloss) y2 = int(3.0 * gloss) y3 = int(9.6 * gloss) y4 = int(3.2 * gloss) y5 = int(4.0 * gloss) y6 = int(0.4 * gloss) pdb.gimp_curves_spline(newlayer, 0, 16, (0, 0, 63, (63 + y1), 95, (95 + y2), 127, (127 - y3), 156, (156 + y4), 191, (191 - y5), 223, (223 + y6), 255, 255)) #postblur if postblur > 0: pdb.plug_in_gauss_rle(timg, newlayer, postblur, 1, 1) pdb.gimp_layer_set_lock_alpha(newlayer, False) #reload selection pdb.gimp_selection_load(tmpchan) #clear excess if inner: pdb.gimp_selection_invert(timg) if not pdb.gimp_selection_is_empty(timg): pdb.gimp_edit_clear(newlayer) #reload original selection pdb.gimp_selection_load(tmpchan2) #remove temp channels timg.remove_channel(tmpchan) timg.remove_channel(tmpchan2) #set active layer to what it was if type(self.activelayer) == gimp.Layer: pdb.gimp_image_set_active_layer(timg, self.activelayer) else: pdb.gimp_image_set_active_layer(timg, newlayer) #end undo group and finish plugin gimp.context_pop() timg.undo_group_end() pdb.gimp_displays_flush() # debugMessage("I got to end of makelayer") return newlayer
def remap_hue(self): #input can be RGB or RGBA bpp = self.drawable.bpp (bx1, by1, bx2, by2) = self.drawable.mask_bounds bw = bx2 - bx1 bh = by2 - by1 #input layer offset (ox, oy) = self.drawable.offsets src_rgn = self.drawable.get_pixel_rgn(bx1, by1, bw, bh, False, False) #all the input pixels in one huge array #src_rgn[...] returns a packed byte array as a string #we unpack this string using python array src_pixels = array.array("B", src_rgn[bx1:bx2, by1:by2]) #delete existing preview layer self.layer_destroy() #create new output layer self.layer = gimp.Layer(self.image, "Hue map", bw, bh, RGBA_IMAGE, 100, NORMAL_MODE) #set correct position self.layer.set_offsets(bx1 + ox, by1 + oy) dest_rgn = self.layer.get_pixel_rgn(0, 0, bw, bh, True, True) #all the output pixels dest_pixels = array.array("B", dest_rgn[0:bw, 0:bh]) #output is always RGBA dest_bpp = 4 #add layer into image self.image.add_layer(self.layer, 0) #for 8bit RGB, the hue resolution is 6*256 = 1536 #sampling in lower resolution (like 360°) would result in color loss #we pre-sample the gradient instead of sampling it on each pixel #it results in better performance on larger selections (> 39x39 px) num_samples = 6 * 256 hue_samples = pdb.gimp_gradient_get_uniform_samples( self.gradient_button.get_gradient(), num_samples + 1, False)[1] hues = [None] * num_samples for i in range(0, num_samples): #convert rgb into hue sample_rgb = gimpcolor.RGB(hue_samples[i * 4 + 0], hue_samples[i * 4 + 1], hue_samples[i * 4 + 2], hue_samples[i * 4 + 3]) hues[i] = sample_rgb.to_hsv().h #start a progress bar gimp.progress_init("Hue map") for y in range(0, bh): for x in range(0, bw): pos = (x + bw * y) * bpp #read a pixel as a 3 or 4 byte array c_array = src_pixels[pos:(pos + bpp)] #create a RGB struct, if there is no alpha, set it to 100% c_rgb = gimpcolor.RGB(c_array[0], c_array[1], c_array[2], c_array[3] if bpp == 4 else 255) #RGB -> HSV c_hsv = c_rgb.to_hsv() #calculate index into hue replacement array hue_index = int(round(c_hsv.h * num_samples)) #replace hue c_hsv.h = hues[hue_index] #HSV -> RGB c_rgb = c_hsv.to_rgb() #RGB -> byte array c_array[0:dest_bpp] = array.array("B", c_rgb[0:dest_bpp]) dest_pos = (x + bw * y) * dest_bpp #write a pixel into the output array dest_pixels[dest_pos:(dest_pos + dest_bpp)] = c_array #update the progress bar gimp.progress_update(float(y + 1) / bh) #write the output pixel array into the output layer dest_rgn[0:bw, 0:bh] = dest_pixels.tostring() #apply changes self.layer.flush() #apply selection mask self.layer.merge_shadow(True) #refresh self.layer.update(0, 0, bw, bh) #refresh gimp.displays_flush()
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 run(img, layer, name, domain, axis): if axis == "X": (X, Y) = (layer.width, layer.height) else: (Y, X) = (layer.width, layer.height) mag_layer = None pha_layer = None type = None new_layers = [] gimp.context_push() img.undo_group_start() if img.base_type == gimpfu.RGB: type = gimpfu.RGBA_IMAGE else: type = gimpfu.GRAY_IMAGE if domain == "FORWARD": w = X X = FFTransform.roundUp2(X) new_layers.append( gimp.Layer(img, name + " Phase %d" % w, X/2, Y, type, 100.0, gimpfu.NORMAL_MODE)) new_layers.append( gimp.Layer(img, name + " Magnitude %d" % w, X/2, Y, type, 100.0, gimpfu.NORMAL_MODE)) elif domain == "REVERSE": # Search the image for the highest layers that are labeled with Phase # and Magnitude for l in img.layers: if " Magnitude " in l.name: mag_layer = l elif " Phase " in l.name: pha_layer = l if mag_layer != None and pha_layer != None: break if mag_layer == None: raise Exception("Could not locate 'FFT Magnitude X' layer!") elif pha_layer == None: raise Exception("Could not locate 'FFT Phase X' layer!") # Okay, we have both layers, read the width in the name X = None t1 = re.search(" [0-9]+", mag_layer.name).group(0) t2 = re.search(" [0-9]+", pha_layer.name).group(0) if t1 == t2: X = int(t1) if X == None: raise Exception("Could not determing layer's original size!") new_layers.append( gimp.Layer(img, name + " Reverse", X, Y, type, 100.0, gimpfu.NORMAL_MODE)) for i in range (len(new_layers)): new_layers[i].set_offsets(layer.offsets[0],layer.offsets[1]) new_layers[i].fill(gimpfu.TRANSPARENT_FILL) img.add_layer(new_layers[i], 0) # New mask mask = new_layers[i].create_mask(0) new_layers[i].add_mask(mask) gimp.progress_init("Transforming pixels ...") transform = FFTransform(X) if domain == "FORWARD": for y in range(Y): gimp.progress_update(float(y) / float(Y)) pixels = getRow(layer, y, axis) output_pixels = [ [], [] ] for color in range(len(pixels)): buf = pixels[color] buf.normalize() # Transform into frequency domain if domain == "FORWARD": # Perform over sampled FFT fdomain = transform.fft(buf, X, 0) mag = fdomain[0].getMagnitude() pha = fdomain[0].getPhase() mag.normalize() pha.normalize() # Scale to pixel max mag *= 255.0 pha *= 255.0 # Stuff into the output pixels output_pixels[0].append(pha) output_pixels[1].append(mag) for i in range(len(output_pixels)): setRow(new_layers[i], y, output_pixels[i], axis) elif domain == "REVERSE": for y in range(Y): gimp.progress_update(float(y) / float(Y)) pixels = {} pixels["mag"] = getRow(mag_layer, y, axis) pixels["pha"] = getRow(pha_layer, y, axis) output_pixels = [] for color in range(len(pixels["mag"])): mag = pixels["mag"][color] pha = pixels["pha"][color] mag.normalize() # Center the phase about 0.0 and scale by PI pha -= 0.5 * pha.getMean() pha.normalize() pha *= math.pi ch = FFTChunk(X) ch.setPolar(mag,pha) tdomain = transform.ifft([ch]) tdomain.normalize() output_pixels.append(255.0 * tdomain) setRow(new_layers[0], y, output_pixels, axis) gimp.progress_update(1.0) # Apply changes for i in range(len(new_layers)): new_layers[i].remove_mask(gimpfu.MASK_APPLY) img.undo_group_end() gimp.context_pop()
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)
def run(img, layer, name, hi_lo, ftype, order, frequency, direction): gimp.context_push() img.undo_group_start() is_iir = None if ftype == "IIR": is_iir = True else: is_iir = False if img.base_type == gimpfu.RGB: type = gimpfu.RGBA_IMAGE else: type = gimpfu.GRAY_IMAGE new_layer = gimp.Layer(img, name, layer.width, layer.height, type, 100.0, gimpfu.NORMAL_MODE) new_layer.set_offsets(layer.offsets[0], layer.offsets[1]) new_layer.fill(gimpfu.TRANSPARENT_FILL) img.add_layer(new_layer, 0) # New mask mask = new_layer.create_mask(0) new_layer.add_mask(mask) f = None sample_rate = float(layer.width) if is_iir: if hi_lo == "LP": f = FilterLowPassIIR(sample_rate, order, frequency, 0.0) else: f = FilterHighPassIIR(sample_rate, order, frequency, 0.0) # Estimate filter delay pulse = Buffer(2 * order) pulse << 1.0 for i in range(0, order): pulse << 0.0 result = Buffer(2 * order) result << f.filter(pulse) # Find the peak mmax = result.getMax() delay = None for i in range(0, result.getLength()): if result[i] == mmax: delay = i + 1 break else: if hi_lo == "LP": f = FilterLowPassFIR(sample_rate, order, frequency) else: f = FilterHighPassFIR(sample_rate, order, frequency) delay = (f.getKernelSize() - 1) / 2 gimp.progress_init("Filtering pixels ...") done_factor = 1.0 offset = 0.0 directions = [direction] if direction == "Both": done_factor = 0.5 directions = ["X", "Y"] for axis in directions: if "X" == axis: (X, Y) = (layer.width, layer.height) else: (Y, X) = (layer.width, layer.height) for y in range(0, Y): gimp.progress_update(offset + done_factor * float(y) / float(Y)) pixels = getRow(layer, y, axis) for i in range(len(pixels)): buf = pixels[i] f.reset() # Prime the filter for j in range(delay): f.filter(buf[0]) last = buf[-1] k = 0 for j in range(buf.getLength()): # while j is < delay, throw out samples if j < delay: f.filter(buf[j]) else: buf[k] = f.filter(buf[j]) k += 1 for j in range(delay): buf[k] = f.filter(last) k += 1 pixels[i] = buf setRow(new_layer, y, pixels, axis) offset += 0.5 layer = new_layer gimp.progress_update(1.0) # Apply changes new_layer.remove_mask(gimpfu.MASK_APPLY) img.undo_group_end() gimp.context_pop()
def LBP(self): width = self.drawable.width height = self.drawable.height #bpp = self.drawable.bpp rad = self.radius #debugging messages #print("Velikost obrazku: " + str(width) + "x" + str(height)) #create new output layer self.layer = gimp.Layer(self.image, "LBP", width, height, GRAY_IMAGE, 100, NORMAL_MODE) #add layer into image self.image.add_layer(self.layer, 0) #region for output dest_rgn = self.layer.get_pixel_rgn(0, 0, width, height, True, True) #custom array for results drawTo = array.array("B") #get image data (region) and convert to array imageArr = self.drawable.get_pixel_rgn(0, 0, width, height, False, False) imageArr_pixels = array.array("B", imageArr[0:width, 0:height]) #convert 1D array to 2D array tmp = np.reshape(imageArr_pixels, (-1, width)) #create array with mirrored border dataBorder = np.pad(array=tmp, pad_width=rad, mode='symmetric') gimp.progress_init("LBP") for x in range(rad, height + rad): for y in range(rad, width + rad): binaryNum = [] #get exactly eight pixels in radius from pixel [x,y] src_pixels = dataBorder[x - rad:x + 1 + rad:rad, y - rad:y + 1 + rad:rad] src_pixels = src_pixels.ravel() #make 2D array 1D #get value of the center pixel [x,y] center = src_pixels[4] #sequence of indexes indexesNeeded = [0, 1, 2, 5, 8, 7, 6, 3] for i in indexesNeeded: if src_pixels[i] > center: binaryNum.append(0) else: binaryNum.append(1) #"count" LBP for pixel st = ''.join(format(str(x)) for x in binaryNum) res = int(st, 2) drawTo.append(res) gimp.progress_update(float(x + 1) / height) #result dest_rgn[0:width, 0:height] = drawTo.tostring() #apply changes self.layer.flush() #apply selection mask self.layer.merge_shadow(True) #refresh self.layer.update(0, 0, width, height) #refresh gimp.displays_flush()