Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Last active September 28, 2024 19:28
Show Gist options
  • Save fabiolimace/ecc9e116d6f9a5f6efd3989048b20d97 to your computer and use it in GitHub Desktop.
Save fabiolimace/ecc9e116d6f9a5f6efd3989048b20d97 to your computer and use it in GitHub Desktop.
Convert a set of two-page print screens into a single PDF file using ImageMagick
#!/bin/bash
#
# Convert a folder containing one page printscreens in PNG format to a PDF file.
#
# Before using this script, you have to make print screens from the book you want to convert to PDF.
#
# This script processes all print screens and generates a PDF file with A4 page size.
#
# If the output file is too big, you can simply print it to file to generate a smaller version of the output file.
#
# Usage:
#
# one-page-prtsc-to-pdf PRTSC_FOLDER OUTPUT_FILE [CROP_AREA]
#
# PRTSC_FOLDER: a folder containing one page printscreens in PNG format
# OUTPUT_FILE: a name for the output PDF file
# CROP_AREA: a selected area to be cropped in this format: 'WIDTHxHEIGHT+X+Y' (example: 1600x1080+160+0)
#
# Dependency:
# ImageMagick's convert tool
#
# ImageMagick settings:
#
# Make a backup of the original settings
# sudo cp /etc/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml.original
#
# Allow PDF conversion to ImageMagick
# https://www.kb.cert.org/vuls/id/332928/
# https://stackoverflow.com/questions/52998331/
# sudo sed -E -i 's/^.*policy.*coder.*none.*PDF/<!--\0-->/' /etc/ImageMagick-6/policy.xml
#
# Increase maximum disk usage policy to 8GiB
# https://stackoverflow.com/questions/31407010/
# sudo sed -E -i 's/(^.*policy.*resource.*disk.*value=)("[^\"]+")(.*)/\1"8GiB"\3/' /etc/ImageMagick-6/policy.xml
#
# Substitute temporary path with TEMPORARY_FOLDER in /etc/ImageMagick-6/policy.xml
# sudo sed -E -i 's/(^.*policy.*resource.*temporary-path.*value=)("[^\"]+")(.*)/\1"\/TEMPORARY_FOLDER"\3/' /etc/ImageMagick-6/policy.xml
#
folder="";
file=""
crop="";
temp=$(mktemp -d)
mkdir $temp/step1
mkdir $temp/step2
mkdir $temp/step3
function usage() {
echo "Usage:"
echo ""
echo " one-page-prtsc-to-pdf PRTSC_FOLDER OUTPUT_FILE [CROP_AREA]";
echo ""
echo "PRTSC_FOLDER: a folder containing one page printscreens in PNG format"
echo "OUTPUT_FILE: a name for the output PDF file"
echo "CROP_AREA: a selected area to be cropped in this format: 'WIDTHxHEIGHT+X+Y' (example: 1600x1080+160+0)"
}
if [[ -n "$1" ]];
then
folder="$1"
else
usage;
exit 1
fi;
if [[ -n "$2" ]];
then
file="$2"
else
usage;
exit 1
fi;
if [[ -n "$3" ]];
then
crop="$3"
else
crop="100%x100%+0+0"
fi;
count=0;
IFS=$'\n';
radius="1";
echo "Processing images in '$folder'."
for i in `find "$folder" -type f -name "*.png" | sort`
do
count=`expr $count + 1`
index=$(printf "%04d" $count)
echo "$index $i"
# Select the desired area using crop operation
convert -crop "$crop" +repage "$i" "$temp/step1/$index.png" || exit 1
# Sharpen pages using unsharp algorithm
convert -unsharp "0x$radius" +repage "$temp/step1/$index.png" "$temp/step2/$index-0.png" || exit 1
done
echo "Generating output file '$file'."
convert -page a4 -format pdf "$temp/step2/*" "$file" || exit 1
echo "Done."
#!/bin/bash
# Exemplo:
# one-page-run.sh "Titulo - Autor"
OUTPUT_FILE=${1-OUTPUT_FILE}.pdf
./one-page-prtsc-to-pdf.sh PRTSC_FOLDER "${OUTPUT_FILE}"
#!/bin/bash
#
# Convert a folder containing two page printscreens in PNG format to a PDF file.
#
# Before using this script, you have to make print screens from the book you want to convert to PDF.
#
# This script processes all print screens and generates a PDF file with A4 page size.
#
# If the output file is too big, you can simply print it to file to generate a smaller version of the output file.
#
# Usage:
#
# two-page-prtsc-to-pdf PRTSC_FOLDER OUTPUT_FILE [CROP_AREA]
#
# PRTSC_FOLDER: a folder containing two page printscreens in PNG format
# OUTPUT_FILE: a name for the output PDF file
# CROP_AREA: a selected area to be cropped in this format: 'WIDTHxHEIGHT+X+Y' (example: 1600x1080+160+0)
#
# Dependency:
# ImageMagick's convert tool
#
# ImageMagick settings:
#
# Make a backup of the original settings
# sudo cp /etc/ImageMagick-6/policy.xml /etc/ImageMagick-6/policy.xml.original
#
# Allow PDF conversion to ImageMagick
# https://www.kb.cert.org/vuls/id/332928/
# https://stackoverflow.com/questions/52998331/
# sudo sed -E -i 's/^.*policy.*coder.*none.*PDF/<!--\0-->/' /etc/ImageMagick-6/policy.xml
#
# Increase maximum disk usage policy to 8GiB
# https://stackoverflow.com/questions/31407010/
# sudo sed -E -i 's/(^.*policy.*resource.*disk.*value=)("[^\"]+")(.*)/\1"8GiB"\3/' /etc/ImageMagick-6/policy.xml
#
# Substitute temporary path with TEMPORARY_FOLDER in /etc/ImageMagick-6/policy.xml
# sudo sed -E -i 's/(^.*policy.*resource.*temporary-path.*value=)("[^\"]+")(.*)/\1"\/TEMPORARY_FOLDER"\3/' /etc/ImageMagick-6/policy.xml
#
folder="";
file=""
crop="";
temp=$(mktemp -d)
mkdir $temp/step1
mkdir $temp/step2
mkdir $temp/step3
function usage() {
echo "Usage:"
echo ""
echo " two-page-prtsc-to-pdf PRTSC_FOLDER OUTPUT_FILE [CROP_AREA]";
echo ""
echo "PRTSC_FOLDER: a folder containing two page printscreens in PNG format"
echo "OUTPUT_FILE: a name for the output PDF file"
echo "CROP_AREA: a selected area to be cropped in this format: 'WIDTHxHEIGHT+X+Y' (example: 1600x1080+160+0)"
}
if [[ -n "$1" ]];
then
folder="$1"
else
usage;
exit 1
fi;
if [[ -n "$2" ]];
then
file="$2"
else
usage;
exit 1
fi;
if [[ -n "$3" ]];
then
crop="$3"
else
crop="100%x100%+0+0"
fi;
count=0;
IFS=$'\n';
radius="1";
echo "Processing images in '$folder'."
for i in `find "$folder" -type f -name "*.png" | sort`
do
count=`expr $count + 1`
index=$(printf "%04d" $count)
echo "$index $i"
# Select the desired area using crop operation
convert -crop "$crop" +repage "$i" "$temp/step1/$index.png" || exit 1
# Split the selected area in two pages
convert -crop '50%x100%' +repage "$temp/step1/$index.png" "$temp/step2/$index.png" || exit 1
# Sharpen pages using unsharp algorithm
convert -unsharp "0x$radius" +repage "$temp/step2/$index-0.png" "$temp/step3/$index-0.png" || exit 1
convert -unsharp "0x$radius" +repage "$temp/step2/$index-1.png" "$temp/step3/$index-1.png" || exit 1
done
echo "Generating output file '$file'."
convert -page a4 -format pdf "$temp/step3/*" "$file" || exit 1
echo "Done."
#!/bin/bash
# Exemplo:
# two-page-run.sh "Titulo - Autor"
OUTPUT_FILE=${1-OUTPUT_FILE}.pdf
./two-page-prtsc-to-pdf.sh PRTSC_FOLDER "${OUTPUT_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment