OCRmyPDF.sh: prepare intelligent image extraction
preparation of extraction of the image in the same resolution than the original image inside the pdf file
This commit is contained in:
+22
-2
@@ -20,8 +20,23 @@ page="1"
|
||||
while [ $page -le $numpages ]; do
|
||||
|
||||
# extract image from current pdf page
|
||||
echo "Page $page: Extracting image from PDF"
|
||||
pdftk "$infile" cat $page output "${tmp}/${page}.pdf" > /dev/null
|
||||
|
||||
# get the size of the pdf (width / height) page in pt (inch*72)
|
||||
# identify -format "return %w, %h\n" "${tmp}/${page}.pdf"
|
||||
|
||||
# extract image from pdf file (keeping the resolution as available in the pdf file)
|
||||
# pdfimages -f 1 -l 1 "${tmp}/${page}.pdf" ${page} 1>&2
|
||||
|
||||
# itterate the extracted images (there can be more than 1 image on the page)
|
||||
# and get there respective number of x/y pixel
|
||||
# identify -format "return %w, %h\n" img1-xxx.ppm
|
||||
|
||||
# compute the resolution of the whole page (taking into account all images)
|
||||
|
||||
|
||||
# extract the image with the right resolution
|
||||
echo "Page $page: Extracting image from PDF"
|
||||
pdftoppm -r $dpi "${tmp}/${page}.pdf" > "${tmp}/${page}.ppm"
|
||||
|
||||
# improve quality of the image to get better ocr results
|
||||
@@ -35,9 +50,13 @@ while [ $page -le $numpages ]; do
|
||||
tesseract -l ${lan} "${tmp}/${page}.forocr.ppm" "${tmp}/${page}.hocr" hocr 1> /dev/null 2> /dev/null
|
||||
cp "${tmp}/${page}.hocr.html" "${tmp}/${page}.hocr"
|
||||
|
||||
# compress image to be put inside the pdf file
|
||||
echo "Page $page: Compress image for final PDF file"
|
||||
convert "${tmp}/${page}.forocr.ppm" "${tmp}/${page}.forpdf.jpg"
|
||||
|
||||
# embed text and image to new pdf file
|
||||
echo "Page $page: Embedding text in PDF"
|
||||
python hocrTransform.py -r 300 -i "${tmp}/${page}.forocr.ppm" "${tmp}/${page}.hocr" "${tmp}/${page}-ocred.pdf"
|
||||
python hocrTransform.py -r $dpi -i "${tmp}/${page}.forpdf.jpg" "${tmp}/${page}.hocr" "${tmp}/${page}-ocred.pdf"
|
||||
|
||||
# go to next page of the pdf
|
||||
page=$(($page+1))
|
||||
@@ -48,6 +67,7 @@ done
|
||||
pdftk ${tmp}/*-ocred.pdf cat output "${tmp}/ocred.pdf"
|
||||
|
||||
# insert metadata
|
||||
# TODO
|
||||
|
||||
# validate generated pdf file (compliance to PDF/A)
|
||||
#java -jar jhove/bin/JhoveApp.jar -m PDF-hul "$1" |egrep "Status|Message"
|
||||
|
||||
Reference in New Issue
Block a user