示例#1
0
def prepare_pdf_back(img, drawable, output_folder):
    """ Prepare a back image for PDF document.
    """
    gimp.progress_init('Prepare a back image for PDF document...')
    pdb.gimp_undo_push_group_start(img)

    try:
        file_name, back_side = _get_filename_backside(img)
    except Exception:  # pylint: disable=W0703
        pdb.gimp_undo_push_group_end(img)
        return

    if not back_side:
        pdb.gimp_undo_push_group_end(img)
        return

    rotation = _get_rotation(drawable)

    if rotation:
        _rotate(drawable, back_side)

    pdb.file_png_save(img, drawable,
                      os.path.join(output_folder, file_name), file_name,
                      0, 9, 1, 0, 0, 1, 1)
    pdb.gimp_undo_push_group_end(img)
def png(image=None):
    if not image:
        image = gimp.image_list()[0]
    prefix = pdb.gimp_image_get_filename(image)[:-4] + "_"
    gimp.progress_init("Save frames as {}_*.png".format(prefix))
    for (layer_index, layer) in enumerate(image.layers):
        try:
            filename = "{}{:02d}.png".format(prefix, int(layer.name))
        except ValueError:
            filename = "{}{}.png".format(prefix, layer.name)
        pdb.file_png_save(
            image,
            layer,
            filename,
            None,
            True,  # interlace
            9,  # compression
            True,  # bkgd
            True,  # gama
            True,  # offs
            True,  # phys
            True,  # time
        )
        gimp.progress_update(100 * (layer_index + 1) / len(image.layers))
    gimp.message("All frames saved as {}_*.png".format(prefix))
示例#3
0
def prepare_makeplayingcards(img, drawable, output_folder):
    """ Prepare an image for MakePlayingCards printing.
    """
    gimp.progress_init('Prepare an image for MakePlayingCards printing...')
    pdb.gimp_undo_push_group_start(img)

    try:
        file_name, back_side = _get_filename_backside(img)
    except Exception:  # pylint: disable=W0703
        pdb.gimp_undo_push_group_end(img)
        return

    rotation = _get_rotation(drawable)
    clip_size = _get_mpc_clip_size(drawable)

    if rotation:
        _rotate(drawable, back_side)

    if clip_size:
        _clip(img, drawable, clip_size, rotation and back_side)

    pdb.file_png_save(img, drawable,
                      os.path.join(output_folder, file_name), file_name,
                      0, 9, 1, 0, 0, 1, 1)
    pdb.gimp_undo_push_group_end(img)
示例#4
0
def prepare_pdf_front_old(img, drawable, output_folder):
    """ Prepare a front image for PDF document. [OLD VERSION]
    """
    gimp.progress_init('Prepare a front image for PDF document...')
    pdb.gimp_undo_push_group_start(img)

    try:
        file_name, back_side = _get_filename_backside(img)
    except Exception:  # pylint: disable=W0703
        pdb.gimp_undo_push_group_end(img)
        return

    if back_side:
        pdb.gimp_undo_push_group_end(img)
        return

    rotation = _get_rotation(drawable)
    margin_size = _get_pdf_margin_size(drawable)
    if rotation:
        _rotate(drawable, back_side)

    if margin_size:
        _add_margin(img, drawable, margin_size)

    clip_size = _get_pdf_clip_size(drawable)
    if clip_size:
        _clip(img, drawable, clip_size, rotation and back_side)

    pdb.file_png_save(img, drawable, os.path.join(output_folder, file_name),
                      file_name, 0, 9, 1, 0, 0, 1, 1)
    pdb.gimp_undo_push_group_end(img)
示例#5
0
def python_runway(img, layer):
    if not layer.is_rgb:
        raise ValueError("Expected RGB layer")
    # because pdb cannot save to a buffer, we have to use a temporary file instead
    f = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    f.close()
    pdb.file_png_save(img, layer, f.name, f.name, 0, 9, 0, 0, 0, 0, 0)
    # convert data from file to base64 encoded bytes
    data = open(f.name, "rb").read()
    b64 = base64.b64encode(data)
    os.unlink(f.name)
    # send data to Runway via a POST request
    data = json.dumps({"semantic_map": b64})
    req = urllib2.Request("http://localhost:8000/query", data,
                          {"Content-Type": "application/json"})
    f = urllib2.urlopen(req)
    resp = json.loads(f.read())
    f.close()
    # save result to a temporary file, because pdb cannot load from a buffer
    jpg = base64.b64decode(resp["output"][22:])
    f = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
    f.close()
    open(f.name, "wb").write(jpg)
    # open the temp file
    image = pdb.gimp_file_load(f.name, f.name)
    # copy the first layer to clipboard
    pdb.gimp_edit_copy(image.layers[0])
    os.unlink(f.name)
    # paste clipboard contents as a floating selection
    floating = pdb.gimp_edit_paste(layer, 0)
    floating.name = layer.name + " [Runway]"
