Skip to content

Instantly share code, notes, and snippets.

@fwolf
Last active April 11, 2019 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwolf/dc492fbf9254c581c02b8ba52a940373 to your computer and use it in GitHub Desktop.
Save fwolf/dc492fbf9254c581c02b8ba52a940373 to your computer and use it in GitHub Desktop.
针对 gb688.cn ,按高度适应,左右分别截图,然后左右合并。
#! /usr/bin/env bash
mkdir left
mkdir right
mkdir pages
#! /usr/bin/env php
<?php
/**
* 针对 gb688.cn ,按高度适应,左右分别截图,然后左右合并。
*
* 图片准备:
* - 安装 scrot 截图软件,xmag 屏幕测量软件(在 x1-apps 包中),Gimp 编辑软件。
* - scrot 需要 0.9.1 以上版本,https://tracker.debian.org/pkg/scrot 。
* - 安装 imagemagick 用于图像处理。
* - 打开 PDF 浏览,全屏,按高度适应,左右滚动到最左。
* - 使用 `xmag -mag 10` 测量出,需截屏区域的左上角、右下角坐标。
* - 计算出截屏区域宽度和高度。
* - 在 .xbindkeysrc 中设置截取区域的快捷键,并运行。
* - 截取所有页面左半部分,存于 left 目录,可根据结果调整截取参数。
* - 照此测量、截取右边部分,存于 right 目录。两次截取的起始 y 坐标和高度应该相同。
* - 截图应按前后顺序来,左右边文件保持数量、位置一致。目录内别放其他无关文件。
* - 用 Gimp 将左边白处油漆桶填充颜色放下面,右边设置透明度 70 放上面,移动找出左右完美重合位置。
* - 得到右边图图层 x 坐标,也就是说,将左边图只保留 x 宽度,就可与右边图左右拼合成完整图。
*
* f16 Zoom to 218%
* left page: 21,57 to 1065,1987 =* scrot -a 22,57,1043,1841
* right page: 0,57 to 1057,1898 = scrot -a 00,57,1057,1841
* offset x: 245
* f13/fo Zoom to 218%
* left page: 21,57 to 1066,1898
* right page: 0,57 to 1058,1898
* offset x: 245
* 为便于处理,截图参数设置为同 f16 了。
*
* Usefull command:
* width: identify -format '%w' l.png
* height: identify -format '%h' l.png
* convert -crop 245x10000000+0+0 l.png ll.png
* convert ll.png r.png +append p1.png
*
* @copyright Copyright 2019 Fwolf <fwolf.aide+merge-gb688@gmail.com>
* @license https://opensource.org/licenses/MIT MIT
*/
//// Check
if (5 > $argc) {
$printUsage = function () {
$baseName = basename(__FILE__);
echo <<<EOF
Usage: $baseName LEFT RIGHT RESULT OFFSET
Parameters:
-LEFT Directory which left half screenshot in
-RIGHT Directory which right half screenshot in
-RESULT Directory to store merged pictures
-OFFSET Left pic need crop after this width and append by right
EOF;
};
$printUsage();
exit(-1);
}
$leftDirRaw = $argv[1];
$leftDir = realpath($leftDirRaw);
if (!is_readable($leftDir)) {
echo "Left dir '{$leftDir}' not exists or readable." . PHP_EOL;
exit(-2);
}
$leftDir .= '/';
$rightDirRaw = $argv[2];
$rightDir = realpath($rightDirRaw);
if (!is_readable($rightDir)) {
echo "Right dir '{$rightDir}' not exists or readable." . PHP_EOL;
exit(-2);
}
$rightDir .= '/';
$destDirRaw = $argv[3];
$destDir = realpath($destDirRaw);
if (!is_writable($destDir)) {
echo "Dest dir '{$destDir}' not exists or writeable." . PHP_EOL;
exit(-2);
}
$destDir .= '/';
$offset = $argv[4];
//// Main body
$leftFiles = scandir($leftDir);
array_shift($leftFiles);
array_shift($leftFiles);
$rightFiles = scandir($rightDir);
array_shift($rightFiles);
array_shift($rightFiles);
$croppedLeft = tempnam(sys_get_temp_dir(), 'merge-gb688-');
$pageNumber = 0;
while (!empty($leftFiles)) {
$leftFile = $leftDir . array_shift($leftFiles);
$rightFile = $rightDir . array_shift($rightFiles);
$pageNumber ++;
// Crop
$cmd = "convert -crop {$offset}x10000000+0+0 " .
"\"{$leftFile}\" \"{$croppedLeft}\"";
exec($cmd);
// Merge
$destFile = $destDir . 'page-' . sprintf('%03d', $pageNumber) . '.png';
$cmd = "convert \"{$croppedLeft}\" \"{$rightFile}\" +append " .
"\"{$destFile}\"";
exec($cmd);
}
echo "Done, total {$pageNumber} pages." . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment