Skip to content

Instantly share code, notes, and snippets.

@pho
Created April 14, 2020 22:01
Show Gist options
  • Save pho/33e3f98ed83c1be1a0681beecc223fa9 to your computer and use it in GitHub Desktop.
Save pho/33e3f98ed83c1be1a0681beecc223fa9 to your computer and use it in GitHub Desktop.
Export a PNG of the cropped content of the view/selection

Put the python script in the GIMP plug-ins folder and restart GIMP

Find which folder that is in Edit / Preferences / Folders / Plug-ins

#!/usr/bin/env python
from gimpfu import *
from time import time
import gtk
import collections
import os
def do_stuff(img, layer, folder, prefix) :
filename = "{}/{}-{:.0f}.png".format(folder, prefix, time() * 1000)
gimp.progress_init("Exporting to " + filename)
new_image = pdb.gimp_image_duplicate(img)
layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(img)
pdb.gimp_image_crop(new_image, x2-x1, y2-y1,x1, y1)
gimp.progress_update(25)
pdb.plug_in_autocrop(new_image, layer)
gimp.progress_update(50)
pdb.file_png_save_defaults(new_image, layer, filename, filename)
gimp.progress_update(75)
pdb.gimp_image_delete(new_image)
gimp.progress_update(100)
register(
"extract_content_as_pngfu",
"Extract content as PNG...",
"Export a PNG of the cropped content of the view/selection",
"pho",
"pho",
"2020",
"Extract content as PNG...",
"*",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input layer", None),
(PF_DIRNAME, "folder", "Folder to save", ""),
(PF_STRING, "prefix", "File prefix", "file")
],
[],
do_stuff, menu="<Image>/Filters/Render")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment