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 rotate(layer, angle): """ rotate a layer """ pdb.gimp_edit_copy(layer) tmp_image = pdb.gimp_edit_paste_as_new() tmp_layer = pdb.gimp_image_get_active_layer(tmp_image) pdb.gimp_item_transform_rotate(tmp_layer, angle, False, tmp_layer.width / 2, tmp_layer.height / 2) print(tmp_layer.width, tmp_layer.height) pdb.gimp_image_resize(tmp_image, tmp_layer.width, tmp_layer.height, 0, 0) pdb.gimp_layer_set_offsets(tmp_layer, 0, 0) pdb.gimp_display_new(tmp_image) pdb.gimp_displays_flush()
def add_black_outline(image, drawable, original_layer_position, width, height, offx, offy): # make selection bigger steps = 3 pdb.gimp_selection_grow(image, steps) # create new layer type = RGBA_IMAGE name = "text background" opacity = 100 mode = NORMAL_MODE layer_textbg = pdb.gimp_layer_new(image, width, height, type, name, opacity, mode) position = original_layer_position + 1 pdb.gimp_image_add_layer(image, layer_textbg, position) #offset new layer by info pdb.gimp_layer_set_offsets(layer_textbg, offx, offy) # select layer image.active_layer = layer_textbg # set FG Color to black foreground = (0, 0, 0) pdb.gimp_context_set_foreground(foreground) # fill selection with black fill_mode = FG_BUCKET_FILL paint_mode = NORMAL_MODE opacity = 100 threshold = 0 sample_merged = 0 x = 0 y = 0 pdb.gimp_bucket_fill(layer_textbg, fill_mode, paint_mode, opacity, threshold, sample_merged, x, y) # select the text layer and merge it to the black outline merge_layer = image.layers[original_layer_position] merge_type = EXPAND_AS_NECESSARY layer = pdb.gimp_image_merge_down(image, merge_layer, merge_type) return layer
def paste_in_new_image(image2, ilayer, width, height, offx, offy): # create a new layer in the second image type = RGBA_IMAGE name = "information text {}".format(ilayer + 1) opacity = 100 mode = NORMAL_MODE layer_info = pdb.gimp_layer_new(image2, width, height, type, name, opacity, mode) position = 0 pdb.gimp_image_add_layer(image2, layer_info, position) # paste image drawable = image2.layers[0] paste_into = True floating_sel = pdb.gimp_edit_paste(drawable, paste_into) pdb.gimp_floating_sel_anchor(floating_sel) # move to original visor position pdb.gimp_layer_set_offsets(drawable, offx, offy)
def add_text_layer(image, text, font, font_size, fontunit_index, layer_position, antialias, font_color, indent, justification, hintstyle, language, letter_spacing, line_spacing, tlayer_width, tlayer_height, x_pos, y_pos): """Add text layer.""" tlayer = pdb.gimp_text_layer_new(image, text, font, font_size, fontunit_index) pdb.gimp_image_add_layer(image, tlayer, layer_position) pdb.gimp_text_layer_set_antialias(tlayer, antialias) pdb.gimp_text_layer_set_color(tlayer, font_color) pdb.gimp_text_layer_set_indent(tlayer, indent) pdb.gimp_text_layer_set_justification(tlayer, justification) pdb.gimp_text_layer_set_hint_style(tlayer, hintstyle) pdb.gimp_text_layer_set_language(tlayer, language) pdb.gimp_text_layer_set_letter_spacing(tlayer, letter_spacing) pdb.gimp_text_layer_set_line_spacing(tlayer, line_spacing) pdb.gimp_text_layer_resize(tlayer, tlayer_width, tlayer_height) pdb.gimp_layer_set_offsets(tlayer, x_pos, y_pos) pdb.gimp_item_set_visible(tlayer, True)
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)