On Sat, Jan 2, 2010 at 11:50 AM, Fab <for...@gimpusers.com> wrote:
> I am new to gimp's scripting and would like to automate the rectangle
> selection tool by defining the position and size using a script.
> Afterwards, I would like to extract the selection, 'paste as new image' and
> save the image as a png file without compression.
>
> It would be nice, if you can give me some hints, how to achieve this. Maybe,
> something similar exists already!?

Hi,

I've attached some code here - it's from a similar plug-in that I
modified to (almost) do what you want.  It will need some work for it
to be useful, but it should be pretty easy to follow and also modify
to suit your needs.  I've only given it the barest amount of testing
with gimp 2.6.6 on linux, but it should be cross-platform in theory
(as long as Python+GIMP is correctly installed).

Note that the gimp.file_png_save() call is most likely using
compression - check the procedure browser for the right parameters.

Also, I chose to duplicate/crop/save/destroy rather than
select/copy/paste new/save/destroy - I just found that method simpler.

Hope this helps and good luck!
Chris
#!/usr/bin/env python
# Author: Chris Mohler
# Copyright 2009 Chris Mohler
# License: GPL v3
# Version 0.1
# GIMP plugin to export portion of image as PNG

from gimpfu import *
import os

gettext.install("gimp20-python", gimp.locale_directory, unicode=True)

# size and position of rectangle
areaWidth = 200
areaHeight = 200
areaPosX = 20
areaPosY = 20


def export_rect(img, drw, path):
    filename = img.name + "-sm.png"
    fullpath = os.path.join(path, filename);
    tmp = img.duplicate()
    tmp.flatten()
    pdb.gimp_image_crop(tmp, areaWidth, areaHeight, areaPosX, areaPosY)
    pdb.file_png_save(tmp, tmp.layers[0], fullpath, filename, 0, 9, 1, 1, 1, 1, 1)
    pdb.gimp_image_delete(tmp)

        
register(
    proc_name=("python-fu-export-rect"),
    blurb=("Export Rectangle as PNG"),
    help=("Export Rectangle as PNG."),
    author=("Chris Mohler"),
    copyright=("Chris Mohler"),
    date=("2009"),
    label=("Export Area"),
    imagetypes=("*"),
    params=[
    (PF_IMAGE, "img", "Image", None),
    (PF_DRAWABLE, "drw", "Drawable", None),
    (PF_DIRNAME, "path", "Save PNG here", os.getcwd()),
       ],
    results=[],
    function=(export_rect), 
    menu=("<Image>/File"), 
    domain=("gimp20-python", gimp.locale_directory)
    )

main()
_______________________________________________
Gimp-user mailing list
Gimp-user@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-user

Reply via email to