-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-fu-clip-page.py
executable file
·113 lines (100 loc) · 4.06 KB
/
python-fu-clip-page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
from gimpfu import *
import sys
import Tkinter
import tkMessageBox
import os
import time
sep = os.path.sep
def do_preprocessing(fileName) :
#print("do_preprocessing "+fileName)
sys.stdout.flush()
theImage = pdb.gimp_file_load(fileName, fileName)
pdb.gimp_image_undo_enable(theImage)
theActiveLayer = pdb.gimp_image_get_active_layer(theImage)
pagedir, filename, fileNo, fileExt = get_filename_parts(fileName)
selection_op = 2
if (fileNo%2 != 0) :
pdb.gimp_image_rotate(theImage, 1)
pdb.gimp_rect_select(theImage, 108, 129, 2150, 3120, selection_op, 0, 0)
else :
pdb.gimp_rect_select(theImage, 283, 231, 2150, 3120, selection_op, 0, 0)
displayId = pdb.gimp_display_new(theImage)
return (theImage, displayId)
def do_postprocessing(theImage, fileName, displayId) :
#print("do_postprocessing "+fileName)
sys.stdout.flush()
nonEmpty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(theImage)
if (nonEmpty == 1) :
theActiveLayer = pdb.gimp_image_get_active_layer(theImage)
x0,y0 = pdb.gimp_drawable_offsets(theActiveLayer)
pdb.gimp_image_crop(theImage,x2-x1,y2-y1,x1,y1)
pdb.script_fu_guides_remove(theImage, theActiveLayer)
pagedir, filename, fileNo, fileExt = get_filename_parts(fileName)
saveFile = pagedir + sep + str(fileNo).zfill(4) + ".xcf"
pdb.gimp_xcf_save(0, theImage, theActiveLayer, saveFile, saveFile)
pdb.gimp_display_delete(displayId)
def get_filename_parts(fileName) :
pagedir, localName = os.path.split(fileName)
fileNo, fileExt = os.path.splitext(localName)
fileNo = int(fileNo)
return (pagedir, localName, fileNo, fileExt)
def start_pages(firstFile, oneShot) :
last = 0;
root = Tkinter.Tk()
root.wm_withdraw()
root.attributes('-topmost', 1) # Raising root above all other windows
root.attributes('-topmost', 0)
pagedir, filename, fileNo, fileExt = get_filename_parts(firstFile)
nextFileLoc = pagedir + sep + str(fileNo).zfill(4) + fileExt
#print("start file: " + nextFileLoc)
while os.path.exists(nextFileLoc) :
#print("file "+firstFile)
tmpName = str(fileNo).zfill(4)
theImage, displayId = do_preprocessing(nextFileLoc)
pdb.gimp_progress_set_text("Ok 'Gimp Continue' popup on file "+tmpName+".")
time.sleep(1)
tkMessageBox.showinfo(title="GIMP Continue", message="Adjust selection for page "+
tmpName +" and OK when complete.")
do_postprocessing(theImage, nextFileLoc, displayId)
fileNo += 1
tmpName = str(fileNo).zfill(4)
nextFileLoc = pagedir + sep + tmpName + fileExt
sys.stdout.flush()
if (oneShot) :
nextFileLoc = ""
#print("all done")
root.destroy()
#Use gimpshelf for persistence across individual script invocation.
#Use parasite for custom data storage in xcf artifacts.
#See /usr/lib/gimp/2.0/python/gimpfu.py
register(
"python-fu-clip-page", #keyboard shortcut
"Workflow for jpg pages to xcf. Iterates over the jpg\
files in a directory. The files must be 4 digit eg 0001.jpg.\
For each file:\n\
Rotate 180 degrees odd numbered pages.\n\
Insert a rectangular selector.\n\
Allow custom adjustment.\n\
Clip the image to the selection.\n\
Save the image to an xcf.\n\
Open the next image.\n\n\
Note: Click on the popup after each file adjustment.\n\
", #tool tip
"Clean an xcf image.", #description
"Daren Isaacs", #author
"Open source (BSD 3-clause license)", #licence
"2020", #year
"python-fu-clip-page", #Label for menu
#prepend <Image><Load><Save><Toolbox>
#or <> will error with the options, also see gimpfu.py
"", #image type
[ (PF_FILE, "file", "The file of the first page", "none"),
(PF_TOGGLE, "oneshot", "Process just a single file.", 1)
], #input parameters
[ #output parameters
],
start_pages, #function callback name
"<Toolbox>/Custom" #menu
)
main()