This commit is contained in:
2019-05-07 01:55:30 -07:00
parent 74db6107aa
commit 57c010a09f

View File

@@ -1,7 +1,7 @@
<?php <?php
// Leaf - Tools for Book Scans // Leaf - Tools for Book Scans
$version = "0.8.8"; $version = "0.8.10";
$time_start = microtime(true); $time_start = microtime(true);
date_default_timezone_set("America/Los_Angeles"); date_default_timezone_set("America/Los_Angeles");
@@ -17,12 +17,12 @@ if (in_array($cores,$reasonable_cores)) {
// Functions // Functions
function crop_imgk_to_xmp($width, $height, $xoff, $yoff) { function crop_imgk_to_xmp($cwidth, $cheight, $xoff, $yoff, $width, $height) {
$top = $yoff/$height; $top = $yoff/$height;
$right = (($width-$xoff)/$width); $right = (($xoff+$cwidth)/$width);
$left = $xoff/$width; $left = $xoff/$width;
$bottom = (($height-$yoff)/$height); $bottom = (($yoff+$cheight)/$height);
return array(number_format($top,6), number_format($right,6), number_format($left,6), number_format($bottom,6)); return array(number_format($top,6), number_format($right,6), number_format($left,6), number_format($bottom,6));
@@ -245,6 +245,7 @@ USAGE: leaf [mode] [-options] directory
Modes: Modes:
autocrop crop using row/column brightness method autocrop crop using row/column brightness method
-levels=<params> levels adjustment for resulting image (ex. \"0-253,0-2,0-255\")
build combine images into a pdf with img2pdf build combine images into a pdf with img2pdf
clean remove scratch files clean remove scratch files
crop define EXIF crop values using template files crop define EXIF crop values using template files
@@ -336,34 +337,71 @@ foreach ($picks as $file) {
echo Welcome("Crop using row/column brightness method"); echo Welcome("Crop using row/column brightness method");
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
if (args("levels")) {
$adjust = args("levels");
}
msg("Checking crops"); msg("Checking crops");
$files = glob(args("dir")."*.*"); $files = glob(args("dir")."*.*");
foreach ($files as $file) { foreach ($files as $file) {
$parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom ".$file." 2>&1")); $tags = chop(shell_exec("exiftool -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -ImageWidth -ImageHeight ".$file." 2>&1"));
@list($top, $right, $left, $bottom) = explode("\n", $parts); foreach (explode("\n", trim($tags)) as $tag) {
echo "\nExisting crop values for ".$file.": "; $parts = explode(":", $tag);
echo "\tTop: ".bashcolor($top, "light blue"); $attr[trim($parts[0])] = trim($parts[1]);
echo "\tRight: ".bashcolor($right, "light green"); }
echo "\tLeft: ".bashcolor($left, "light cyan");
echo "\tBottom: ".bashcolor($bottom, "light red"); $width = $attr['Image Width'];
$height = $attr['Image Height'];
if (isset($attr['Crop Top']) && isset($attr['Crop Right']) && isset($attr['Crop Left']) && isset($attr['Crop Bottom'])) {
echo "\nExisting crop values for ".$file.": ";
echo "\tTop: ".bashcolor($attr['Crop Top'], "light blue");
echo "\tRight: ".bashcolor($attr['Crop Right'], "light green");
echo "\tLeft: ".bashcolor($attr['Crop Left'], "light cyan");
echo "\tBottom: ".bashcolor($attr['Crop Bottom'], "light red");
} else {
echo "\nNo existing crop values for ".$file;
}
$detectx = explode("\n",chop(shell_exec("convert \( $file +repage -scale x1! -bordercolor black -border 1 -fuzz 30% -trim \) -format \"%w\n%O\" info:"))); if (isset($adjust)) {
$detecty = explode("\n",chop(shell_exec("convert \( $file +repage -scale 1x! -bordercolor black -border 1 -fuzz 60% -trim \) -format \"%h\n%O\" info:"))); $adjfile = "\( ".$file." -level ".$adjust." \)";
} else {
$adjfile = $file;
}
$detectx = explode("\n",chop(@shell_exec("convert \( ".$adjfile." +repage -scale x1! -bordercolor black -border 1 -fuzz 30% -trim \) -format \"%w\n%O\" info: 2>&1")));
$detecty = explode("\n",chop(@shell_exec("convert \( ".$adjfile." +repage -scale 1x! -bordercolor black -border 1 -fuzz 60% -trim \) -format \"%h\n%O\" info: 2>&1")));
$detectxparts = explode("+", $detectx[1]); $detectxparts = explode("+", $detectx[1]);
$detectyparts = explode("+", $detecty[1]); $detectyparts = explode("+", $detecty[1]);
list($ntop, $nright, $nleft, $nbottom) = crop_imgk_to_xmp($detectx[0], $detecty[0], $detectxparts[1], $detectxparts[2]);
echo "\nNew crop values for ".$file.": "; if ($debug) { echo "\n".bashcolor("imgk values: cwidth: ".$detectx[0].", cheight: ".$detecty[0].", xoff: ".$detectxparts[1].", yoff: ".$detectxparts[2].", width: ".$width.", height: ".$height, "white","light purple"); }
echo "\tTop: ".bashcolor($ntop, "blue");
echo "\tRight: ".bashcolor($nright, "green");
echo "\tLeft: ".bashcolor($nleft, "cyan");
echo "\tBottom: ".bashcolor($nbottom, "red");
$msg = "Setting crop values for ".$file; if (isset($detectx[0]) && $detectx[0] > 1) { $cwidth = $detectx[0]; } else { $cwidth = null; }
$cmd = "exiftool -overwrite_original -XMP-crs:HasCrop=1 -XMP-crs:AlreadyApplied=0 -XMP-crs:CropTop=".$ntop." -XMP-crs:CropRight=".$nright." -XMP-crs:CropLeft=".$nleft." -XMP-crs:CropBottom=".$nbottom." ".$file; if (isset($detecty[0]) && $detecty[0] > 1) { $cheight = $detecty[0]; } else { $cheight = null; }
if (isset($detectxparts[1]) && $detectxparts[1] > 1) { $xoff = $detectxparts[1]; } else { $xoff = null; }
if (isset($detectxparts[2])) { $yoff = $detectxparts[2]; } else { $yoff = null; }
if ($cwidth && $cheight && $xoff && $yoff) {
list($ntop, $nright, $nleft, $nbottom) = crop_imgk_to_xmp($cwidth, $cheight, $xoff, $yoff, $width, $height);
echo "\nNew crop values for ".$file.": ";
echo "\tTop: ".bashcolor($ntop, "blue");
echo "\tRight: ".bashcolor($nright, "green");
echo "\tLeft: ".bashcolor($nleft, "cyan");
echo "\tBottom: ".bashcolor($nbottom, "red");
$msg = "Setting crop values for ".$file;
$cmd = "exiftool -overwrite_original -XMP-crs:HasCrop=1 -XMP-crs:AlreadyApplied=0 -XMP-crs:CropTop=".$ntop." -XMP-crs:CropRight=".$nright." -XMP-crs:CropLeft=".$nleft." -XMP-crs:CropBottom=".$nbottom." ".$file;
} else {
$msg = "Incomplete crop values for ".$file.", skipping...";
$cmd = "true";
}
$thread[] = array($msg, $cmd); $thread[] = array($msg, $cmd);
@@ -1343,7 +1381,7 @@ fin();
echo Welcome("Insert image into numbered sequence"); echo Welcome("Insert image into numbered sequence");
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
$files = glob(args("dir")."[0-9][0-9][0-9]-*.jpg"); $files = glob(args("dir")."[0-9][0-9][0-9]-*.*");
$count = count($files); $count = count($files);
if (!$count) { if (!$count) {
@@ -1387,7 +1425,7 @@ foreach ($ops as $parts) {
rename($parts[0], $parts[1]); rename($parts[0], $parts[1]);
} }
fin("Starting file count: ".$count."; Ending file count: ".count(glob(args("dir")."[0-9][0-9][0-9]-*.jpg"))); fin("Starting file count: ".$count."; Ending file count: ".count(glob(args("dir")."[0-9][0-9][0-9]-*.*")));
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// Note: Review // Note: Review