This commit is contained in:
2019-05-07 01:53:33 -07:00
parent e35c2515f2
commit 1806fcf0dc

View File

@@ -2,14 +2,14 @@
<?php
// Leaf - Tools for Book Scans
$version = "0.6.9";
$version = "0.7.0";
$time_start = microtime(true);
date_default_timezone_set("America/Los_Angeles");
$xt = 3; // threads
// Functions
function multiexec($thread, $x) {
function multiexec($thread, $x, $null = true) {
$batch = array_chunk($thread,$x);
foreach ($batch as $group) {
$msg = array();
@@ -18,12 +18,17 @@ function multiexec($thread, $x) {
$msg[] = $parts[0];
$cmd[] = $parts[1];
}
$echo = implode("\n", $msg);
$exec = implode(" > /dev/null & ",$cmd)." & wait";
echo $echo;
if (strlen($echo) > 1) {
echo "\n";
if (strlen($msg[0]) > 1) {
$echo = implode("\n", $msg)."\n";
} else {
$echo = implode("", $msg);
}
if ($null) {
$exec = implode(" > /dev/null & ",$cmd)." & wait";
} else {
$exec = implode(" & ",$cmd)." & wait";
}
echo $echo;
exec($exec);
}
}
@@ -851,10 +856,10 @@ foreach ($files as $file) {
echo "Processing ".$file.": ";
$ext = pathinfo($file, PATHINFO_EXTENSION);
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 ".$file." 2>&1"));
if (strlen($parts) > 1) { list($top, $right, $left, $bottom) = explode("\n", $parts); }
if (isset($top) | isset($right) | isset($left) | isset($bottom)) {
shell_exec("exiftool -overwrite_original -XMP-crs:CropTop= -XMP-crs:CropRight= -XMP-crs:CropLeft= -XMP-crs:CropBottom= ".$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 (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");
echo "removed";
} else {
echo "no crop found";
@@ -882,7 +887,6 @@ if (args("max")) {
if (null !== args("pad")) {
$deskew_padding = args("pad");
echo "pad = ".args("pad");
} else {
$deskew_padding = 80;
}
@@ -899,8 +903,6 @@ if (args("size")) {
$deskew_size = 2200;
}
die;
$files = glob(args("dir")."*.{jpg,JPG,dng,DNG}", GLOB_BRACE);
echo "Scanning for crop values: ";
@@ -974,29 +976,59 @@ if (!args("nomap")) {
$thread = null;
echo "\n";
msg("Detecting skew...");
echo "Deskewing with ".$xt." threads: ";
foreach ($files as $file) {
$ext = pathinfo($file,PATHINFO_EXTENSION);
if (args("nomap")) {
$dmfile = $file;
} else {
$ext = pathinfo($file,PATHINFO_EXTENSION);
$dmfile = "scratch/".basename($file, ".".$ext)."_dmap.jpg";
}
$txtfile = "scratch/".basename($file, ".".$ext)."_angle.txt";
if (file_exists($txtfile)) {
$result = shell_exec("deskew -o /tmp/null -l 99 \"".$dmfile."\" 2>&1");
$arr = explode("Skew angle found: ", $result);
$angle = substr($arr[1], 0, 4);
$msg = $file.": ".$angle;
$msg = "o";
$cmd = "true";
} else {
$msg = ".";
$cmd = "deskew -o /tmp/null -l 99 ".$dmfile." | grep 'Skew angle found:' | cut -f2- -d: | tr -d ' \n' > ".$txtfile;
}
$thread[] = array($msg, $cmd);
}
multiexec($thread,$xt,false);
echo "\n\n";
foreach ($files as $file) {
$txtfile = "scratch/".basename($file, ".".$ext)."_angle.txt";
$angle = file_get_contents($txtfile);
$msg = $file." = ".$angle;
if ($angle > $deskew_max_angle | $angle < ($deskew_max_angle*-1)) {
$msg .= " (too big)";
} else {
$angles[$file] = $angle;
}
msg($msg);
}
$thread = null;
echo "\n\n";
echo "Checking for existing rotation: ";
@@ -1030,7 +1062,9 @@ foreach ($files as $file) {
}
echo "\n\n";
echo "\n";
msg("Write to EXIF?",2);
if (is_array($thread)) {