-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBookCrop.ahk
963 lines (777 loc) · 27.1 KB
/
BookCrop.ahk
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#singleinstance, force
#Persistent
SetBatchLines -1
info =
(C
;BookCrop
Quickly crop many same sized images via an overlay preview
version 2018-10-21
By Nod5
Free Software GPLv3
AutoHotkey
Made in Windows 10
HOW TO USE
1. Drag and drop jpeg/tif images or a folder with images
2. BookCrop overlays them into one preview
3. Draw a rectangle and click "crop" to crop all images
SETUP
- Install latest GraphicsMagick (Q8 version is faster)
- Get two files from libjpeg-turbo:
- Download libjpeg-turbo-1.5.3-gcc64.exe or newer
- Unzip the .exe with 7zip
- browse to \bin subfolder
- copy jpegtran.exe and libjpeg-62.dll and place next to BookCrop.exe
MORE FEATURES
Ctrl+Click and draw another rectangle to split crop into
two images with L and R suffix.
Ctrl+Tab starts R L mode
Files ending with L.jpg and R.jpg are previewed and cropped separately.
R/L at preview: rotate preview in 90 degree steps.
S at preview: Scrub mode
Draw a rectangle to whiten that area in binarized tif input images.
Useful for scrubbing noise near inner/outer edges on book page photos.
Command line input: a folder path or image filepaths or a .txt with
one image filepath per line.
Drop a folder: Process all jpeg or tif in folder (whichever there is more of).
Drop a single jpeg or tif: Quick preview. Move threshold slider to refresh.
Change threshold in quick preview if the overlay is too dark or light.
Important: Preview and cropping only works well on same size input images.
Advice: Use crop to subfolder, so you can redo if you crop too much.
)
wintitle = BookCrop
;variable inventory
; x1 y1 x2 y2 = crop rectangle top left and bottom right corners:
; screenx1 = x1 relative to screen top left
; picx1 = x1 relative to gui pic top left
; x1 = x1 relative to gui pic top left
; extra_x1 = ... for extra second rectangle
; x y w h = upscaled, relative to full image top left
; extra_x ... = ... for extra second rectangle
; x1 in LetUserSelectRect() = (local) ongoing rect top left relative to screen
;gui to drag drop images onto
Gui,6: font, s8 cgray
Gui,6: Add, Text,x290 y345 gsplash, ?
Gui,6: font, s12 bold
Gui,6: Add, GroupBox, x5 y2 w290 h300
if A_IsCompiled
Gui,6: Add, Picture,x130 y75, %A_ScriptName% ;use script icon
Gui,6: Add, Text,x78 y125 h130 w200 vtxt,Drop .jpg folder
Gui,6: Show,h360 w300 y250,%wintitle%
checkini()
if splash = 1
goto splash
;default to crop mode
crop_scrub := "Crop"
;parse command line parameters
if A_Args[1]
{
;textfile with linebreak separated image paths
If ( SubStr(A_Args[1], -3) == ".txt" ) and FileExist(A_Args[1])
{
txtfile := A_Args[1]
FileEncoding, UTF-8
FileRead, params, *t %txtfile% ;*t formats `n`r into `n
goto param_started
}
;else add each param to list
Else If InStr( FileExist(A_Args[1]), "D" ) ;folder
{
params := A_Args[1]
goto param_started
}
;else make list of params
for key, value in A_Args
params .= value "`n"
goto param_started
}
;function: read ini settings or create default ini if none exists
checkini() {
global
xini = %A_ScriptFullPath%.ini
ifnotexist, %xini%
{
xinitext =
(Ltrim
[%wintitle%]
splash=1
subdir=1
scrubdir=1
threshold=20
)
FileAppend, %xinitext%, %xini%
}
;settings from ini or default
xval = splash,subdir,scrubdir,threshold ;default: 1,1,1,20
Loop, Parse, xval,`,
IniRead, %A_LoopField%, %xini%, %wintitle%, %A_LoopField%, 1
threshold := threshold > 1 and threshold <= 99 ? threshold : 20
}
#IfWinActive, BookCrop ahk_class AutoHotkeyGUI
;toggle R L mode
^Tab::
GuiControlGet,xt,6:,txt
txt := InStr(xt,"R L") ? "Drop .jpg folder" : "Drop .jpg folder`nto crop R L suffixed`nimages separately"
GuiControl,6:, txt, %txt%
return
;toggle scrub mode at preview stage
s::
if blockshift or (xext = "jpg")
return
GuiControlGet, crop_scrub,5:, crop
crop_scrub := crop_scrub == "Crop" ? "Scrub" : "Crop"
GuiControl, 5:, crop, %crop_scrub% ;toggle button text
GuiControl, 5: Move, crop, % (crop_scrub == "Crop") ? "w70" : "w170"
return
;rotate images before crop
r::
l::
;only rotate at preview stage
GuiControlGet,button_control, 5:, Button1
if !button_control
return
;degrees to apply in this rotation
deg := a_thislabel == "r" ? 90 : -90
;total rotation after this. used later to rotate img before crop
totrot += deg
totrot := totrot<0 ? totrot+360 : totrot>360 ? totrot-360 : totrot ;0 90 180 270
;case1: rotated pic height won't fit inside screen height -> shrink preview height to fit
;case2: previous rotation involved height shrink of preview -> use original preview backup
;case3: rotated height fits and no previous height shrink -> rotate
;case1: pic will not fit screen height, so shrink it
if (pic_w > A_ScreenHeight-145)
{
;backup preview, to use if rotate again
If !FileExist(overlay "_orig.png")
{
FileCopy, % overlay, % overlay "_orig.png", 1
;backup dimensions
orig_pic_h := pic_h , orig_pic_w := pic_w
}
;new height
pic_h := A_ScreenHeight-145
;extra proportion used later for extra upscale
rot_prop := pic_h/pic_w
;rotate and resize
;note: -geometry xH makes gm pick a width that keeps aspect ratio
Runwait "%gm%" convert "%overlay%" -rotate %deg% -geometry x%pic_h% "%overlay%" ,,hide
;get new width of resized preview
rot_Img := ComObjCreate("WIA.ImageFile")
rot_Img.LoadFile(overlay)
pic_w := rot_Img.Width
}
;case2: previous rotate did height shrink
else if rot_prop
{
;assume this step rotates back into original preview or its 180 degree mirror
;restore backup
FileCopy, % overlay "_orig.png", % overlay, 1
;use totrot here since we're operating on the original preview image
if (totrot != 360)
Runwait "%gm%" convert "%overlay%" -rotate %totrot% "%overlay%" ,,hide
;reuse original vars
pic_h := orig_pic_h , pic_w := orig_pic_w
;clear extra rot_prop, since were back at original preview size
rot_prop := ""
}
;case3: regular rotate
else
{
Runwait "%gm%" convert "%overlay%" -rotate %deg% "%overlay%" ,,hide
;flip vars
stemp := pic_h , pic_h := pic_w, pic_w := stemp
}
;msgbox % "pic_h=" pic_h " | pic_w=" pic_w " | totrot=" totrot " | rot_prop=" rot_prop
x1 =
;remove all gui
Loop, 14
Gui, %A_Index%: destroy
makegui() ;global
return
;show help window
Tab:: goto splash
#IfWinActive
;help window
splash:
WinGetPos,mainx,mainy, mainw,, %wintitle%
mainx += mainw
Gui 7:+LastFoundExist
IfWinExist
{
;close help window if already open
gui,7: destroy
return
}
Gui, 7: +ToolWindow -SysMenu -Caption -resize +AlwaysOnTop +0x800000 -DPIScale
Gui, 7: Font, bold s12
Gui, 7: Add, Text,, %wintitle%
Gui, 7: Font, normal s9
Gui, 7: Add, Text,, %info%
Gui, 7: Add, Checkbox,xm Checked%splash% section vsplashbox gsplashbox, show help on startup
Gui, 7: Add, Checkbox,xm Checked%subdir% section vsubdirbox gsubdirbox, crop to subfolder
Gui, 7: Add, Checkbox,xm Checked%scrubdir% section vscrubdirbox gscrubdirbox, scrub to subfolder
Gui, 7: Add, text, yp+30 x20 w55 h15,threshold
Gui, 7: Add, text, vtext yp xp+47 w20 h15,%threshold%
Gui, 7: Add, Slider, NoTicks yp+15 x11 w180 h20 vslider gslider, %threshold%
Gui, 7: Add, Link,ys xm+200,<a href="https://github.com/nod5/BookCrop">github.com/nod5/BookCrop</a>
Gui, 7: Add, Link,yp+20 xm+200,<a href="https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick-binaries/">graphicsmagick.org</a>
Gui, 7: Add, Link,yp+20 xm+200,<a href="https://sourceforge.net/projects/libjpeg-turbo/files/">libjpeg-turbo</a>
Gui, 7: show, x%mainx% y%mainy%
return
;help window slider for preview binarization threshold
;single image preview mode: update preview on each slider drag
;multi image preview mode: apply threshold change next time images are dropped
slider:
Gui, Submit, NoHide
IniWrite, %slider%, %xini%, %wintitle%, threshold
threshold := slider
GuiControl,,text, %slider%
if !single_image_mode
return
if !FileExist(gm)
;check graphicsmagick and jpegtran, return full gm.exe path
gm := checkpaths()
if !FileExist(gm)
return
;make new threshold binarized preview
RunWait "%gm%" convert -size %pic_w%x%pic_h% "%firstfile%" -sample %pic_w%x%pic_h% -threshold %threshold%`% -transparent white -flatten "%overlay%" ,,hide
Gui,5: destroy
makegui() ;global
return
7GuiEscape:
gui,7: destroy
return
;helpwin checkboxes: write change to ini
splashbox:
Gui, Submit, NoHide
IniWrite, %splashbox%, %xini%, %wintitle%, splash
return
subdirbox:
Gui, Submit, NoHide
IniWrite, %subdirbox%, %xini%, %wintitle%, subdir
subdir := subdirbox
return
scrubdirbox:
Gui, Submit, NoHide
IniWrite, %scrubdirbox%, %xini%, %wintitle%, scrubdir
scrubdir := scrubdirbox
return
;file drop event
6GuiDropFiles:
5GuiDropFiles:
param_started:
arr_R := "", arr_L := ""
single_image_mode := ""
if !FileExist(gm)
;check graphicsmagick and jpegtran, return full gm.exe path
gm := checkpaths()
if !FileExist(gm)
return
xext =
;files from parameter or dropped
dropped := params ? params : A_GuiEvent
Loop, parse, dropped, `n
{
if (a_index == 1)
firstfile := A_LoopField
if (a_index == 2)
secondfile := A_LoopField
if (a_index == 2)
Break
}
FileGetAttrib, xattrib, %firstfile%
SplitPath, firstfile,,xdir,xext
SplitPath, secondfile,,xdir2,xext2
if xattrib not contains D ;no directory
if xext not in jpg,tif,tiff ;no jpg or tif
return
if xattrib contains D
;directory -> use it, but trim end slash
xdir := SubStr(firstfile, 0) == "\" ? SubStr(firstfile,1,-1) : firstfile
overlay := xdir "\" A_scriptname "_over.png" ;overlay filepath
;remove old overlay
FileDelete, %overlay%
FileDelete, %overlay%_orig.png
Gui,5: destroy
Gui,6: destroy
Gui,7: destroy
;single image mode -> preview mode for threshold adjustment, no cropping
if !secondfile
if xext in jpg,tif,tiff
{
getdim(firstfile, prop, pic_w, pic_h, imgw, imgh) ;ByRef returns
;make single image preview
RunWait "%gm%" convert -size %pic_w%x%pic_h% "%firstfile%" -sample %pic_w%x%pic_h% -threshold %threshold%`% -transparent white -flatten "%overlay%" ,,hide
single_image_mode := 1
makegui() ;global
return
}
;multi image mode
at := Object() , aj := Object() ;tiff or jpg array
;image drop -> overlay all dropped tif or jpg
;firstfile and secondfile is jpg or tif
if xext in jpg,tif,tiff
if xext2 in jpg,tif,tiff
{
Loop, parse, dropped, `n
{
SplitPath, A_LoopField,,,tempext
if (tempext == "tif" or tempext == "tiff")
at.Insert(A_LoopField)
if (tempext == "jpg")
aj.Insert(A_LoopField)
}
}
;folder drop -> overlay all tif or jpg in folder
if InStr(xattrib, "D")
{
Loop, Files, %xdir%\*.jpg
aj.Insert(A_LoopFileFullpath)
Loop, Files, %xdir%\*.tif
at.Insert(A_LoopFileFullpath)
Loop, Files, %xdir%\*.tiff
at.Insert(A_LoopFileFullpath)
}
;check array
if (aj.MaxIndex() <= 1 and at.MaxIndex() <= 1)
return
;process most common image type
xext := aj.MaxIndex() >= at.MaxIndex() ? "jpg" : "tif"
;arr_do is most common image array
arr_do := aj.MaxIndex() >= at.MaxIndex() ? aj : at
;make R L arrays based on filename R L suffix
arr_R := Object() , arr_L := Object()
if InStr(txt,"R L") ;R L mode (as shown in gui text)
{
for key, val in arr_do
{
SplitPath, val,,,valext
if SubStr(val,"-" StrLen(valext)+1) == "R." xext ;R.jpg
arr_R.Insert(val)
else if SubStr(val,"-" StrLen(valext)+1) == "L." xext ;L.jpg
arr_L.Insert(val)
}
if !arr_R.MaxIndex() or !arr_L.MaxIndex() ;R or L array empty
{
msgbox error: R or L files missing!
reload
}
arr_do := arr_L ;do L array first
}
;CREATE OVERLAY
; notes:
; binarize with transparent white
; mpc format is faster than jpg
; run many jobs to max out cpu cores
rightside:
;jump back here later to do R side when in in R L mode
;assume first file's dimensions for all
getdim(arr_do[1], prop, pic_w, pic_h, imgw, imgh) ;ByRef returns
;number of images in batch
xcount := arr_do.MaxIndex()
SetTimer, prog, 200
Progress, 0,, Creating overlay,%wintitle%
;for speedtest
tick := A_TickCount
for key, imgpath in arr_do
{
;resize binarize to mpc files
Run "%gm%" convert -size %pic_w%x%pic_h% "%imgpath%" -sample %pic_w%x%pic_h% -threshold %threshold%`% -transparent white "%imgpath%_%A_scriptname%_temp.mpc" ,,hide
sleep 10
}
;wait until all jobs end
while processExist("gm.exe")
sleep 100
;flatten all mpc into one overlay image
;note: very fast, no need to optimize
Runwait "%gm%" convert "%xdir%\*_%A_scriptname%_temp.mpc" -flatten "%overlay%" ,,hide
FileDelete, %xdir%\*_%A_scriptname%_temp.mpc
FileDelete, %xdir%\*_%A_scriptname%_temp.cache
tick := A_TickCount - tick
SetTimer, prog, Off
progress, off
;show overlay preview gui
makegui() ;global
;msgbox overlay time = %tick% ms ;speedtest
return
;progress timer
prog:
Loop, %xdir%\*.mpc
mpc_count := a_index
prog := ( mpc_count / xcount ) * 100
progress, %prog%
return
;user clicks on overlay preview pic
pic:
if single_image_mode
return
;if Control is pressed and selection1 exists, do selection2
xcontr := GetKeyState("Control") == 1 and x1 ? 1 : ""
;get vars for transform from screen relative to pic relative x/y
;pic position relative to screen
WinGetPos, px,py,pw,ph, ahk_id %pichWnd%
edgex1 := px ;pic left edge relative to screen
edgey1 := py ;pic top edge
edgex2 := px + pw ;pic right edge
edgey2 := py + ph ;pic low edge
;Draw rectangle as mouse moves. Return rectangle on Lbutton release.
;returns via ByRef
;returns rect corners relative to screen
LetUserSelectRect(screenx1, screeny1, screenx2, screeny2)
;cancel if no rectangle was made
if (screenx1 == screenx2 or screeny1 == screeny2)
{
Loop, 4
Gui, %xcontr%%A_Index%: destroy ;clear this selection
return
}
;rect corners relative to pic top left
picx1 := screenx1 - edgex1 , picy1 := screeny1 - edgey1
picx2 := screenx2 - edgex1 , picy2 := screeny2 - edgey1
if (xcontr = 1)
;set selection2 crop vars
extra_x1:=picx1, extra_x2:=picx2, extra_y1:=picy1, extra_y2:=picy2
else
;set selection1 crop vars
x1:=picx1, x2:=picx2, y1:=picy1, y2:=picy2, extra_x1:="", extra_y1:="", extra_x2:="", extra_y2:=""
;line width
r:=2
;show rectangle on preview pic
;note: x y relative to parent
Gui, %xcontr%1:Show, % "NA X" picx1 " Y" picy1 " W" picx2-picx1 " H" r
Gui, %xcontr%2:Show, % "NA X" picx1 " Y" picy2-r " W" picx2-picx1 " H" r
Gui, %xcontr%3:Show, % "NA X" picx1 " Y" picy1 " W" r " H" picy2-picy1
Gui, %xcontr%4:Show, % "NA X" picx2-r " Y" picy1 " W" r " H" picy2-picy1
GuiControl,5: Enable, crop ;enable crop button
ControlFocus, Button1, ahk_id %MainhWnd% ;remove focus from the rectangle guis
return
;CROP OR SCRUB IMAGES
;based on user drawn rect
crop:
;prevent s from toggling crop/scrub mode below this line
blockshift := 1
if !x1
;no selection
return
;for speedtest
tick := A_TickCount
;make image crop values by upscaling from selection rect
;pic was shrunk extra when rotating?
if rot_prop
prop := prop * rot_prop
;rectangle
x :=round(x1/prop) , y :=round(y1/prop)
;flip imgw imgh if rotation from portrait to landscape (or vice versa)
if isOdd( totrot / 10) ;90 270 450 ... -90 -270 -450 ...
stemp := imgw, imgw := imgh, imgh := stemp
;force crop area to be within imgw imgh bounds, else jpegtran fail
w := round( (x2-x1)/prop ) + x > imgw ? imgw - x : round( (x2-x1)/prop )
h := round( (y2-y1)/prop ) + y > imgh ? imgh - y : round( (y2-y1)/prop )
;extra rectangle
if extra_x1
{
extra_x :=Round(extra_x1/prop), extra_y :=Round(extra_y1/prop)
;force crop area within imgw imgh bounds, else jpegtran fail
extra_w :=Round( (extra_x2-extra_x1)/prop ) + extra_x > imgw ? imgw : Round( (extra_x2-extra_x1)/prop )
extra_h :=Round( (extra_y2-extra_y1)/prop ) + extra_y > imgh ? imgh : Round( (extra_y2-extra_y1)/prop )
}
;prepare for cropping
if extra_x
;split crop to two images
xcount *= 2
Progress, 0,, Cropping,,
settimer, progout, 600
xtimestart := A_now
;crop/scrub to subfolder or overwrite?
if (crop_scrub == "Crop" and subdir) or (crop_scrub == "Scrub" and scrubdir)
{
;crop to subfolder
;default mode
if (arr_do != arr_R) and (arr_do != arr_L)
{
xoutdir := xdir "\" xtimestart
FileCreateDir, % xoutdir
}
;crop only L side images this time
if (arr_do = arr_L)
{
;subfolder
xoutdir := xdir "\" xtimestart
FileCreateDir, % xoutdir
}
;crop only R side images this time
if (arr_do = arr_R)
{
;reuses subfolder from L side images crop
xoutdir := xoutdir
}
}
else
;no subdir, output to same folder as inputs
;note: this overwrites input images
xoutdir := xdir
;set crop tool based on image type
cropper := xext == "tif" ? gm : A_scriptdir "\jpegtran.exe"
;rotation argument
rot := !totrot ? "" : "-rotate " totrot
if totrot in 0,360,720,-360,-720
rot := ""
for key,imgpath in arr_do
{
SplitPath, imgpath,xname,,ext_this,xnoext
;scrub tif/tiff selection(s) white with graphicsmagick
; note:
; The -threshold step will cause scrub to fail if input .tif has JPEG compression
; We need -threshold since without it some 1bit .tif inputs will grow ~10x in size.
; note:
; To remove JPEG compression on .tif before input to BookCrop and Scrub:
; gm.exe convert <input tif> -compress none <output tif>
; http://www.graphicsmagick.org/GraphicsMagick.html#details-compress
if (xext == "tif" and crop_scrub == "Scrub")
{
xend := x + w , yend := y + h
xend2 := extra_x + extra_w , yend2 := extra_y + extra_h
if extra_x ;selection1 and selection2
Run "%cropper%" convert "%imgpath%" %rot% -fill white -draw "rectangle %X%`,%Y% %xend%`,%yend% rectangle %extra_x%`,%extra_y% %xend2%`,%yend2%" -threshold 50`% "%xoutdir%\%xname%" ,,hide
else ;selection1
Run "%cropper%" convert "%imgpath%" %rot% -fill white -draw "rectangle %X%`,%Y% %xend%`,%yend%" -threshold 50`% "%xoutdir%\%xname%" ,,hide
}
;crop tif/tiff with graphicsmagick
else if (xext == "tif")
{
if extra_x ;split crop R and L side
{
Run "%cropper%" convert %rot% -crop %extra_w%x%extra_h%+%extra_x%+%extra_y% "%imgpath%" "%xoutdir%\%xnoext%R.%ext_this%",,hide
Run "%cropper%" convert %rot% -crop %W%x%H%+%X%+%Y% "%imgpath%" "%xoutdir%\%xnoext%L.%ext_this%" ,,hide
}
else
Run "%cropper%" convert %rot% -crop %W%x%H%+%X%+%Y% "%imgpath%" "%xoutdir%\%xname%" ,,hide
}
;crop jpg with jpegtran
else if (xext == "jpg")
{
if extra_x ;split crop R and L side ;jpegtran -crop WxH+X+Y (X/Y = startpoints)
{
Run "%cropper%" %rot% -crop %extra_w%x%extra_h%+%extra_x%+%extra_y% -outfile "%xoutdir%\%xnoext%R.%ext_this%" "%imgpath%",,hide
Run "%cropper%" %rot% -crop %W%x%H%+%X%+%Y% -outfile "%xoutdir%\%xnoext%L.%ext_this%" "%imgpath%",,hide
}
else
Run "%cropper%" %rot% -crop %W%x%H%+%X%+%Y% -outfile "%xoutdir%\%xname%" "%imgpath%",,hide
}
sleep 10
}
SplitPath, cropper, cropperfilename
;wait for all gm.exe or jpegtran.exe jobs to end
while processExist(cropperfilename)
sleep 100
settimer, progout, off
progress, off
sleep 300
FileDelete, %overlay%
FileDelete, %overlay%_orig.png
;Check if any outputs are missing
arr_missing := []
for key, val in arr_do
{
SplitPath, val, filename, dir, ext, namenoext
;check if single output file is missing
if !extra_x or (crop_scrub = "Scrub")
if !FileExist(xoutdir "\" filename)
arr_missing.Insert(filename)
;check if R L suffixed split output files are missing
if extra_x and (crop_scrub != "Scrub")
if !FileExist(xoutdir "\" namenoext "R." ext)
arr_missing.Insert(namenoext "R." ext)
if extra_x and (crop_scrub != "Scrub")
if !FileExist(xoutdir "\" namenoext "L." ext)
arr_missing.Insert(namenoext "L." ext)
}
; Notes:
; In some cases some output images are not created.
; - .jpg crop error case:
; If input images differ in pixel dimensions then rectangle may be larger than the image.
; Workaround: use inputs with same dimensions or crop smaller.
; - .tif scrub error case:
; If input tif has JPEG compression then -threshold command will not work (see earlier note)
; Workaround: change from JPEG compression to none or other compression first.
;if outputs are missing: notify and reload
missing_count := arr_missing.MaxIndex()
expected_count := arr_do.MaxIndex()
missing_list := ""
if missing_count
{
for key, val in arr_missing
missing_list .= val "`n"
guess_crop := "If input images differ in pixel dimensions`nthen retry with a smaller crop rectangle."
guess_scrub := "If input .tif images have JPEG compression`nthen change/remove compression and retry Scrub."
error_guess := crop_scrub = "Scrub" ? guess_scrub : guess_crop
missing =
(LTrim
Error: Some output files could not be created.
%error_guess%
BookCrop will now restart.
Error creating %missing_count% out of %expected_count% files:
%missing_list%
)
msgbox % missing
reload
}
;if L side images was cropped, continue with R side
if (arr_do == arr_L)
{
arr_do := arr_R
totrot := ""
rot_prop := ""
;clear second rectangle data, avoids unwanted splitcrop with old second rect on R set
extra_x1:= extra_x2:= extra_y1:= extra_y2:= ""
extra_x:= extra_y:= extra_w:= extra_h:= ""
;clear block so that S hotkey works also at R set preview
blockshift := ""
Gui,5: destroy
Gui,6: destroy
Gui,7: destroy
goto rightside
}
;speedtest
tick := A_TickCount - tick
;msgbox crop time = %tick% ms
reload
return
;timer: progress bar while cropping
progout:
xtot = 0
Loop, %xoutdir%\*.%xext%
{
FileGetTime, xtime, %A_LoopFileFullPath% ;last mod time
if ( xtime > xtimestart ) ;if modified since crop started
xtot += 1 ;progress one step
}
prog := ( xtot / xcount ) * 100
progress, %prog%
return
6GuiClose:
5GuiClose:
FileDelete, %overlay%
FileDelete, %overlay%_orig.png
ExitApp
;function: test if input is Odd
isOdd(n){ ;return 1 if odd, else 0
return n&1
}
; FUNCTION: SHOW SELECTION RECTANGLE
; first corner set from mouse start position
; other corner tracks user mouse move
; click fixates second corner and returns screen relative rect corners
; note: x1 x2 y1 y2 are local vars for rect corners relative to screen
; they are ByRef returned into screenx1 screenx2 ...
; based on LetUserSelectRect function by Lexikos
; www.autohotkey.com/community/viewtopic.php?t=49784
LetUserSelectRect(ByRef X1, ByRef Y1, ByRef X2, ByRef Y2)
{
CoordMode, Mouse, Screen
static r := 2 ;line thickness
global xcontr
Loop 4
{
if !xcontr ;clear sel2 if new sel1
Gui, 1%A_Index%: destroy
Gui, %xcontr%%A_Index%: destroy ;clear same type sel
Gui, %xcontr%%A_Index%: -Caption +ToolWindow +AlwaysOnTop -DPIScale
Gui, %xcontr%%A_Index%: Color, Red
if !xcontr
GuiControl,5: Disable, crop
}
if GetKeyState("Lbutton", "P") == "U"
return ;user already released button (quick click)
MouseGetPos, xo, yo ;first click position
SetTimer, lusr_update, 10 ;selection rectangle update timer
KeyWait, LButton ;wait for LButton release
SetTimer, lusr_update, Off
global MainhWnd
Loop 4 ;make child
Gui, %xcontr%%A_Index%: +Parent%MainhWnd%
return
lusr_update:
CoordMode, Mouse, Screen
MouseGetPos, x, y
;flip x1/x2 y1/y2 if negative rect draw
y1 := y<yo ? y:yo , y2 := y<yo ? yo:y
x1 := x<xo ? x:xo , x2 := x<xo ? xo:x
;pic edges relative to screen
global edgex1, edgey1, edgex2, edgey2
;bound draw at pic edges
x1 := x1 < edgex1 ? edgex1:x1 , x2 := x2>edgex2 ? edgex2:x2
y1 := y1 < edgey1 ? edgey1:y1 , y2 := y2>edgey2 ? edgey2:y2
; Update selection rectangle ;gui 1 2 3 4 (sel 1) or 11 12 13 14 (sel 2)
Gui, %xcontr%1:Show, % "NA X" x1 " Y" y1 " W" x2-x1 " H" r
Gui, %xcontr%2:Show, % "NA X" x1 " Y" y2-r " W" x2-x1 " H" r
Gui, %xcontr%3:Show, % "NA X" x1 " Y" y1 " W" r " H" y2-y1
Gui, %xcontr%4:Show, % "NA X" x2-r " Y" y1 " W" r " H" y2-y1
return
}
;function: check that dependencies exist and get graphicsmagick gm.exe path
checkpaths() {
;search program folders, since gm updates can break registry lookup method
prog := StrReplace(A_ProgramFiles, " (x86)", "") ;Program Files path in any locale
Loop, Files, %prog%\GraphicsMagick* , D
binpath := A_LoopFileFullPath
Loop, Files, %prog% (x86)\GraphicsMagick* , D
binpath := A_LoopFileFullPath
if !FileExist(binpath "\gm.exe")
msgbox, Error: GraphicsMagick not found.`nInstall it and try again.
If !FileExist(A_Scriptdir "\jpegtran.exe") or !FileExist(A_Scriptdir "\libjpeg-62.dll")
msgbox, Error: jpegtran.exe and/or libjpeg-62.dll not found.`nPlace them next to BookCrop.
If FileExist(binpath "\gm.exe") and FileExist(A_Scriptdir "\jpegtran.exe") and FileExist(A_Scriptdir "\libjpeg-62.dll")
return % binpath "\gm.exe"
}
;function: get image source dimensions and calculate gui pic dimensions
getdim(xdimfile, ByRef prop, ByRef pic_w, ByRef pic_h, Byref imgw, Byref imgh) {
Img := ComObjCreate("WIA.ImageFile")
Img.LoadFile(xdimfile)
;image dimensions
imgw := Img.Width , imgh := Img.Height
;try: fit image pic to screen height
pic_h := A_ScreenHeight-145
;exact proportion, used later to upscale rectangle before crop
prop := pic_h/imgh
pic_w := imgw*prop
;if too wide, fit pic to screen width instead (landscape image)
pic_wmax := A_ScreenWidth-100
if pic_w > pic_wmax ;if too wide then fit pic to screen width instead
pic_w := A_ScreenWidth-100, prop := pic_w/imgw, pic_h := imgh*prop
;pic dimensions
pic_h := Round(pic_h), pic_w := Round(pic_w)
}
;function: check if process exist
processExist(im){
process,exist,% im
return errorLevel ;PID or 0
}
;function: make new overlay preview window
makegui() {
global
Gui,5: destroy
Gui,6: destroy
pic_h := pic_h < 50 ? A_screenheight-100 : pic_h
pic_w := pic_w < 50 ? A_screenwidth : pic_w
Gui,5: -DPIScale
Gui,5: margin,0,0
Gui,5: font, s12 bold
hbut := pic_h+5
htot := pic_h+40
xspl := pic_w-10
Gui,5: Add, Button, x100 y%hbut% vcrop gcrop, Crop
GuiControl,5: Disable, crop ;disabled until selection1
Gui,5: font, s8 cgray norm
Gui,5: Show,w%pic_w% h%htot%,%wintitle%
Gui,5: Add, Text,x%xspl% yp+10 gsplash, ? ;helpwin button
Gui,5: +LastFound
MainhWnd := WinExist()
;pic overlay child window
Gui, 6: margin,0,0
Gui, 6: Add, Pic, vpic gpic, %overlay%
Gui, 6: +Owner -Caption -SysMenu -resize +ToolWindow +0x800000 -DPIScale
Gui, 6: Show, x0 y0
Gui, 6: +LastFound
pichWnd := WinExist()
Gui, 6: +Parent%MainhWnd%
}