This commit is contained in:
2019-05-07 01:54:13 -07:00
parent 8f5ea6b62d
commit 659d1ec70d

View File

@@ -1,8 +1,7 @@
#!/usr/bin/php
<?php <?php
// Leaf - Tools for Book Scans // Leaf - Tools for Book Scans
$version = "0.8.0"; $version = "0.8.1";
$time_start = microtime(true); $time_start = microtime(true);
date_default_timezone_set("America/Los_Angeles"); date_default_timezone_set("America/Los_Angeles");
@@ -187,6 +186,8 @@ generate create final jpg images for pdf creation
-px=<num> png size multiplier -px=<num> png size multiplier
-padjust=<params> png levels adjustment (otherwise calculated from adjust) -padjust=<params> png levels adjustment (otherwise calculated from adjust)
makepdf combine images into a pdf with img2pdf makepdf combine images into a pdf with img2pdf
match move files and set crop values to match source dir
-source=<dir> read FROM dir
profile apply xmp profile to images (requires exiv2 > 0.25) profile apply xmp profile to images (requires exiv2 > 0.25)
-file=<file> xmp profile -file=<file> xmp profile
resort reorder image sequence by adding a new image resort reorder image sequence by adding a new image
@@ -207,6 +208,53 @@ strip strip exif crop values from images with exiftool
echo $help; echo $help;
fin(); fin();
////////////////////////////////////////////////////////////////////////////////////////////////
// Note: Match
//////////////////////
} elseif (args("app") == "match") {
echo Welcome("Match directories");
/////////////////////////////////////////////////////////////////////////
$source = chop(args("source"), "/")."/";
$files = glob($source."[0-9][0-9][0-9]-*");
if (!$files) {
msg("No valid files in source dir!",1);
}
foreach ($files as $file) {
$ext = pathinfo($file,PATHINFO_EXTENSION);
$key = substr(basename($file), 0, 3);
$match = glob(args("dir")."*".substr(basename($file, ".".$ext), 4)."*");
if (isset($match[0]) && file_exists($match[0])) {
$dest = $match[0];
$dparts = pathinfo($dest);
if (basename($file,$ext) != basename($dest,$dparts['extension'])) {
$newdest = $dparts['dirname']."/".$key."-".$dparts['basename'];
msg("Moving ".$dest." to ".$newdest);
//rename($dest,$newdest);
}
$parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -XMP-crs:CropAngle ".$file." 2>&1"));
@list($top, $right, $left, $bottom, $angle) = explode("\n", $parts);
if (isset($top) || isset($right) || isset($left) || isset($bottom) || isset($angle)) {
$info = "Writing crop information from ".$file." to ".$newdest;
$info .= " Top: ".bashcolor($top,"green");
$info .= " Right: ".bashcolor($right,"green");
$info .= " Left: ".bashcolor($left,"green");
$info .= " Bottom: ".bashcolor($bottom,"green");
$info .= " Angle: ".bashcolor($angle,"green");
msg($info);
//exec("exiftool -overwrite_original -XMP-crs:HasCrop=1 -XMP-crs:AlreadyApplied=0 -XMP-crs:CropTop=".$top." -XMP-crs:CropRight=".$right." -XMP-crs:CropLeft=".$left." -XMP-crs:CropBottom=".$bottom." -XMP-crs:CropAngle=".$angle." ".$file);
} else {
msg("Source file ".$file." does not contain crop information");
}
} else {
msg("No match found for ".$file." in destination");
}
}
fin();
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// Note: Generate // Note: Generate
////////////////////// //////////////////////
@@ -276,19 +324,19 @@ foreach ($files as $file) {
echo "\n\n"; echo "\n\n";
echo "Bitmap files: "; echo "Bitmap files: ";
if (is_array($bitmap)) { echo count($bitmap); } else { echo "None"; } if (isset($bitmap)) { echo count($bitmap); } else { echo "None"; }
echo "\n"; echo "\n";
echo "Greyscale files: "; echo "Greyscale files: ";
if (is_array($grey)) { echo count($grey); } else { echo "None"; } if (isset($grey)) { echo count($grey); } else { echo "None"; }
echo "\n"; echo "\n";
echo "Color files: "; echo "Color files: ";
if (is_array($color)) { echo count($color); } else { echo "None"; } if (isset($color)) { echo count($color); } else { echo "None"; }
echo "\n"; echo "\n";
echo "Spine files: "; echo "Spine files: ";
if (is_array($spine)) { echo count($spine); } else { echo "None"; } if (isset($spine)) { echo count($spine); } else { echo "None"; }
echo "\n\n"; echo "\n\n";
if (count($spine) > 1) { if (count($spine) > 1) {
@@ -310,8 +358,8 @@ if ($ratioavg > 1) { // page is tall
$paper_height = number_format($paper_width*$ratioavg,2); $paper_height = number_format($paper_width*$ratioavg,2);
} }
$dpi = $page_height / $paper_height; $dpi = number_format($page_height/$paper_height,2);
$canonicalpngdpi = $dpi*$multiplier; $canonicalpngdpi = number_format($dpi*$multiplier,2);
$pngdpi = $canonicalpngdpi*39.37007874016; $pngdpi = $canonicalpngdpi*39.37007874016;
$dims = $page_width."x".$page_height; $dims = $page_width."x".$page_height;
$pngdims = ($page_width*$multiplier)."x".($page_height*$multiplier); $pngdims = ($page_width*$multiplier)."x".($page_height*$multiplier);
@@ -327,12 +375,14 @@ foreach ($color as $parts) {
$jpg[] = $parts; $jpg[] = $parts;
} }
if (isset($grey)) {
foreach ($grey as $parts) { foreach ($grey as $parts) {
$parts[] = 1; $parts[] = 1;
$jpg[] = $parts; $jpg[] = $parts;
} }
}
if (is_array($spine)) { if (isset($spine)) {
$spine[0][] = 2; $spine[0][] = 2;
$jpg[] = $spine[0]; $jpg[] = $spine[0];
} }
@@ -380,6 +430,8 @@ echo "\n";
msg("Setting DPI: "); msg("Setting DPI: ");
multiexec($thread_b,$xt); multiexec($thread_b,$xt);
if (isset($bitmap)) {
foreach ($bitmap as $parts) { foreach ($bitmap as $parts) {
$file = $parts[0]; $file = $parts[0];
@@ -423,6 +475,8 @@ echo "\n";
msg("Setting DPI: "); msg("Setting DPI: ");
multiexec($thread_d,$xt); multiexec($thread_d,$xt);
}
fin(); fin();
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
@@ -436,9 +490,7 @@ msg("Remove scratch files?",2);
if (!is_dir("scratch")) { if (!is_dir("scratch")) {
msg("Problem with scratch dir",2); msg("Problem with scratch dir",2);
} else { } else {
foreach (glob("scratch/*.*") as $file) { exec("/opt/local/bin/trash -a scratch/");
unlink($file);
}
} }
fin(); fin();
@@ -1007,7 +1059,7 @@ if (file_exists($dest)) {
if (file_exists($dest)) { msg("Freak accident",1); } if (file_exists($dest)) { msg("Freak accident",1); }
} }
echo "Creating pdf...\n\n"; echo "Creating pdf...\n\n";
exec("img2pdf --verbose --viewer-page-layout twocolumnright --creator 'Leaf ".$version."' --output ".$dest." ".$input); exec("img2pdf --verbose --viewer-page-layout twocolumnright --author 'Leaf ".$version."' --output ".$dest." ".$input);
echo "\n"; echo "\n";
msg("Press return to open in Acrobat",2); msg("Press return to open in Acrobat",2);
exec("open ".$dest." -b com.adobe.Acrobat.Pro"); exec("open ".$dest." -b com.adobe.Acrobat.Pro");
@@ -1088,7 +1140,7 @@ if (!args("map")) {
if (!file_exists($map)) { if (!file_exists($map)) {
@exec("convert -".$method." -colorspace gray ".args("map")." ".$map); @exec("convert -".$method." -colorspace gray ".args("map")." ".$map);
} }
} elseif (substr(args("map"), -3, 3) == "jpg") { } elseif (substr(args("map"), -3, 3) == "jpg" | substr(args("map"), -3, 3) == "tif") {
$map = args("map"); $map = args("map");
} }
@@ -1144,7 +1196,7 @@ foreach ($files as $file) {
} }
list ($width, $height) = getimagesize($use); list ($width, $height) = getimagesize($use);
list ($twidth, $theight) = getimagesize($file); list ($twidth, $theight) = getimagesize($file);
if ($width != $twidth | $height != $theight) { if ($width != $twidth || $height != $theight) {
$tmap = $use."'[".$twidth."x".$theight."!]'"; $tmap = $use."'[".$twidth."x".$theight."!]'";
$msg .= " (resize map) "; $msg .= " (resize map) ";
} else { } else {
@@ -1201,7 +1253,7 @@ foreach ($files as $file) {
list ($imwidth, $imheight) = getimagesize($file); list ($imwidth, $imheight) = getimagesize($file);
$x = floor($imheight/$height); $x = floor($imheight/$height);
} }
if ($ext == "jpg" | $ext == "JPG") { if ($ext == "jpg" || $ext == "JPG") {
exec("exiftool -overwrite_original -Xresolution=".$x." -Yresolution=".$x." -jfif:Xresolution=".$x." -jfif:Yresolution=".$x." ".$file); exec("exiftool -overwrite_original -Xresolution=".$x." -Yresolution=".$x." -jfif:Xresolution=".$x." -jfif:Yresolution=".$x." ".$file);
echo "set DPI to ".$x; echo "set DPI to ".$x;
} elseif ($ext == "DNG") { } elseif ($ext == "DNG") {
@@ -1231,11 +1283,11 @@ $files = glob(args("dir")."*.*");
foreach ($files as $file) { foreach ($files as $file) {
echo "Processing ".$file.": "; echo "Processing ".$file.": ";
$ext = pathinfo($file, PATHINFO_EXTENSION); $ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == "jpg" | $ext == "JPG" | $ext == "DNG") { if ($ext == "jpg" || $ext == "JPG" || $ext == "DNG") {
$parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -XMP-crs:CropAngle ".$file." 2>&1")); $parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -XMP-crs:CropAngle ".$file." 2>&1"));
if (strlen($parts) > 1) { @list($top, $right, $left, $bottom, $angle) = explode("\n", $parts); } if (strlen($parts) > 1) { @list($top, $right, $left, $bottom, $angle) = explode("\n", $parts); }
if (isset($top) | isset($right) | isset($left) | isset($bottom) | isset($angle)) { if (isset($top) || isset($right) || isset($left) || isset($bottom) || isset($angle)) {
shell_exec("exiftool -overwrite_original -XMP-crs:CropTop= -XMP-crs:CropRight= -XMP-crs:CropLeft= -XMP-crs:CropBottom= -XMP-crs:CropBottom= ".$file." 2>&1"); shell_exec("exiftool -overwrite_original -XMP-crs:CropTop= -XMP-crs:CropRight= -XMP-crs:CropLeft= -XMP-crs:CropBottom= -XMP-crs:CropAngle= ".$file." 2>&1");
echo "removed"; echo "removed";
} else { } else {
echo "no crop found"; echo "no crop found";
@@ -1294,7 +1346,7 @@ if (!args("nomap")) {
$parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -ImageWidth -ImageHeight ".$file." 2>&1")); $parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -ImageWidth -ImageHeight ".$file." 2>&1"));
@list($top, $right, $left, $bottom, $check_width, $check_height) = explode("\n", $parts); @list($top, $right, $left, $bottom, $check_width, $check_height) = explode("\n", $parts);
if (!$top | !$right | !$left | !$bottom) { msg($file." missing crop information!",1); } if (!$top || !$right || !$left || !$bottom) { msg($file." missing crop information!",1); }
$jpeg_width = $check_height; $jpeg_width = $check_height;
$jpeg_height = $check_width; $jpeg_height = $check_width;
@@ -1393,7 +1445,7 @@ foreach ($files as $file) {
$msg = $file." = ".$angle; $msg = $file." = ".$angle;
if ($angle > $deskew_max_angle | $angle < ($deskew_max_angle*-1)) { if ($angle > $deskew_max_angle || $angle < ($deskew_max_angle*-1)) {
$msg .= " (too big)"; $msg .= " (too big)";
} else { } else {
$angles[$file] = $angle; $angles[$file] = $angle;