It is currently Sat Jun 22, 2024 9:02 pm


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Script for selecting, cropping the selection and saving the file
PostPosted: Tue Jun 14, 2022 8:56 am  (#1) 
Offline
New Member

Joined: Jun 12, 2022
Posts: 4
Hi,

I am working on a project of digitisation of documents in an archive. I am a beginner, I failed in an attempt to code a script to automate the process of cropping and saving an image file with Gimp --leaving a margin around the document image.

In other words, the desired automation would be, step by step:

1. Create a rectangular selection around the document image following the previously established guides.

Image

2. Increase this initial selection in x number of pixels to leave a margin around the document. I achieved to code an easy script for this particular task (in its code you can modify the margin changing the value 50 on the command steps). I copy the code of this script below this message.
3. Crop the selection.
4. Export the obtained image as a TIFF file format without any compression. Overwrite the existing file.


Many thanks for any help.



**margins.py**

#!/usr/bin/env python

from gimpfu import *

def margins(image, drawable):
#Inici de grup de funcions
pdb.gimp_image_undo_group_start(image)
#Afegeix-hi un marge de 0.6 cm, i.e. 71 pixels si dpi=300
steps = 50
pdb.gimp_selection_grow(image, steps)
#Costats no arrodonits
value = 0.0
toggle = 0
pdb.script_fu_selection_rounded_rectangle(image, drawable, value, toggle)
#Final de grup de funcions
pdb.gimp_image_undo_group_end(image)

register(
"python-fu-margins",
"Afegeix un marge a tot el volt de la imatge",
"Afegeix-hi un marge en pixels al comandament steps",
"", "", "2021",
"Margins",
"*",
[
(PF_IMAGE, "image", "takes current image", None),
(PF_DRAWABLE, "drawable", "Input layer", None)
],
[],
margins, menu="<Image>/Select")

main()


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Tue Jun 14, 2022 3:16 pm  (#2) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4750
IMHO you should better set options for the Crop tools (fixed size, and related values for instance). Then within a given Gimp session you re-use the same options (these options are carried from one image to the next, unlike guides). You can also save the options to re-user them in a future Gimp sessions, either as saving them as specific tool options with a name, or as default tool options.

Also, when handling many files, the most time consuming-part is opening files. I have an `ofn-file-next` script that saves the current image and opens the next in the same directory that is designed to improve this.

_________________
Image


Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Tue Jun 14, 2022 11:10 pm  (#3) 
Offline
GimpChat Member

Joined: Sep 26, 2015
Posts: 51
You should look into ImageMagick/GraphicsMagick. A command line tool(s) that can do image manipulations. As i recall it can crop images based on color thresholds. It may be more suitable for your project.


Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Wed Jun 15, 2022 10:09 am  (#4) 
Offline
New Member

Joined: Jun 12, 2022
Posts: 4
Yes, I remember that my first attempt months ago to make this work faster was to test the autocrop with ImageMagick based on color thresholds, but the obtained results were a bit inaccurate (for the digital preservation of unbound manuscripts our goal is to leave at least an 1/4-inch margin, as established by The Library of Congress in: Technical Standards for Digital Conversion of Texts and Graphic Materials, "2.4.1. Cropping. Cropping of bound and manuscript materials", page 15). Anyway, thanks AdmFubar for your suggestion.

On the other hand, in our case I think it's not possible to work with a prefixed values on the crop tool, because it's about an archive where almost each piece of paper has a particular form and size (the producer of that archive was a poet without means to purchase paper amid the Franco's dictatorship, so he used any leaf he found to write his work). Anyway, thanks Ofnuts for your proposal. Actually, for the rare cases such as the example illustrated in my previous message (so the example was no good, in this project the unbound leaves are the most common documents), I use prefixed guides thanks to the script GIMP_save_load_guides.py created by Tin Tran.

Also, I tested your script ofn-file-next.py, but it produces an error: I applied it on a first file (DIG_A-MMI-03551_0001_r.tif) of a folder that contains the files DIG_A-MMI-03551_0001_r.tif, DIG_A-MMI-03551_0002_r.tif, DIG_A-MMI-03551_0002_v.tif, DIG_A-MMI-03551_0003_r.tif, and so on. The script saves the current file correctly (DIG_A-MMI-03551_0001_r.tif), but it can't quit the file and appears the following message in the Error Console:

Image


Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Wed Jun 15, 2022 3:07 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Jan 13, 2011
Posts: 2260
Location: Poland
My suggestion is to use two script-fu (required to run - are added in appendix and updated for Gimp-2.10):
1. Export Sequential_processing.scm by Rob Antonishen viewtopic.php?f=9&t=4846&start=0&hilit=Sequential_processing
2. Guides-to-selection-mk2.scm by SteveMi viewtopic.php?f=9&t=14565&hilit=guides+to+selection&start=20#p201330

As many as 8 lines of code came out of it. :hehe
Test it on a small batch copy and let me know if it is right for You.


Attachments:
Test Margins.zip [4.67 KiB]
Downloaded 36 times
GtS1.jpg
GtS1.jpg [ 21.57 KiB | Viewed 1830 times ]
GtS.jpg
GtS.jpg [ 23.85 KiB | Viewed 1830 times ]

_________________
Image

Slava
Ukraini!
Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Thu Jun 16, 2022 1:21 am  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4750
lmbgimp wrote:

Also, I tested your script ofn-file-next.py, but it produces an error: I applied it on a first file (DIG_A-MMI-03551_0001_r.tif) of a folder that contains the files DIG_A-MMI-03551_0001_r.tif, DIG_A-MMI-03551_0002_r.tif, DIG_A-MMI-03551_0002_v.tif, DIG_A-MMI-03551_0003_r.tif, and so on. The script saves the current file correctly (DIG_A-MMI-03551_0001_r.tif), but it can't quit the file and appears the following message in the Error Console:

[ Image ]


What is the full path to the directory that contains the files?

_________________
Image


Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Tue Jun 21, 2022 8:41 am  (#7) 
Offline
New Member

Joined: Jun 12, 2022
Posts: 4
Sorry for the delay, these days I am sick. I will answer the other pending questions when I will get better.

Ofnuts, the full path of the directory that contains the files in my testing is: F:/a.mmi/cc/

The disk storage shown as F: is an external hard drive. Thanks.


Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Sun Jul 03, 2022 9:21 am  (#8) 
Offline
New Member

Joined: Jun 12, 2022
Posts: 4
Many many thanks MareroQ!

Finally this week I could analyse your plugin, as a beginner I learned a bit more about some aspects that I didn't know about the plugins of Gimp thanks to your work.

:tyspin The encoding declaration #-*- coding:utf-8 -*- in the second line of the plugin, that enables the use of words in Catalan language, is a true gift for me. I had the problem that the plugins didn't run for example if I introduced comment lines in Catalan, and I didn't know any way to do it.

This week I rewrote the plugin to adapt it better to the needs of the digitisation project where I am working for: as I always apply the same margin before cropping an image, I prefer to set the number of margin pixels in the code. On the other hand, I have chosen the plugin save-export-clean.py created by Akkana Peck as an alternative way of exporting the file.

marginscropwrite.py
#!/usr/bin/env python
#-*- coding:utf-8 -*-

# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# To view a copy of the GNU General Public License

# The plugin requires the following script-fu to be installed:
# Guides-to-selection-mk2.scm by SteveMi

# The plugin requires the following plugin to be installed:
# save-export-clean.py by Akkana Peck

# The plugin requires a previous selection made with four guides, i.e. two vertical guides and two horizontal guides

# The plugin applies a margin defined in pixels --see the variable steps in the code below--
# on a selection established by means of four guides, and then crop the image on the margin
# and finally export the file thanks to the plugin save-export-clean.py by Akkana Peck.
# The plugin save-export-clean.py marks the image clean, so use it with caution: this will
# overwrite your work and GIMP won't warn you when you exit.

from gimpfu import *

def marginscropwrite(image, drawable):     
    pdb.gimp_image_undo_group_start(image)
    pdb.script_fu_guides_to_selection_mk2(image, drawable, 0)
    #Add 20 pixels of margin
    steps = 20
    pdb.gimp_selection_grow(image, steps)   
    value = 0.0
    toggle = 0
    pdb.script_fu_selection_rounded_rectangle(image, drawable, value, toggle)
    pdb.gimp_selection_invert(image)
    pdb.gimp_drawable_edit_clear(drawable)
    pdb.plug_in_autocrop(image, drawable)
    pdb.script_fu_guides_remove(image, drawable)
    pdb.python_fu_export_clean(image, drawable)
    pdb.gimp_image_undo_group_end(image)
   

register(
    "python-fu-marginscropwrite",
    "Crop the image to the extents of a selection made with four guides, leaving a margin from the selection, and overwrite the file",
    "Define a margin in pixels in the variable steps to be applied to a previous selection made with four guides",
    "MrQ/lmbgimp", "", "2022",
    "Crop to Selection with a Margin and Overwrite",
    "*",
    [
        (PF_IMAGE, "image", "takes current image", None),
        (PF_DRAWABLE, "drawable", "Input layer", None)
    ],
    [],
    marginscropwrite, menu="<Image>/Image")

main()



1. The first step is open many files at once. I.e.: File > Open... and select many files in the Open Image window (with the Single-Window Mode of Gimp enabled).

2. Select the limits of the document with the Rectangle Select Tool and then convert the selection into guides with the option Image > Guides > New Guides from Selection. To automate the next step of removing the rectangular selection, you can substitute the standard option New Guides from Selection for an easy plugin to make containing the procedures pdb.script_fu_guides_from_selection(image, drawable) and pdb.gimp_selection_none(image)

3. Move the four guides to adjust them to the limits of the document thanks to the zoom of Gimp.

Image

4. Execute the plugin marginscropwrite.py (a) to apply a margin of 20 pixels to the previous selection, (b) crop the image to this margin (c) and overwrite the file. You can execute the installed plugin marginscropwrite.py in: Image > Crop to Selection with a Margin and Overwrite


Last edited by lmbgimp on Sun Jul 03, 2022 12:05 pm, edited 7 times in total.

Top
 Post subject: Re: Script for selecting, cropping the selection and saving the file
PostPosted: Sun Jul 03, 2022 11:43 am  (#9) 
Offline
GimpChat Member
User avatar

Joined: Jan 13, 2011
Posts: 2260
Location: Poland
I'm glad you managed to personally customize the plug to your needs. ;)

_________________
Image

Slava
Ukraini!


Top
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Is there a script for doing Stroke outside selection?

4

No new posts Cropping

4

No new posts Attachment(s) Seeking Help with First Multi-File Batch Script

9

No new posts Attachment(s) Cropping an image

6

No new posts Selecting outline of image

2



* Login  



Powered by phpBB3 © phpBB Group