def pythonSaveToPSD(image):

    # Prep
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()

    # Code
    drawable = pdb.gimp_image_get_active_drawable(image)
    filename = pdb.gimp_image_get_filename(image)
    temp = filename.replace("_CLEAN", "")
    temp2 = temp.replace("_EDIT", "")
    temp = temp2.replace("_XCF", "")
    filename = temp.replace("_PSD", "")
    path = os.path.dirname(filename)
    path += "_PSD"
    name = os.path.basename(filename)
    out_psd = os.path.join(path, name)
    out_psd = os.path.splitext(out_psd)[0] + '.psd'

    # Create folder
    if not os.path.exists(path):
        os.makedirs(path)

    # Save in PSD
    pdb.gimp_file_save(image, drawable, out_psd, out_psd)
    pdb.gimp_image_clean_all(image)

    # Finish
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_displays_flush()
Exemple #2
0
def pythonSaveToClean(image):

    # Prep
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()

    # Code
    filename_old = pdb.gimp_image_get_filename(image)
    filename = filename_old.replace("_CLEAN", "")
    # drawable = pdb.gimp_image_get_active_drawable(image)
    path = os.path.dirname(filename)
    path += "_CLEAN"
    name = os.path.basename(filename)
    out_file = os.path.join(path, name)

    # Create CLEAN folder
    if not os.path.exists(path):
        os.makedirs(path)

    # Merge visible layers in new image and export in CLEAN folder
    new_image = pdb.gimp_image_duplicate(image)
    layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
    pdb.gimp_file_save(new_image, layer, out_file, out_file)
    pdb.gimp_image_delete(new_image)
    pdb.gimp_image_clean_all(image)

    # Finish
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_displays_flush()
def pythonSaveToEditJpeg(image):

    # Prep
    pdb.gimp_image_undo_group_start(image)
    pdb.gimp_context_push()

    # Code
    # drawable = pdb.gimp_image_get_active_drawable(image)
    filename = pdb.gimp_image_get_filename(image)
    temp = filename.replace("_CLEAN", "")
    temp2 = temp.replace("_EDIT", "")
    filename = temp2.replace("_XCF", "")
    path = os.path.dirname(filename)
    path += "_EDIT"
    name = os.path.basename(filename)
    out_file = os.path.join(path, name)
    out_file = os.path.splitext(out_file)[0] + '.jpg'

    # Create _EDIT folder
    if not os.path.exists(path):
        os.makedirs(path)

    # Merge visible layers in new image and export in JPEG
    new_image = pdb.gimp_image_duplicate(image)
    layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
    pdb.gimp_file_save(new_image, layer, out_file, out_file)
    pdb.gimp_image_delete(new_image)
    pdb.gimp_image_clean_all(image)

    # Finish
    pdb.gimp_context_pop()
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_displays_flush()
Exemple #4
0
def make_captcha(sx, sy, font_height, letter_spacing, left_margin, angle_range,
                 fonts, answer):
    """Generate a captcha consisting of the letters in answer.

    :rtype: :class:`gimp.Image`
    :returns: The CAPTCHA as a gimp-python image object.
    """
    img = gimp.Image(sx, sy, RGB_IMAGE)
    img.disable_undo()

    light_noise_layer = gimp.Layer(img, 'light noise', sx, sy, RGB_IMAGE, 100,
                                   NORMAL_MODE)
    img.add_layer(light_noise_layer, 0)

    gpdb.gimp_selection_none(img)
    gpdb.gimp_drawable_fill(light_noise_layer, WHITE_FILL)

    # plug_in_randomize_hurl at 1% 1 time is vastly superior to
    # scatter_rgb here, but has a bug where it creates an artifact at
    # the top of the image when invoked in a scripting context like
    # this.
    #
    # Future experiment: dial down the amount of noise generated by
    # scatter, then run it through levels to darken it, then
    # blur. This should be equivalent to hurl + blur.
    #gpdb.plug_in_randomize_hurl(img, light_noise_layer, 1, 1, 0, 0)
    gpdb.plug_in_scatter_hsv(img, light_noise_layer, 1, 25, 200, 180)
    gpdb.plug_in_gauss_iir(img, light_noise_layer, 1, 1, 1)
    gpdb.gimp_desaturate(light_noise_layer)

    # Next make pure black layer which we will copy repeatedly as a
    # place to cut out letters.
    blackLayer = gimp.Layer(img, 'black', sx, sy, RGB_IMAGE, 100, NORMAL_MODE)
    img.add_layer(blackLayer, 0)
    blackLayer.add_alpha()
    gpdb.gimp_layer_add_alpha(blackLayer)
    gpdb.gimp_drawable_fill(blackLayer, WHITE_FILL)
    gpdb.gimp_invert(blackLayer)

    # Loop through each letter, making it a separate black layer.
    right = left_margin
    last_substrate = None
    for letter in answer:
        font = random.choice(FONTS)
        substrate = blackLayer.copy()
        img.add_layer(substrate, 0)
        new_right = cookie_cutter_letter(img, substrate, right, font, letter)
        # look out for really narrow letters
        if new_right - right < 20:
            new_right += 5
        right = new_right
    img.remove_layer(blackLayer)

    # Hide the light noise layer, then collapse all the remaining
    # layers (all letters) into a single layer.
    light_noise_layer.visible = False
    textLayer = gpdb.gimp_image_merge_visible_layers(img, CLIP_TO_IMAGE)
    light_noise_layer.visible = True

    # Create a layer of dark noise which will display the letters.
    dark_noise_layer = gimp.Layer(img, 'dark noise', sx, sy, RGB_IMAGE, 100,
                                  MULTIPLY_MODE)
    img.add_layer(dark_noise_layer, 1)
    gpdb.gimp_drawable_fill(dark_noise_layer, WHITE_FILL)
    gpdb.plug_in_randomize_hurl(img, dark_noise_layer, 25, 1, 0, 0)
    gpdb.gimp_desaturate(dark_noise_layer)

    # These next operations are ordered carefully. Changing the order
    # dramatically affects how the output looks.

    # Here's where we do the cutout operation.
    gpdb.gimp_selection_layer_alpha(textLayer)
    gpdb.gimp_selection_invert(img)
    gpdb.gimp_edit_clear(dark_noise_layer)
    gpdb.gimp_selection_none(img)

    # After the cutout, blur the dark noise layer and then darken it:
    gpdb.plug_in_gauss_iir(img, dark_noise_layer, 1, 1, 1)
    gpdb.gimp_levels(dark_noise_layer, HISTOGRAM_VALUE, 127, 255, 0.25, 0, 255)
    textLayer.visible = False

    # If you start gimp without --no-interface with an X server, this
    # line will let you see the image looks like at this point in the
    # script, layers and all. It should be fine to move this line to
    # any problematic part of the script for debugging.
    #
    # gimp.Display(gpdb.gimp_image_duplicate(img))

    final = img.flatten()
    gpdb.gimp_image_clean_all(img)
    img.enable_undo()
    return img, final