示例#6
0
def run(directory, rewrite = False):
	from gimpfu import pdb
	start = time.time()
	#for all xcf files in working directory
	print("Running on directory '{0}'".format(directory))
	for filename in os.listdir(directory):
		if filename[-4:] in allowedFormat:
			print("Found a file : '{0}'".format(filename))
			image = pdb.gimp_file_load(os.path.join(directory, filename), os.path.join(directory, filename))

			with open(os.path.join(directory, os.path.splitext(filename)[0]+".desc"), "wb") as f:
				for layer in image.layers:
					for c in [layer] + layer.children:
						f.write("texture = {}\n"\
								"size = {}, {}\n"\
								"position = {}, {}\n".format(c.name, c.width, c.height, *c.offsets))

						print("Write layer '{0}' in '{0}.png'".format(c.name))
						# write the new png file
						pdb.file_png_save(image, c, os.path.join(directory, c.name +".png"), 
													os.path.join(directory, c.name +".png"), 
													0, 9, 0, 0, 0, 0, 0)

	end = time.time()
	print("Finished, total processing time : {0:.{1}f}".format(end-start, 2))
示例#7
0
def run(directory, rewrite=False):
    from gimpfu import pdb
    start = time.time()
    #for all xcf files in working directory
    print("Running on directory '{0}'".format(directory))
    for filename in os.listdir(directory):
        if filename[-4:] in allowedFormat:
            print("Found a file : '{0}'".format(filename))
            image = pdb.gimp_file_load(os.path.join(directory, filename),
                                       os.path.join(directory, filename))

            with open(
                    os.path.join(directory,
                                 os.path.splitext(filename)[0] + ".desc"),
                    "wb") as f:
                for layer in image.layers:
                    for c in [layer] + layer.children:
                        f.write("texture = {}\n"\
                          "size = {}, {}\n"\
                          "position = {}, {}\n".format(c.name, c.width, c.height, *c.offsets))

                        print("Write layer '{0}' in '{0}.png'".format(c.name))
                        # write the new png file
                        pdb.file_png_save(
                            image, c, os.path.join(directory, c.name + ".png"),
                            os.path.join(directory, c.name + ".png"), 0, 9, 0,
                            0, 0, 0, 0)

    end = time.time()
    print("Finished, total processing time : {0:.{1}f}".format(end - start, 2))
示例#8
0
def prepare_db_output(img, drawable, output_folder):
    """ Prepare an image for DB output.
    """
    gimp.progress_init('Prepare an image for DB output...')
    pdb.gimp_undo_push_group_start(img)

    try:
        file_name, _ = _get_filename_backside(img)
    except Exception:  # pylint: disable=W0703
        pdb.gimp_undo_push_group_end(img)
        return

    pdb.script_fu_round_corners(img, drawable, 40, 0, 0, 0, 0, 0, 0)

    pdb.file_png_save(img, drawable, os.path.join(output_folder, file_name),
                      file_name, 0, 9, 1, 0, 0, 1, 1)
    pdb.gimp_undo_push_group_end(img)
示例#9
0
def cut_bleed_margins(img, drawable, output_folder):
    """ Cut bleed margins from an image.
    """
    gimp.progress_init('Cut bleed margins from an image...')
    pdb.gimp_undo_push_group_start(img)

    try:
        file_name, _ = _get_filename_backside(img)
    except Exception:  # pylint: disable=W0703
        pdb.gimp_undo_push_group_end(img)
        return

    clip_size = _get_bleed_margin_size(drawable)
    if clip_size:
        _clip(img, drawable, clip_size, False)

    pdb.file_png_save(img, drawable, os.path.join(output_folder, file_name),
                      file_name, 0, 9, 1, 0, 0, 1, 1)
    pdb.gimp_undo_push_group_end(img)
示例#10
0
def texte(inR,inG,inB,inFichier,highlight_text,change_numero):
    print("\tLoading image {}".format(inFichier))
    image = pdb.gimp_file_load(inFichier,inFichier)
    color = (inR,inG,inB)

    # pdb.gimp_display_new(image)
    if highlight_text:
        print("\t\tHighlighting text...")
        effet_texte(color, image)
    
    if change_numero > 0:
        print("\t\tChanging number...")
        change_num(image, change_numero)
    
    pdb.gimp_selection_none(image)
    layer_id = pdb.gimp_image_get_layers(image)[1][0]
    layer = gimp.Item.from_id(layer_id)
    pdb.script_fu_add_bevel(image, layer, 5, False, False)
    drawable = pdb.gimp_image_get_active_drawable(image)

    pdb.file_png_save(image, drawable, inFichier, inFichier,
                        0,5,0,0,0,0,0) 
    pdb.gimp_image_delete(image)
示例#11
0
def python_gaugan(img, layer, style="random"):
    if not layer.is_rgb:
        raise ValueError("Expected RGB layer")
    # because pdb cannot save to a buffer, we have to use a temporary file instead
    f = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    f.close()
    pdb.file_png_save(img, layer, f.name, f.name, 0, 9, 0, 0, 0, 0, 0)
    data = open(f.name, "rb").read()
    os.unlink(f.name)
    # send to GauGAN API
    gaugan = GauganAPI()
    jpg = gaugan.convert(data, style)
    # save result to a temporary file, because pdb cannot load from a buffer
    f = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
    f.close()
    open(f.name, "wb").write(jpg)
    # open the temp file
    image = pdb.gimp_file_load(f.name, f.name)
    # copy the first layer to clipboard
    pdb.gimp_edit_copy(image.layers[0])
    os.unlink(f.name)
    # paste clipboard contents as a floating selection
    floating = pdb.gimp_edit_paste(layer, 0)
    floating.name = layer.name + " [GauGAN]"
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)