Ali
Ali

Reputation: 443

Linux tools: How to apply a filter on a PDF document?

I am a researcher and I have to read research papers. Unfortunately, the characters are not dark enough, so the papers are hard to read when printed on the paper. Note that the printer's cartridge has no problem, but the characters are not printed dark enough (the text is already in black: take a look at a sample).

This is how the characters look like in Photoshop:

[enter image description here]

Note that the background is transparent when you import a PDF document in photoshop.

I use this awful solution:

First, I Import the PDF document into Photoshop. The pages are imported as individual images with transparent background.

Then, for each page I do either of these two methods:

  1. Method 1: Copy the layer over itself multiple times, so that the image gets darker
  2. Method 2: Apply a Min filter on the image

This is how it looks like after conversion (left: Min filter, right: layer duplication)

[This is how it looks like after conversion (left: Min filter, right: layer duplication)]

This solves my problem for printing a single page and I can read the printed contents easily. However, it is hard to convert every page of every PDF paper using PHOTOSHOP!!!!. Is there any wiser solution/tool/application ???

Here is what I need: 1. How to convert PDF to high-quality image (either in Linux or Windows, with any tool). 2. How to apply Min Filter (or any better filter) on the image files automatically. (e.g. a script or whatever)

Thanks!

Upvotes: 0

Views: 638

Answers (1)

Ali
Ali

Reputation: 443

It can be done using four tools:

  1. pdftoppm: Convert PDF file to separate png images
  2. octave (MATLAB's open-source alternative): Generate an octave script that applies the min filter on the images. After running the script,
  3. pdflatex: Create a single .tex file that imports all the images, then compile the .tex file to obtain a single PDF
  4. bash!: A bash script that automates the process

Here is a fully automated solution as a single bash script:

pdftoppm  -rx 300 -ry 300   -png $1 img

# Create an octave script than applieds min-filter to all files.
echo "#!/usr/bin/octave -qf" > script.m
echo "pkg load image" >> script.m
for i in `ls -1 img*.png`
do
    echo "i = imread('$i');" >> script.m 
    echo "i(:,:,1) = ordfilt2(i(:,:,1),1,ones(3,3));" >> script.m 
    echo "i(:,:,2) = ordfilt2(i(:,:,2),1,ones(3,3));" >> script.m 
    echo "i(:,:,3) = ordfilt2(i(:,:,3),1,ones(3,3));" >> script.m 
    echo "imwrite(i,'p$i')" >> script.m 
done

# Running the octave script
chmod 755 script.m
./script.m

# Converting png images to a single PDF
# Create a latex file that contains all image files

echo "\documentclass{article}" > f.tex
echo "\usepackage[active,tightpage]{preview}" >> f.tex
echo "\usepackage{graphicx}" >> f.tex
echo "\PreviewMacro[{*[][]{}}]{\includegraphics}" >> f.tex
echo "\begin{document}" >> f.tex
echo -n "%" >> f.tex
for i in `ls -1 pimg*.png`
do
    echo "\newpage" >> f.tex
    echo "\includegraphics{"$i"}" >> f.tex
done
echo "\end{document}" >> f.tex


#Compiling the latex document
pdflatex -synctex=1 -interaction=nonstopmode f

Upvotes: 1

Related Questions