def prepare_tts(img, _, output_folder): # pylint: disable=R0914 """ Prepare a TTS sheet image. """ gimp.progress_init('Prepare a TTS sheet image...') pdb.gimp_undo_push_group_start(img) try: file_name, _ = _get_filename_backside(img, 'jpg') except Exception: # pylint: disable=W0703 pdb.gimp_undo_push_group_end(img) return parts = file_name.split('_') num = int(parts[-3]) rows = int(parts[-4]) columns = int(parts[-5]) new_width = columns * 750 new_height = rows * 1050 pdb.gimp_image_scale(img, new_width, new_height) json_path = re.sub(r'\.jpg$', '.json', pdb.gimp_image_get_filename(img)) try: with open(json_path, 'r') as fobj: cards = json.load(fobj) except Exception: # pylint: disable=W0703 pdb.gimp_undo_push_group_end(img) return cards = [c['path'] for c in cards] if len(cards) != num: pdb.gimp_undo_push_group_end(img) return card_rows = [ cards[i * columns:(i + 1) * columns] for i in range((len(cards) + columns - 1) // columns) ] if len(card_rows) != rows: pdb.gimp_undo_push_group_end(img) return for i, card_row in enumerate(card_rows): for j, card_path in enumerate(card_row): if not os.path.exists(card_path): pdb.gimp_undo_push_group_end(img) return card_layer = pdb.gimp_file_load_layer(img, card_path) pdb.gimp_image_insert_layer(img, card_layer, None, -1) rotation = _get_rotation(card_layer) if rotation: _rotate(card_layer, True) pdb.gimp_layer_set_offsets(card_layer, j * 750, i * 1050) pdb.gimp_image_merge_down(img, card_layer, 1) pdb.file_jpeg_save(img, img.layers[0], os.path.join(output_folder, file_name), file_name, 1, 0, 1, 0, '', 2, 1, 0, 0) pdb.gimp_undo_push_group_end(img)
def cree_background(inLayer, inLayerF, Img): """Add background to inLayer""" newlayer = pdb.gimp_layer_copy(inLayer, True) newfond = pdb.gimp_layer_copy(inLayerF, True) layername = pdb.gimp_item_get_name(inLayer) pdb.gimp_item_set_visible(newlayer, True) pdb.gimp_image_insert_layer(Img, newlayer, None, 0) pdb.gimp_image_set_active_layer(Img, inLayer) pdb.gimp_image_insert_layer(Img, newfond, None, -1) pdb.gimp_image_lower_item(Img, newfond) pdb.gimp_item_set_visible(newfond, 1) pdb.gimp_context_set_sample_transparent(True) pdb.gimp_image_select_contiguous_color(Img, CHANNEL_OP_REPLACE, newlayer, 10, 10) pdb.gimp_edit_clear(newfond) pdb.gimp_item_set_visible(inLayer, True) clipped_layer = pdb.gimp_image_merge_down(Img, inLayer, CLIP_TO_IMAGE) pdb.gimp_item_set_name(clipped_layer, layername) pdb.gimp_selection_invert(Img) pdb.gimp_context_set_foreground((117, 117, 154)) pdb.gimp_edit_fill(newlayer, FILL_FOREGROUND) floating_sel = pdb.gimp_edit_paste(newlayer, 0) pdb.gimp_layer_set_opacity(floating_sel, 70) pdb.gimp_floating_sel_anchor(floating_sel) pdb.gimp_layer_set_opacity(newlayer, 85) pdb.gimp_selection_none(Img) layerfinal = pdb.gimp_image_get_layer_by_name(Img, layername) pdb.gimp_item_set_visible(layerfinal, False)
def layer_images(imageFiles): """Joins imageFiles as layers.""" initialFile = next(imageFiles) layeredImage = pdb.gimp_file_load(initialFile, initialFile) for imageFile in imageFiles: newLayer = pdb.gimp_file_load_layer(layeredImage, imageFile) pdb.gimp_image_insert_layer(layeredImage, newLayer, None, 0) return layeredImage
def load_caches(args): inColorFondR, inColorFondG, inColorFondB, outDir = args.split(" ") inDir, inNameLayerFleche = GRAPH_PATH + "layers/", "layer_fleche-1.png" inColorFondR, inColorFondG, inColorFondB = int(inColorFondR), int( inColorFondG), int(inColorFondB) nb, listimg = pdb.file_glob(inDir + "*.png", 1) baseimage = pdb.gimp_image_new(10, 10, RGB) fondcolor = (inColorFondR, inColorFondG, inColorFondB) for filename in listimg: layer = pdb.gimp_file_load_layer(baseimage, filename) pdb.gimp_image_insert_layer(baseimage, layer, None, 0) pdb.gimp_image_resize_to_layers(baseimage) pdb.gimp_message("Layers chargés") pdb.gimp_selection_all(baseimage) layerfond = pdb.gimp_image_get_layers(baseimage)[1][0] layerfond = gimp.Item.from_id(layerfond) fond = pdb.gimp_layer_copy(layerfond, 1) _, _, _, xmax, ymax = pdb.gimp_selection_bounds(baseimage) pdb.gimp_item_set_name(fond, "layer_fond.png") pdb.gimp_image_insert_layer(baseimage, fond, None, 0) pdb.gimp_edit_clear(fond) pdb.gimp_image_select_round_rectangle(baseimage, CHANNEL_OP_REPLACE, 0, 0, xmax, ymax, 35, 35) pdb.gimp_selection_shrink(baseimage, 3) pdb.gimp_selection_feather(baseimage, 20) pdb.gimp_context_set_foreground(fondcolor) pdb.gimp_edit_fill(fond, FILL_FOREGROUND) pdb.gimp_image_lower_item_to_bottom(baseimage, fond) pdb.plug_in_hsv_noise(baseimage, fond, 5, 38, 63, 74) pdb.gimp_selection_none(baseimage) pdb.gimp_message("Fond créé") caches(baseimage, inNameLayerFleche, "layer_fond.png") pdb.gimp_message("Cache créé") layercache = pdb.gimp_image_get_layer_by_name(baseimage, "cache.png") layerfond = pdb.gimp_image_get_layer_by_name(baseimage, "layer_fond.png") layerfleche = pdb.gimp_image_get_layer_by_name(baseimage, inNameLayerFleche) pdb.gimp_item_set_visible(layerfond, True) pdb.gimp_image_merge_down(baseimage, layercache, CLIP_TO_IMAGE) pdb.gimp_item_set_visible(layerfleche, True) pdb.gimp_image_merge_down(baseimage, layerfleche, CLIP_TO_IMAGE) pdb.gimp_image_scale(baseimage, 900, 550) # drawable = pdb.gimp_image_get_active_drawable(baseimage) pdb.script_fu_multiple_layer_actions(baseimage, None, 0, 0, (0, 0, 0), 4, 0, 0, 0, 0, 0) pdb.gimp_message("Taille de l'image ajustée") pdb.script_fu_export_layers(baseimage, None, outDir, "~l") pdb.gimp_message("Layers enregistrés")
def draw_circle_2(img, layer, c_x, c_y, rayon, color): """ draw a plain circle on layer """ start_x = c_x - rayon if c_x - rayon > 0 else 0 start_y = c_y - rayon if c_y - rayon > 0 else 0 width = rayon * 2 height = rayon * 2 print(start_x, start_y, c_x, c_y, width, height) # Make sure this layer supports alpha so we can write to each pixel's # alpha component layer.add_alpha() circle_layer = pdb.gimp_layer_new(img, layer.width, layer.height, RGBA_IMAGE, "circle", 100, LAYER_MODE_NORMAL) pdb.gimp_image_insert_layer(img, circle_layer, None, 0) source_region = layer.get_pixel_rgn(start_x, start_y, width + 1, height + 1, False, False) pixel_size = len(source_region[start_x, start_y]) print("pixel_size:", pixel_size) region = circle_layer.get_pixel_rgn(start_x, start_y, start_x + width + 1, start_y + height + 1, True, True) pixels = array("B", "\x00" * (width * height * pixel_size)) for y in xrange(0, height): for x in xrange(0, width): index = (x + width * y) * pixel_size # pixel = source_pixels[index: index + pixel_size] new_pixel = array('B', [color[0], color[1], color[2], 255]) # Write the modified pixel out to our destination array xx = x - rayon yy = y - rayon a = xx * xx + yy * yy b = rayon * rayon if a <= b: pixels[index:index + pixel_size] = new_pixel # Copy the whole array into the writeable pixel region region[start_x:start_x + width, start_y:start_y + height] = pixels.tostring() # Write our changes back over the original layer circle_layer.flush() circle_layer.merge_shadow(True) circle_layer.update(start_x, start_y, width, height) # merge the circle layer and the background layer new_layer = merge_layer(img, layer, circle_layer) return new_layer
def effet_texte(inColor, inImg): layer = pdb.gimp_image_get_active_layer(inImg) pdb.gimp_context_set_sample_transparent(True) pdb.gimp_image_select_contiguous_color(inImg, CHANNEL_OP_REPLACE, layer, 1, 1) pdb.gimp_selection_invert(inImg) _, x, _, x2, y2 = pdb.gimp_selection_bounds(inImg) xmiddle = x + (x2 - x) // 2 y = y2 - 1 keep = True while keep: if y < 0: print("Error in effet_texte for img {} : y < 0 !".format(inImg)) return if (not _is_transparent(inImg, layer, xmiddle, y)) and _is_black(inImg, layer, xmiddle, y): keep = False pdb.gimp_context_set_sample_transparent(False) pdb.gimp_image_select_contiguous_color(inImg, CHANNEL_OP_REPLACE, layer, xmiddle, y) pdb.gimp_edit_clear(layer) y -= 1 _, x1, y1, x2, y2 = pdb.gimp_selection_bounds(inImg) newlayer = pdb.gimp_layer_copy(layer, True) pdb.gimp_image_select_rectangle(inImg, CHANNEL_OP_REPLACE, x1, y1, x2-x1, y2-y1) pdb.gimp_context_set_sample_transparent(True) pdb.gimp_image_select_contiguous_color(inImg, CHANNEL_OP_SUBTRACT, layer, x1, y1) pdb.gimp_edit_clear(layer) pdb.gimp_selection_invert(inImg) pdb.gimp_image_insert_layer(inImg, newlayer, None, 0) pdb.gimp_edit_clear(newlayer) pdb.script_fu_layerfx_outer_glow(inImg, newlayer, inColor, 75, 0, 0, 0, 0, 5, 0, 1) pdb.gimp_image_merge_visible_layers(inImg, CLIP_TO_IMAGE) pdb.gimp_selection_none(inImg) pdb.gimp_displays_flush()
def createTechIcons(timg, tdrawable,file,folder, outFolder,scaleTo): for fileName in os.listdir(folder): # backGroundImage=pdb.gimp_file_load(file, file) # backGroundLayer=backGroundImage.layers[0] if fileName[-4:]!=".dds": continue outFile=outFolder+"/"+"tech_"+fileName image=pdb.gimp_file_load(folder+"/"+fileName, folder+"/"+fileName) pdb.gimp_image_scale(image,scaleTo,scaleTo) pdb.gimp_image_resize(image,52,52,-1,-1) for layer in image.layers: pdb.gimp_layer_resize_to_image_size(layer) # layer_group = pdb.gimp_layer_group_new(image) layer_group=pdb.gimp_item_get_parent(image.layers[0]) layer=pdb.gimp_file_load_layer(image, file) pdb.gimp_image_insert_layer(image,layer,layer_group,1)#(image, layer, parent, position) for layer in image.layers: pdb.gimp_item_set_visible(layer,True) layer = pdb.gimp_image_merge_visible_layers(image, 0) pdb.gimp_image_set_active_layer(image, layer) drawable = pdb.gimp_image_get_active_layer(image) # with open("E:/out.txt",'w') as outTxt: # outTxt.write(outFile) pdb.file_dds_save(image, drawable, outFile,outFile,0,0,0,0,0,0,0)
def sheet(image=None, cols=0): if not image: image = gimp.image_list()[0] if not cols: best = (1, 10000000) for cols in range(1, len(image.layers) + 1): rows = (len(image.layers) + (cols - 1)) // cols (sheet_width, sheet_height) = (cols * image.width, rows * image.height) sheet_aspect_ratio = sheet_width / sheet_height if sheet_width > sheet_height else sheet_height / sheet_width if sheet_aspect_ratio < best[1]: best = (cols, sheet_aspect_ratio) cols = best[0] file_path = "{}_sheet_{}_frames_{}_columns_{}x{}.png".format( pdb.gimp_image_get_filename(image)[:-4], len(image.layers), cols, image.width, image.height) gimp.progress_init("Save sheet as {}".format(file_path)) rows = (len(image.layers) + (cols - 1)) // cols sheet = pdb.gimp_image_new(image.width * cols, image.height * rows, 0) try: sheet_layer = pdb.gimp_layer_new( sheet, sheet.width, sheet.height, 1, # type = RGBA-IMAGE "sprite sheet", 100, # opacity = 100 % 0 # mode = LAYER-MODE-NORMAL-LEGACY ) pdb.gimp_image_insert_layer(sheet, sheet_layer, None, 0) (row, col) = (0, 0) for (layer_index, layer) in enumerate(image.layers): pdb.gimp_selection_none(image) pdb.gimp_layer_resize_to_image_size(layer) pdb.gimp_edit_copy(layer) floating = pdb.gimp_edit_paste(sheet_layer, True) (left, top) = floating.offsets pdb.gimp_layer_translate(floating, col * image.width - left, row * image.height - top) pdb.gimp_floating_sel_anchor(floating) col += 1 if col >= cols: col = 0 row += 1 gimp.progress_update(100 * (layer_index + 1) / len(image.layers)) pdb.file_png_save( sheet, sheet_layer, file_path, None, True, # interlace 9, # compression True, # bkgd True, # gama True, # offs True, # phys True, # time ) gimp.message("All frames saved as {}".format(file_path)) finally: pdb.gimp_image_delete(sheet)
def insert_layer(layeredImage, currentLayer, Delay): """Insert currentLayer into layeredImage with Delay.""" layerName = '({}ms)'.format(int(Delay)) pdb.gimp_item_set_name(currentLayer, layerName) pdb.gimp_image_insert_layer(layeredImage, currentLayer, None, 0)
def halftone_gimp(img, layer, slide_density, slide_circle_size, work_size_flag, work_size_width, revert_size_flag, black_strength, slide_angle): # Obtain layer dimensions. width = layer.width height = layer.height print("original size:", width, height) print("density", slide_density) D = int(slide_density) print("circle size", "slide_circle_size") C = int(slide_circle_size) print("black strength", black_strength) B = int(black_strength) ANGLES = POSSIBLE_ANGLES[int(slide_angle) - 1] print("angles", ANGLES) # New image dimensions if work_size_flag is True: new_width = int(work_size_width) new_height = int(height * (new_width / float(width))) print("new_size:", new_width, new_height) scale_image(img, new_width, new_height) else: new_width = width new_height = height add_cmyk_layers(img, layer) halftone_layers = [None] * 4 for k in range(0, 4): current_layer = find_layer(img, COMPONENT_STRING[k]) rotate_layer(current_layer, ANGLES[k]) halftone_layers[k] = pdb.gimp_layer_new( img, current_layer.width, current_layer.height, RGBA_IMAGE, COMPONENT_STRING[k] + " halftone", 100, MULTIPLY_MODE) pdb.gimp_image_insert_layer(img, halftone_layers[k], None, 0) pdb.gimp_drawable_fill(halftone_layers[k], FILL_WHITE) start = timer() halftone_layer(img, current_layer, halftone_layers[k], D, COLORS[k], C, B) end = timer() print("halftone in ", end - start) rotate_layer(halftone_layers[k], -ANGLES[k]) x0, y0 = pdb.gimp_drawable_offsets(halftone_layers[k]) non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(img) print(x0, y0) print(x1, y1, x2, y2) xx = new_width / 2 - current_layer.width / 2 yy = new_height / 2 - current_layer.height / 2 # print xx, yy pdb.gimp_layer_set_offsets(halftone_layers[k], x0 + xx, y0 + yy) for i_layer in COMPONENT_STRING: del_layer = find_layer(img, i_layer) img.remove_layer(del_layer) for i_layer in img.layers: if i_layer.name.endswith("halftone"): i_layer.visible = False # if revert_size_flag is True: # scale_image(img, width, height) else: i_layer.visible = False for i_layer in img.layers: if i_layer.name.endswith("halftone"): current_layer = pdb.gimp_layer_new(img, new_width, new_height, RGBA_IMAGE, "_" + i_layer.name + "_", 100, MULTIPLY_MODE) pdb.gimp_image_insert_layer(img, current_layer, None, 0) pdb.gimp_rect_select(img, 0, 0, new_width, new_height, 2, 0, 0) pdb.gimp_edit_copy(i_layer) new_layer = pdb.gimp_edit_paste(current_layer, True) pdb.gimp_floating_sel_anchor(new_layer) for i_layer in img.layers: if i_layer.name.endswith("halftone"): print(i_layer.name) img.remove_layer(i_layer) if revert_size_flag is True: scale_image(img, width, height)
def add_cmyk_layers(img, layer): """ decompose layer in cmyk components """ # Obtain layer dimensions. width = layer.width height = layer.height # Make sure this layer supports alpha so we can write to each pixel's alpha # component layer.add_alpha() for k in range(0, 4): # Grab a pixel region (readonly) covering the entire image and copy # pixel data into an array source_region = layer.get_pixel_rgn(0, 0, width, height, False, False) source_pixels = array("B", source_region[0:width, 0:height]) pixel_size = len(source_region[0, 0]) print("pixel_size", pixel_size) # Create component layer in the Image component_layer = pdb.gimp_layer_new(img, width, height, RGBA_IMAGE, COMPONENT_STRING[k], 100, LAYER_MODE_NORMAL) pdb.gimp_image_insert_layer(img, component_layer, None, 0) pdb.gimp_drawable_fill(component_layer, FILL_WHITE) # Create another region (writeable) and an array that can store all # our modified pixels component_region = component_layer.get_pixel_rgn( 0, 0, width, height, True, True) component_pixels = array("B", "\x00" * (width * height * pixel_size)) gimp.progress_init("getting " + COMPONENT_STRING[k] + " component layer...") x = 0 y = 0 # Loop through every pixel in the image/layer for y in xrange(0, (height - 1)): for x in xrange(0, (width - 1)): gimp.progress_update(1.0 * y / height) source_index = (x + width * y) * pixel_size pixel = source_pixels[source_index:source_index + pixel_size] # Write the modified pixel out to our destination array component_pixel = pixel # intensity = 0 if k == 3: # black specific intensity = pixel[k] # intensity = int(round(pixel[0] * 0.2126 + # pixel[1] * 0.7152 + # pixel[2] * 0.0722)) # c_linear = (pixel[0] / 255.0) * 0.2126 # + (pixel[1] / 255.0) * 0.7152 # + (pixel[2] / 255.0) * 0.0722 # intensity = 12.92 * c_linear if c_linear <= 0.0031308 else 1.055 * \ # pow(c_linear, 1/2.4) - 0.055 # intensity = int(round(intensity*255.0)) intensity = int( round(pixel[0] * 0.299 + pixel[1] * 0.587 + pixel[2] * 0.114)) else: intensity = pixel[k] component_pixel[3] = (255 - intensity) component_pixel[0] = COLORS[k][0] component_pixel[1] = COLORS[k][1] component_pixel[2] = COLORS[k][2] component_pixels[source_index:source_index + pixel_size] = component_pixel # Copy the whole array into the writeable pixel region component_region[0:width, 0:height] = component_pixels.tostring() # Write our changes back over the original layer component_layer.flush() component_layer.merge_shadow(True) component_layer.update(0, 0, width, height) pdb.gimp_progress_end() pdb.gimp_displays_flush()