def python_animation_from_videocaps(image, drawable, fps=24, reduce_by=4, text_mode='none', text_layer=None): """The plugin's main function.""" gimpfu.gimp.progress_init('Working on frames...') # Reduce frames delay = int(1000 / fps * reduce_by) for i, layer in enumerate(image.layers[:]): if i % reduce_by == 0: layer.name = '%s (%sms)' % (layer.name, delay) else: pdb.gimp_image_remove_layer(image, layer) gimpfu.gimp.progress_update(0.5) # Add text if text_mode != 'none' and text_layer.image == image: pdb.gimp_layer_resize_to_image_size(text_layer) pdb.gimp_edit_copy(text_layer) layers = image.layers[::2] if text_mode == 'blink' else image.layers for i, layer in enumerate(layers): selection = pdb.gimp_edit_paste(layer, True) pdb.gimp_floating_sel_anchor(selection) pdb.gimp_image_remove_layer(image, text_layer) gimpfu.gimp.progress_update(1)
def python_animation_from_videocaps(image, drawable, fps=24, reduce_by=4, text_mode='none', text_layer=None): """The plugin's main function.""" gimpfu.gimp.progress_init('Working on frames...') # Reduce frames delay = int(1000 / fps * reduce_by) for i, layer in enumerate(image.layers[:]): if i % reduce_by == 0: layer.name = '%s (%sms)' % (layer.name, delay) else: pdb.gimp_image_remove_layer(image, layer) gimpfu.gimp.progress_update(0.5) # Add text if text_mode != 'none' and text_layer.image == image: pdb.gimp_layer_resize_to_image_size(text_layer) pdb.gimp_edit_copy(text_layer) layers = image.layers[::2] if text_mode == 'blink' else image.layers for i, layer in enumerate(layers): selection = pdb.gimp_edit_paste(layer, True) pdb.gimp_floating_sel_anchor(selection) pdb.gimp_image_remove_layer(image, text_layer) gimpfu.gimp.progress_update(1)
def add_alpha(image=None): if not image: image = gimp.image_list()[0] image_size = (image.width, image.height) for layer in image.layers: if not pdb.gimp_drawable_has_alpha(layer): layer.add_alpha() if layer.offsets != (0, 0) or (layer.width, layer.height) != image_size: pdb.gimp_layer_resize_to_image_size(layer)
def nobox(image=None): if not image: image = gimp.image_list()[0] pdb.gimp_context_set_sample_transparent(True) image_size = (image.width, image.height) for layer in image.layers: if not pdb.gimp_drawable_has_alpha(layer): layer.add_alpha() if layer.offsets != (0, 0) or (layer.width, layer.height) != image_size: pdb.gimp_layer_resize_to_image_size(layer) (xmax, ymax) = (image.width - 1, image.height - 1) for (x, y) in [(0, 0), (xmax, 0), (0, ymax), (xmax, ymax)]: _num_channels, pixel = pdb.gimp_drawable_get_pixel(layer, x, y) if all(p > 240 for p in pixel): pdb.gimp_image_select_contiguous_color(image, 2, layer, x, y) pdb.gimp_edit_clear(layer)
def sort(image=None): if not image: image = gimp.image_list()[0] image_size = (image.width, image.height) for layer in image.layers: if layer.offsets != (0, 0) or (layer.width, layer.height) != image_size: pdb.gimp_layer_resize_to_image_size(layer) more = True while more: more = False for a, b in zip(image.layers, image.layers[1:]): move_down = False try: move_down = int(a.name) < int(b.name) except ValueError: move_down = a.name < b.name if move_down: pdb.gimp_image_lower_item(image, a) more = True break
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 stickerify_bordure(image, current_layer, black_grow=3, white_grow=12, shadow=True, canvas_increase=0, resize=False): def duplicate_layer(): copy = current_layer.copy() image.add_layer(copy) # copy is added above so we want to go down a bit image.active_layer = current_layer return copy def fill_black(): pdb.gimp_edit_bucket_fill(current_layer, 1, 0, 100, 255, 0, 0, 0) def fill_white(): pdb.gimp_edit_bucket_fill(current_layer, 0, 0, 100, 255, 0, 0, 0) def set_colors(): pdb.gimp_context_set_foreground((255, 255, 255)) pdb.gimp_context_set_background((0, 0, 0)) pdb.gimp_context_push() pdb.gimp_image_undo_group_start(image) # clean selection to avoid bugs pdb.gimp_selection_none(image) set_colors() # resize early to avoid compressing the bordure if resize: width, height = image.width, image.height if width == height: new_width, new_height = 512, 512 elif width > height: new_width, new_height = 512, int(height * (512.0 / width)) elif width < height: new_width, new_height = int(width * (512.0 / height)), 512 pdb.gimp_image_scale(image, new_width, new_height) if canvas_increase: width, height = image.width, image.height width_increase = int(width * (canvas_increase / 100)) height_increase = int(height * (canvas_increase / 100)) pdb.gimp_image_resize(image, width + width_increase, height + height_increase, int(width_increase / 2), int(height_increase / 2)) pdb.gimp_layer_resize_to_image_size(current_layer) duplicate_layer() # alpha to selection pdb.gimp_image_select_item(image, 0, current_layer) pdb.gimp_selection_grow(image, black_grow) fill_black() second_layer = duplicate_layer() pdb.gimp_selection_grow(image, white_grow) fill_white() if shadow: duplicate_layer() fill_black() current_layer.translate(8, 8) pdb.gimp_selection_all(image) pdb.plug_in_gauss(image, current_layer, 20, 20, 0) pdb.gimp_layer_set_opacity(current_layer, 70) pdb.gimp_image_merge_down(image, second_layer, 0) if shadow: pdb.gimp_image_merge_down(image, image.active_layer, 0) pdb.gimp_layer_set_name(image.active_layer, "Sticker bordure") pdb.gimp_selection_none(image) pdb.gimp_image_undo_group_end(image) pdb.gimp_context_pop() pdb.gimp_displays_flush()
def layerToDDS(timg, tdrawable, file, argumentFile, outFolder, byName, scaleBool, scaleTo): #, argumentFile,hmm,blub): # num_layers, layer_ids = pdb.gimp_image_get_layers(image) # with open("E:/out.txt",'w') as outTxt: # outTxt.write(str(os.getcwd())) outFolder = os.path.dirname(file) + "/" + outFolder if not os.path.exists(outFolder): os.makedirs(outFolder) if argumentFile.lower() == "all": image = pdb.gimp_file_load(file, file) fileContent = [] for layer in image.layers: layername = pdb.gimp_item_get_name(layer) fileContent.append(layername + " " + layername) byName = True else: with open(argumentFile, 'r') as argfile: fileContent = [line for line in argfile] for l in fileContent: image = pdb.gimp_file_load(file, file) if scaleBool: if scaleTo: pdb.gimp_image_resize(image, 52, 52, -26, -26) else: pdb.gimp_image_scale(image, 52, 52) line = shlex.split(l) outFile = outFolder + "/" + line[0] if outFile[-4:] != ".dds": outFile += ".dds" for layer in image.layers: # layer = pdb.gimp_image_get_layer_by_name(image, id) pdb.gimp_item_set_visible(layer, False) layers = [] for i, word in enumerate(line[1:]): if byName: if word[:4] == "MOVE": word2 = word[5:] layer = pdb.gimp_image_get_layer_by_name(image, word2) # pdb.gimp_item_set_visible(layer,True) pdb.gimp_layer_scale(layer, 40, 40, False) pdb.gimp_image_set_active_layer(image, layer) drawable = pdb.gimp_image_get_active_layer(image) if word[4] == "L": pdb.script_fu_move_layer_to(image, drawable, -2.5, 5) elif word[4] == "R": pdb.script_fu_move_layer_to(image, drawable, 17.5, 5) line[1 + i] = line[1 + i][5:] # pdb.gimp_item_set_visible(layer,False) for word in line[1:]: #outTxt.write(word+"\n") #layers.append(pdb.gimp_layer_copy(image.layers[int(word)],False)) #pdb.gimp_image_insert_layer(image, layer,image.layers[int(word)], 0) try: if byName: pdb.gimp_item_set_visible( pdb.gimp_image_get_layer_by_name(image, word), True) else: pdb.gimp_item_set_visible(image.layers[int(word)], True) except: pdb.gimp_message("Error parsing line " + " ".join(line) + ". Probably a missing/invalid item") raise if scaleBool and scaleTo: for word in line[2:]: if byName: layer = pdb.gimp_image_get_layer_by_name(image, word) else: layer = image.layers[int(word)] pdb.gimp_layer_scale(layer, scaleTo, scaleTo, False) pdb.gimp_layer_resize(layer, 52, 52, -1, -1) # for layer in layers: # outTxt.write("{!s}".format(layer)) # pdb.gimp_item_set_visible(layer, True) #pdb.gimp_item_set_visible(image.layers[3], True) #pdb.gimp_item_set_visible(image.layers[5], 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) for layerIt in image.layers: if layerIt != layer or not scaleTo: pdb.gimp_layer_resize_to_image_size(layerIt) try: pdb.file_dds_save(image, drawable, outFile, outFile, 0, 0, 0, 0, 0, 0, 0) except: pdb.file_dds_save(image, drawable, outFile, outFile, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)