This commit is contained in:
2019-05-07 01:55:16 -07:00
parent 158a35ee7b
commit 74db6107aa

148
leaf.php
View File

@@ -1,7 +1,7 @@
<?php
// Leaf - Tools for Book Scans
$version = "0.8.7";
$version = "0.8.8";
$time_start = microtime(true);
date_default_timezone_set("America/Los_Angeles");
@@ -17,6 +17,46 @@ if (in_array($cores,$reasonable_cores)) {
// Functions
function crop_imgk_to_xmp($width, $height, $xoff, $yoff) {
$top = $yoff/$height;
$right = (($width-$xoff)/$width);
$left = $xoff/$width;
$bottom = (($height-$yoff)/$height);
return array(number_format($top,6), number_format($right,6), number_format($left,6), number_format($bottom,6));
}
function crop_xmp_to_imgk($parts, $deskew_padding) {
@list($top, $right, $left, $bottom, $check_width, $check_height, $orientation) = explode("\n", $parts);
if (!$top || !$right || !$left || !$bottom || !$check_width || !$check_height || ! $orientation) {
return null;
}
$jpeg_width = $check_height;
$jpeg_height = $check_width;
$top_pixels = floor( $jpeg_width * $top );
$right_pixels = $jpeg_height - floor( $right * $jpeg_height );
$left_pixels = floor( $jpeg_height * $left );
$bottom_pixels = $jpeg_width - floor( $bottom * $jpeg_width );
$width = $jpeg_width - ( $top_pixels + $bottom_pixels );
$height = $jpeg_height - ( $left_pixels + $right_pixels );
if ($orientation == "Rotate 90 CW") {
return ( $width + $deskew_padding*2 )."x".( $height + $deskew_padding*2 )."+".( $bottom_pixels - $deskew_padding )."+".( $left_pixels - $deskew_padding );
} elseif ($orientation == "Rotate 270 CW") {
return ( $width + $deskew_padding*2 )."x".( $height + $deskew_padding*2 )."+".( $top_pixels - $deskew_padding )."+".( $right_pixels - $deskew_padding );
} else {
return null;
}
}
function levels_ps_to_imgk($string) {
$parts = explode(",", $string);
if (count($parts) != 3) {
@@ -204,6 +244,7 @@ $help = "Leaf $version
USAGE: leaf [mode] [-options] directory
Modes:
autocrop crop using row/column brightness method
build combine images into a pdf with img2pdf
clean remove scratch files
crop define EXIF crop values using template files
@@ -215,7 +256,7 @@ deskew detect rotation angles
-contrast=<num> (20) levels attenuation
-size=<num> (2200) dmap size in pixels
-nomap don't use dmaps
depaper divide page with itself using mask (better despine)
depaper divide page with itself using mask (better despine)
-mask=<file> mask file for spine-left image
-mpush=<levels> levels for divide map (ex. \"0-253,0-2,0-255\")
-mix=<0-100> (null) percentage mix with original (90% = mostly og)
@@ -247,6 +288,9 @@ generate create final jpg images for pdf creation
-pt=<0-255> use threshold method with photoshop level value
-pc=<2,2!,3> (2) colors for final png (2! = intermediary dithering)
-unsharp=<params> photoshop unsharp parameters (ex. \"100,1,0\")
-skipc skip color images
-skipg skip grayscale images
-skipb skip bitmap images
match move files and set crop values to match source dir
-source=<dir> read FROM dir
notes save leaf shell history to text file
@@ -271,6 +315,67 @@ strip strip exif crop values from images with exiftool
echo $help;
fin();
foreach ($picks 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"));
@list($top, $right, $left, $bottom) = explode("\n", $parts);
if ($top && $right && $left && $bottom) {
$key = filter_var(substr(basename($file), 4), FILTER_SANITIZE_NUMBER_INT);
$crops[$key]['top'] = $top;
$crops[$key]['right'] = $right;
$crops[$key]['left'] = $left;
$crops[$key]['bottom'] = $bottom;
} else {
msg("Problem reading crops from file ".$file.". Continue?",2);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Note: Autocrop
//////////////////////
} elseif (args("app") == "autocrop") {
echo Welcome("Crop using row/column brightness method");
/////////////////////////////////////////////////////////////////////////
msg("Checking crops");
$files = glob(args("dir")."*.*");
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"));
@list($top, $right, $left, $bottom) = explode("\n", $parts);
echo "\nExisting crop values for ".$file.": ";
echo "\tTop: ".bashcolor($top, "light blue");
echo "\tRight: ".bashcolor($right, "light green");
echo "\tLeft: ".bashcolor($left, "light cyan");
echo "\tBottom: ".bashcolor($bottom, "light red");
$detectx = explode("\n",chop(shell_exec("convert \( $file +repage -scale x1! -bordercolor black -border 1 -fuzz 30% -trim \) -format \"%w\n%O\" info:")));
$detecty = explode("\n",chop(shell_exec("convert \( $file +repage -scale 1x! -bordercolor black -border 1 -fuzz 60% -trim \) -format \"%h\n%O\" info:")));
$detectxparts = explode("+", $detectx[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.": ";
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;
$thread[] = array($msg, $cmd);
echo "\n".$msg; exec($cmd);
}
//msg("Write crop values?",2);
//multiexec($thread,$xt);
fin();
////////////////////////////////////////////////////////////////////////////////////////////////
// Note: Notes
//////////////////////
@@ -1377,8 +1482,8 @@ echo Welcome("Combine finished images into a pdf with python img2pdf");
$input = args("dir")."*.*";
$dest = basename(getcwd()).".pdf";
if (file_exists($dest)) {
$dest = basename(getcwd())."_".rand(1000,9999).".pdf";
if (file_exists($dest)) { msg("Freak accident",1); }
msg("Files ".$dest." already exists. Move to trash?",2);
exec("/opt/local/bin/trash -a ".$dest);
}
echo "Creating pdf...\n\n";
exec("img2pdf --verbose --viewer-page-layout twocolumnright --author 'Leaf ".$version."' --output ".$dest." ".$input);
@@ -1687,25 +1792,12 @@ if (!args("nomap")) {
if (!file_exists($dmfile)) {
$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);
if (!$top || !$right || !$left || !$bottom) { msg($file." missing crop information!",1); }
$parts = chop(shell_exec("exiftool -s -s -s -XMP-crs:CropTop -XMP-crs:CropRight -XMP-crs:CropLeft -XMP-crs:CropBottom -ImageWidth -ImageHeight -Orientation ".$file." 2>&1"));
$getcrop = crop_xmp_to_imgk($parts,$deskew_padding);
$jpeg_width = $check_height;
$jpeg_height = $check_width;
$top_pixels = floor( $jpeg_width * $top );
$right_pixels = $jpeg_height - floor( $right * $jpeg_height );
$left_pixels = floor( $jpeg_height * $left );
$bottom_pixels = $jpeg_width - floor( $bottom * $jpeg_width );
$width = $jpeg_width - ( $top_pixels + $bottom_pixels );
$height = $jpeg_height - ( $left_pixels + $right_pixels );
$area_left = ( $width + $deskew_padding*2 )."x".( $height + $deskew_padding*2 )."+".( $bottom_pixels - $deskew_padding )."+".( $left_pixels - $deskew_padding );
$area_right = ( $width + $deskew_padding*2 )."x".( $height + $deskew_padding*2 )."+".( $top_pixels - $deskew_padding )."+".( $right_pixels - $deskew_padding );
//echo "\n\n(Area left = ".$area_left.", Area right = ".$area_right.")";
if (!$getcrop) {
msg($file." missing crop or orientation information!",1);
}
$size = $deskew_size."x".$deskew_size;
@@ -1715,11 +1807,7 @@ if (!args("nomap")) {
$adj = $deskew_contrast."%,".(100-$deskew_contrast)."%";
}
if (substr(basename($file), 0, 3) % 2 == 0) {
$cmd = "convert ".$file." -auto-orient -crop ".$area_right." -level ".$adj." -colorspace Gray -resize ".$size." ".$dmfile;
} else {
$cmd = "convert ".$file." -auto-orient -crop ".$area_left." -level ".$adj." -colorspace Gray -resize ".$size." ".$dmfile;
}
$cmd = "convert ".$file." -auto-orient -crop ".$getcrop." -level ".$adj." -colorspace Gray -resize ".$size." ".$dmfile;
$msg = "Creating dmap for ".$file;
$thread[] = array($msg, $cmd);
@@ -1862,10 +1950,4 @@ echo Welcome("Program name \"".$argv[1]."\" not found");
die;
// check bridge labels
// for i in *; do exiftool -s -s -s -Label $i; done
// convert to sRGB lossy
// jpegicc -q100 001-Plustek0001.jpg 001-Plustek0001_out.jpg
?>