OpticalCharacterRecognition:Example:JpgToPdf
lastmod 2025-02-14created 2019-01-275 edits
Describes how to convert text made up of JPEG files into PDF using OCR.
How to
Place the files in one folder as follows.
$ ls
001.jpg 060.jpg 119.jpg 178.jpg 237.jpg 296.jpg 355.jpg 414.jpg 473.jpg 532.jpg 591.jpg
002.jpg 061.jpg 120.jpg 179.jpg 238.jpg 297.jpg 356.jpg 415.jpg 474.jpg 533.jpg 592.jpg
003.jpg 062.jpg 121.jpg 180.jpg 239.jpg 298.jpg 357.jpg 416.jpg 475.jpg 534.jpg 593.jpg
004.jpg 063.jpg 122.jpg 181.jpg 240.jpg 299.jpg 358.jpg 417.jpg 476.jpg 535.jpg 594.jpg
...
Write the following script in the same folder. ImageMagick is used for the conversion.
#!/bin/bash
if [[ -z $1 || -z $2 ]]; then
echo "$0 {begin_index} {end_index}"
exit 1
fi
BEGIN_INDEX=$1
END_INDEX=$2
INDEX=0
FILE_LIST=
for cursor in *.jpg; do
if [[ $INDEX -ge $BEGIN_INDEX && $INDEX -le $END_INDEX ]]; then
FILE_LIST="$FILE_LIST $cursor"
fi
let "INDEX = INDEX + 1"
done
convert $FILE_LIST -resize 100% -quality 10 conv/$BEGIN_INDEX-$END_INDEX.pdf
If there are about 600 images in total, run it as follows.
$ ./jpg2pdf.sh 0 50
$ ./jpg2pdf.sh 51 100
$ ./jpg2pdf.sh 101 150
$ ./jpg2pdf.sh 151 200
$ ./jpg2pdf.sh 201 250
$ ./jpg2pdf.sh 251 300
$ ./jpg2pdf.sh 301 350
$ ./jpg2pdf.sh 351 400
$ ./jpg2pdf.sh 401 450
$ ./jpg2pdf.sh 451 500
$ ./jpg2pdf.sh 501 550
$ ./jpg2pdf.sh 551 600
$ ./jpg2pdf.sh 601 650
The PDFs are made in units of 50 pages in order to use the Google OCR service. (See Convert PDF and photo files to text - Computer - Google Drive Help)
In short, the following conditions must be met.
- Format: You can convert .JPEG, .PNG, .GIF, and PDF (multi-page document) files.
- File size: The file size must be 2 MB or less.
- Resolution: Text must be at least 10 pixels high.
- Orientation: The document must be oriented correctly. If the image is facing the wrong way, rotate it before uploading it to Google Drive.
- Language: Google Drive detects the language used in the document.
- Fonts and character set: You get better results with common fonts such as Arial or Times New Roman.
- Image quality: Works best with bright, sharp images with clear contrast.
Finally, convert the image files as follows.
- On your computer, go to drive.google.com.
- Right-click the file you want to convert.
- Click Open with, then Google Docs.
- The image file is converted to a Google Doc, but some formatting may not transfer.
- Bold, italics, font size, font type, and line breaks are usually kept.
- Lists, tables, columns, footnotes, and endnotes are usually not kept.