def runPlugin(

  filename_input,
  filename_result,
  filename_face_detect,
  filename_cascade,
  colour_rectangle
) :

	"""
	This function gets registered with GIMP as it implements the following Plugin : Detect Faces

	This function will attempt to detect faces within an image, and any faces which it does detect it will frame with a rectangle. Once it has finished doing this,
	it will save the resulting image into a file.


	Parameters:

	filename_input (String) :
	The filename of the input image.

	filename_result (String) :
	The filename of the file into which the resulting image will be saved.

	filename_cascade (String) :
	The filename of the Harr Transform Cascade file which should be used to detect the faces.

	colour_rectangle (String) :
	The colour to use for the face framing rectangles.


	Returns:

	NA


	Invoked by :

	GIMP


	Invokes:

	None
	"""


	nameProcedure = "runPlugin_single"


	print("%s : Enter" % (nameProcedure))

	print("%s : %s" % (nameProcedure, filename_input))
	print("%s : %s" % (nameProcedure, filename_result))
	print("%s : %s" % (nameProcedure, colour_rectangle))

	# Open and display withih GIMP, the input image.

	image_input = pdb.gimp_file_load(filename_input, filename_input)

	pdb.gimp_display_new(image_input)

	# "Clean" the following image by setting its Dirty (Edit) counter to 0. 
	#
	# If this is not done and the user attempts to close the image, then GIMP will complain that the image has
	# unsaved changes.

	pdb.gimp_image_clean_all(image_input)

	# Run the Face detection code on the Input image.
	#
	# At the time this Plugin was developed, Python and its Plugins could only be implemented using Python 2; whereas the Face detection code has 
	# unfortunately been implemented using Python 3 source code and modules. Therefore, if this Plugin were to try and run the Face detection code
	# directly, then the Python Runtime system would fail. It is for this reason that the Face detection code has been invoked below as a separate
	# processs.

	subprocess.call(
	  [
	   "python3",
	   filename_face_detect,
	   filename_cascade,
	   filename_input,
	   filename_result,
	   colour_rectangle
	  ]
	)

	# Open and display within GIMP, the result image.

	image_result = pdb.gimp_file_load(filename_result, filename_result)

	pdb.gimp_display_new(image_result)

	# "Clean" the following image by setting its Dirty (Edit) counter to 0. 
	#
	# If this is not done and the user attempts to close the image, then GIMP will complain that the image has
	# unsaved changes.

	pdb.gimp_image_clean_all(image_result)

	print("%s : Exit" % (nameProcedure))
def make_captcha(sx, sy, font_height, letter_spacing, left_margin,
                 angle_range, fonts, answer):
    """Generate a captcha consisting of the letters in answer.

    :rtype: :class:`gimp.Image`
    :returns: The CAPTCHA as a gimp-python image object.
    """
    img = gimp.Image(sx, sy, RGB_IMAGE)
    img.disable_undo()

    light_noise_layer = gimp.Layer(img, 'light noise', sx, sy,
                                   RGB_IMAGE, 100, NORMAL_MODE)
    img.add_layer(light_noise_layer, 0)

    gpdb.gimp_selection_none(img)
    gpdb.gimp_drawable_fill(light_noise_layer, WHITE_FILL)

    # plug_in_randomize_hurl at 1% 1 time is vastly superior to
    # scatter_rgb here, but has a bug where it creates an artifact at
    # the top of the image when invoked in a scripting context like
    # this.
    #
    # Future experiment: dial down the amount of noise generated by
    # scatter, then run it through levels to darken it, then
    # blur. This should be equivalent to hurl + blur.
    #gpdb.plug_in_randomize_hurl(img, light_noise_layer, 1, 1, 0, 0)
    gpdb.plug_in_scatter_hsv(img, light_noise_layer, 1, 25, 200, 180)
    gpdb.plug_in_gauss_iir(img, light_noise_layer, 1, 1, 1)
    gpdb.gimp_desaturate(light_noise_layer)

    # Next make pure black layer which we will copy repeatedly as a
    # place to cut out letters.
    blackLayer = gimp.Layer(img, 'black', sx, sy, RGB_IMAGE, 100, NORMAL_MODE)
    img.add_layer(blackLayer, 0)
    blackLayer.add_alpha()
    gpdb.gimp_layer_add_alpha(blackLayer)
    gpdb.gimp_drawable_fill(blackLayer, WHITE_FILL)
    gpdb.gimp_invert(blackLayer)

    # Loop through each letter, making it a separate black layer.
    right = left_margin
    last_substrate = None
    for letter in answer:
        font = random.choice(FONTS)
        substrate = blackLayer.copy()
        img.add_layer(substrate, 0)
        new_right = cookie_cutter_letter(img, substrate, right, font, letter)
        # look out for really narrow letters
        if new_right - right < 20:
            new_right += 5
        right = new_right
    img.remove_layer(blackLayer)

    # Hide the light noise layer, then collapse all the remaining
    # layers (all letters) into a single layer.
    light_noise_layer.visible = False
    textLayer = gpdb.gimp_image_merge_visible_layers(img, CLIP_TO_IMAGE)
    light_noise_layer.visible = True

    # Create a layer of dark noise which will display the letters.
    dark_noise_layer = gimp.Layer(img, 'dark noise', sx, sy,
                                  RGB_IMAGE, 100, MULTIPLY_MODE)
    img.add_layer(dark_noise_layer, 1)
    gpdb.gimp_drawable_fill(dark_noise_layer, WHITE_FILL)
    gpdb.plug_in_randomize_hurl(img, dark_noise_layer, 25, 1, 0, 0)
    gpdb.gimp_desaturate(dark_noise_layer)

    # These next operations are ordered carefully. Changing the order
    # dramatically affects how the output looks.

    # Here's where we do the cutout operation.
    gpdb.gimp_selection_layer_alpha(textLayer)
    gpdb.gimp_selection_invert(img)
    gpdb.gimp_edit_clear(dark_noise_layer)
    gpdb.gimp_selection_none(img)

    # After the cutout, blur the dark noise layer and then darken it:
    gpdb.plug_in_gauss_iir(img, dark_noise_layer, 1, 1, 1)
    gpdb.gimp_levels(dark_noise_layer, HISTOGRAM_VALUE, 127, 255, 0.25, 0, 255)
    textLayer.visible = False

    # If you start gimp without --no-interface with an X server, this
    # line will let you see the image looks like at this point in the
    # script, layers and all. It should be fine to move this line to
    # any problematic part of the script for debugging.
    #
    # gimp.Display(gpdb.gimp_image_duplicate(img))

    final = img.flatten()
    gpdb.gimp_image_clean_all(img)
    img.enable_undo()
    return img, final