This commit is contained in:
2019-05-07 01:51:03 -07:00
parent b786e1a4f6
commit 5f9e59d87c

233
leaf.php
View File

@@ -2,10 +2,48 @@
<?php <?php
// Leaf - Tools for Book Scans // Leaf - Tools for Book Scans
$version = "0.5.8"; $version = "0.6.0";
$time_start = microtime(true);
// Functions // Functions
function fin() {
echo "\nFinished in ".floor($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"])." seconds\n";
die;
}
function msg($text, $action = null) {
if ($action == 1) {
echo bashcolor($text."\n","red"); die;
} elseif ($action == 2) {
echo bashcolor($text." (Y/n)\n","blue");
$line = trim(fgets(fopen("php://stdin","r")));
$line = $line ?: "y";
if($line != "y"){
echo bashcolor("Exiting!\n","red"); die;
}
} else {
echo $text."\n";
}
}
function args($query) {
global $argv;
foreach ($argv as $value) {
$parts = explode("=", $value);
$opt[trim($parts[0],"-")] = isset($parts[1]) ? $parts[1] : 1;
}
if ($query == "app") {
return $argv[1];
} elseif ($query == "dir") {
return chop($argv[count($argv)-1], "/")."/";
} elseif (isset($opt[$query])) {
return $opt[$query];
} else {
return null;
}
}
function Welcome($msg) { function Welcome($msg) {
global $version; global $version;
$out = "\033[1;1H\033[2J"; // clear screen $out = "\033[1;1H\033[2J"; // clear screen
@@ -38,20 +76,196 @@ function bashcolor($str,$fgcolor="white",$bgcolor=null) {
return $out; return $out;
} }
// Checks
if (!is_dir(args("dir"))) {
msg("Problem with working dir: ".args("dir"), 1);
}
if (!is_dir("scratch")) {
mkdir("scratch");
}
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// Convert: convert a dir of tiffs to jpeg for processing // Review: output a table of stats on source images
////////////////////// //////////////////////
if ($argv[1] == "convert") { if (args("app") == "review") {
echo Welcome("Convert to jpeg"); echo Welcome("List image statistics");
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
$files = glob(args("dir")."*.*");
foreach ($files as $file) {
if (isset($attr)) {
$prev_attr = $attr;
$attr = null;
}
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == "png") {
$tags = @shell_exec("exiftool -FileSize -ProfileDescription -BitsPerSample -PhotoshopQuality -ImageWidth -ImageHeight -PixelsPerUnitX -PixelsPerUnitY ".$file);
} else {
$tags = @shell_exec("exiftool -FileSize -ProfileDescription -BitsPerSample -PhotoshopQuality -ImageWidth -ImageHeight -Xresolution -Yresolution ".$file);
}
foreach (explode("\n", trim($tags)) as $tag) {
$parts = explode(":", $tag);
$attr[trim($parts[0])] = trim($parts[1]);
}
if ($ext == "png" && isset($attr['Pixels Per Unit X'])) {
$attr['X Resolution'] = round($attr['Pixels Per Unit X']/39.37007874016);
$attr['Y Resolution'] = round($attr['Pixels Per Unit Y']/39.37007874016);
}
$hilite["jpg"] = "blue";
$hilite["png"] = "green";
$hilite["JPG"] = "light blue";
$hilite["tif"] = "dark grey";
$hilite["DNG"] = "purple";
if (!isset($hilite[pathinfo($file, PATHINFO_EXTENSION)])) {
echo "Can't read ".$file."\n";
continue;
}
$print[] = bashcolor($file, $hilite[pathinfo($file, PATHINFO_EXTENSION)]);
$size = $attr['File Size'];
if (isset($attr['Photoshop Quality'])) {
$size .= " (Quality = ".$attr['Photoshop Quality'].")";
}
$print[] = $size;
if (isset($attr['Profile Description'])) {
$print[] = bashcolor($attr['Profile Description']." (".$attr['Bits Per Sample']." bit)", "cyan");
} else {
$print[] = bashcolor("unknown","black");
}
$dims = $attr['Image Width']."x".$attr['Image Height'];
$color = "purple";
if (isset($prev_attr['Image Width']) && isset($prev_attr['Image Height'])) {
$prev_dims = $prev_attr['Image Width']."x".$prev_attr['Image Height'];
if ($dims != $prev_dims) {
$color = "light purple";
}
}
$print[] = bashcolor($attr['Image Width']."x".$attr['Image Height'], $color);
if (isset($attr['X Resolution'])) {
$print[] = bashcolor(floor($attr['X Resolution']).":".floor($attr['Y Resolution']), "brown");
$width = number_format($attr['Image Width']/$attr['X Resolution'],2);
$height = number_format($attr['Image Height']/$attr['Y Resolution'],2);
$print[] = bashcolor($width."\" x ".$height."\"", "white", "black");
} else {
$print[] = bashcolor("unknown","black");
$print[] = bashcolor("can't determine","black","white");
}
foreach ($print as $piece) {
if ($piece == $print[0]) {
echo str_pad($piece, 60);
} else {
echo str_pad($piece, 40);
}
}
echo "\n";
$print = null;
}
fin();
////////////////////////////////////////////////////////////////////////////////////////////////
// DPIset: batch set resolution tags
//////////////////////
} elseif (args("app") == "dpiset") {
echo Welcome("Batch set EXIF resolution tags");
/////////////////////////////////////////////////////////////////////////
$files = glob(args("dir")."*.*");
if (null !== args("x")) {
$x = args("x");
} else {
msg("No resolution value",1);
}
foreach ($files as $file) {
echo "Processing ".$file;
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext == "jpg" | $ext == "JPG") {
exec("exiftool -overwrite_original -Xresolution=".$x." -Yresolution=".$x." ".$file);
} elseif ($ext == "DNG") {
exec("exiftool -overwrite_original -Xresolution=".$x." -Yresolution=".$x." -SubIFD:Xresolution=".$x." -SubIFD:Yresolution=".$x." -SubIFD1:Xresolution=".$x." -SubIFD1:Yresolution=".$x." ".$file);
} elseif ($ext == "png") {
$res = $x*39.37007874016;
exec("exiftool -overwrite_original -PixelsPerUnitX=".$res." -PixelsPerUnitY=".$res." ".$file);
} else {
echo " -> skip";
}
echo "\n";
}
fin();
////////////////////////////////////////////////////////////////////////////////////////////////
// Deskew: detect skew angle and apply to rotation tag
//////////////////////
} elseif (args("app") == "deskew") {
echo Welcome("Detect skew angles");
/////////////////////////////////////////////////////////////////////////
$deskew_max_angle = ".4";
$deskew_padding = 80;
$deskew_contrast = 20;
$deskew_size = 2200;
$files = glob(args("dir")."*.jpg");
if (!args("nomap")) {
msg("Creating dmaps...");
} else {
msg("Skipping dmap creation");
}
if (shell_exec("exiftool -s -s -s -CropAngle ".$files[0]." 2>&1")) {
msg("Crop angle detected on ".$files[0].", continue?",2);
}
foreach ($files as $file) {
if (args("nomap")) {
$dmfile = $file;
} else {
$dmfile = "scratch/".basename($file, ".jpg")."_dmap.jpg";
}
$result = shell_exec("deskew -o /tmp/null -l 99 \"".$dmfile."\" 2>&1");
$arr = explode("Skew angle found: ", $result);
$angle = substr($arr[1], 0, 4);
echo $file.": ".$angle;
if ($angle > $deskew_max_angle | $angle < ($deskew_max_angle*-1)) {
echo " (too big)";
} else {
$angles[$file] = $angle;
}
echo "\n";
}
// echo "\nWriting angles to EXIF: ";
//
// foreach ($files as $file) {
//
// if (isset($angles[$file])) {
// exec("exiftool -overwrite_original -XMP-crs:CropAngle=".$angles[$file]." ".$file);
// echo ".";
// } else {
// // erase top edge crop to indicate rotation was not applied
// exec("exiftool -overwrite_original -XMP-crs:CropTop=0 ".$file." 2>&1");
// }
// }
// }
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// Elif // Elif
////////////////////// //////////////////////
} else { } else {
echo Welcome("Program name ".$argv[1]." not found"); echo Welcome("Program name \"".$argv[1]."\" not found");
} }
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
@@ -59,6 +273,15 @@ die;
// Settings // Settings
$tif_dir = "null"; $tif_dir = "null";