Last active
October 8, 2023 10:55
-
-
Save kuanyui/7a711333966967cc0d7023bff9848c20 to your computer and use it in GitHub Desktop.
(Perl + ImageMagick) Crop image into right/left side inside bash directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perl -e ' | |
use strict; | |
use warnings; | |
use File::Basename; | |
my $SRCDIR = "ori02/"; | |
my $OUTDIR = "out02/"; | |
my $w = 1506; | |
my $h = 2160; | |
my $firstPageInCenter = 1; | |
my $leftPageX = 405; | |
my $rightPageX = $w + $leftPageX; | |
qx(mkdir -p $OUTDIR); | |
my $i = 0; | |
foreach my $oriFile (glob("${SRCDIR}/*.jpg")) { | |
if ($firstPageInCenter && $i == 0) { | |
my $pageNo0 = sprintf("%03d", $i++); | |
my $pageX = $leftPageX + int($w / 2); | |
qx(convert "$oriFile" -crop "${w}x${h}+${pageX}" "${OUTDIR}${pageNo0}.jpg"); | |
next; | |
} | |
my $basename = substr(basename($oriFile), 0, -4); | |
my $pageNo0 = sprintf("%03d", $i++); | |
my $pageNo1 = sprintf("%03d", $i++); | |
qx(convert "$oriFile" -crop "${w}x${h}+${rightPageX}" "${OUTDIR}${pageNo0}.jpg"); | |
qx(convert "$oriFile" -crop "${w}x${h}+${leftPageX}" "${OUTDIR}${pageNo1}.jpg"); | |
} | |
' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment