Skip to content

The script is overly complex for what it does and should also use arguments as input/ouput parameters #2

Description

@systemofapwne

First, I have to thank you for your script, since it pointed me towards the steps on properly converting colorized PDFs to monochrome PDFs.

However, the script looks extremely overcomplex and written by someone, who tinkered around "until it works" without knowing the full potential of bash scripting. I do not blame you for that: We all began like that.
This is why I wanted to contribute with a simplified and optimized version, that does everything way more elegant and also faster, utilized parallel processing.

IN=my_colorized.pdf
OUT=./out.pdf

# Make sure, we have some temp directory to work in
TMP="/tmp/pdfimg"
mkdir -p $TMP

# Convert PDF to single images
pdfimages ${IN} ${TMP}/im

# Convert with limited amount of threads using parallel
THREADS=4
find ${TMP} -name "*.ppm" | parallel -I% -j ${THREADS} --max-args 1 convert -monochrome % %.pdf

# Combine
pdfunite ${TMP}/*.pdf ./${OUT}

# Cleanup
rm ${TMP}/*

The only additional dependency here is GNU parallel, that distributes the BW conversion tasks to multiple threads in order to speed it up.

I also suggest, that you follow the unix philosophy of using arguments as input parameters to your program.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions