0

File manager context menu scripts sometimes do the job far quicker than using a GUI utility. So I've been using dozens of simple and more complex scripts for a long time in file managers Dolphin, Nautilus and Nemo, although I have elementary level scripting skills. However, this time I'm stuck with a very simple loop to OCR selected image file(s) using tesseract in Dolphin, which works in many other scripts:

for filename in "${@}"; do
    tesseract -l eng "$filename" "${filename%.*}"
done

This should normally be executed for a selected image (or each and every one of the selected images) like this, a command which works in Terminal, giving me a text file named "image.txt":

tesseract -l eng "image.png" "image"

Any ideas please???

1 Answer 1

0

I think this might be due to a bug in tesseract. Therefore an alternative solution might be something like this:

for filename in "${@}"; do
name="${filename%.*}"
ext="${filename##*.}"
output="${name}-alpha.${ext}"
convert "$filename" -alpha off "$output"
mv "$output" "$filename"
dpi="$(identify -units "PixelsPerInch" -format "%x\n" "$filename" | xargs printf "%.*f\n" "0")"
ocrmypdf -l eng --image-dpi "$dpi" "$filename" "${name}.pdf"
pdftotext -q "${name}.pdf"
ocr_txt="$(cat "${name}.txt" | sed -e "s///g" -e "s/\t/ /g" -e "s/  / /g" -e "/^\s*$/d")"
kdialog --title "Image OCR Text" --msgbox "$ocr_txt"
rm "${name}.pdf" "${name}.txt"

done

This will remove any transparency (alpha) channel as required by ocrmypdf; pass on the image dpi resolution to it for better results; then use ocrmypdf to convert the image to pdf with an embedded OCR text layer; and finally display that text in a dialog box.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .