add text to image with convert - bash

I am being stuck at some image editing using bash commands.
I have 414 images labelled 1file.png 2file.png ... 414file.png. To each of these I would like to add the number, which is already present in the name. The goal is to create a .gif which will show the counts of images in one corner counting up. (Creating the .gif using convert * result.gif works)
The problem is it will not add my number in the image but literally print $i. Does somebody know how to read in the loop variable?
I have tried:
for i in {0..9}; do
convert -pointsize 80 -fill black -draw 'text 1650 400 "$i"' "$i"file.png "$i"file.png;
done
Many thanks in advance.

Problem solved:
for i in {0..1}; do
convert -pointsize 80 -fill black -draw 'text 1650 400 '\"$i\"'' "$i"file.png "$i"file.png;
done

Related

Graphicsmagick Unrecognized option (-annotate)

I am trying to add some text in image. I am using following command:
convert - -pointsize 37 -font fonts/source-sans-pro-regular.ttf -fill "#FFFFFF80" -gravity SouthEast -annotate +48+48 "some text"
but I am getting:
convert convert: Unrecognized option (-annotate).
Version of graphicsmagick is:
GraphicsMagick 1.4 snapshot-20210721 Q16
What can be a problem?
I see two issues here.
Firstly, GraphicsMagick commands start with gm, i.e.
gm convert ...
gm mogrify ...
gm identify ...
Secondly, the gm convert command does not have an option -annotate, see list of supported options here.
Maybe you meant to install ImageMagick which would:
work without gm prefix, as you expected, and
accept the -annotate option you hoped to use.

Basic ImageMagic disconnect: converting plain ASCII.txt file into image file ASCII.jpg - ending up with errors

I have been trying to convert a plain text file (containing ASCII sentences / lines) into an JPEG image - not caring about any formats or styles - just a first try - keen on quick results:
After half a day trials and errors in all variations - all failing with the below (or similar) errors - and nearly going mad I have solved the following error message :
...
convert-im6.q16: not authorized `#a.txt' # error/property.c/InterpretImageProperties/3516.
I have gotten the wanted result when I have deactivitated
/etc/ImageMagick-6/policy.xml
into
/etc/ImageMagick-6/_policy.xml
Can somebody explain - please - what the magic behind is with this (deactivated) file ???
And please - being sure this file should become re-activated as it is by default - what do I need to change instead (in the file ???)
Thanks in advance with BR
If you are not on a shared server, you can edit your ImageMagick policy.xml file to give read|write permissions for the use of "#".
I suspect the defaults are still "none" for the use of "#" even if you disable the policy.xml file.
Then you can read a text file (in this case ipsum_lorem.txt) :
convert -size 1000x -font arial -pointsize 28 caption:"#ipsum_lorem.txt" x.jpg
or
cat ipsum_lorem.txt | convert -size 1000x -font arial -pointsize 28 caption:"#-" x.jpg
and the result should be:

Filling the gaps made in chinese character due to line removal for ocr

Hello friends,
I have a hard time to ocr the above image due to the gaps that were made due to line removal.So could anyone kindly guide me on how to fill the gaps in chinese character using imagemagick
Cool question! There are many ways of approaching this but unfortunately I can't tell which ones work! So I'll give you some code and you can experiment by changing it around.
For the moment, I tried simply removing any lines that have white pixels in them, but you could look at the lines above and below, or do something else.
#!/bin/bash -xv
# Get lines containing white pixels
convert chinese.gif -colorspace gray -threshold 80% DEBUG-white-lines.png
# Develop that idea and get the line numbers in an array
wl=( $(convert chinese.gif -colorspace gray -threshold 80% -resize 1x\! -threshold 20% txt: | awk -F '[,:]' '/FFFFFF/{print $2}') )
# White lines are:
echo "${wl[#]}"
# Build a string of a whole load of "chop" commands to apply in one go, rather than applying one-at-a-time and saving/re-loading
# As we chop each line, the remaining lines move up, changing their offset by one line - UGHH. Apply a correction!
chop=""
correction=0
for line in "${wl[#]}" ; do
((y=line-correction))
chop="$chop -chop 0x1+0+$y "
((correction=correction+1))
done
echo $chop
convert chinese.gif $chop result.png
Here's the image DEBUG-white-lines.png:
The white lines are identified as:
44 74 134 164 194 254 284 314 374 404
The final command run is:
convert chinese.gif -chop 0x1+0+44 -chop 0x1+0+73 -chop 0x1+0+132 -chop 0x1+0+161 -chop 0x1+0+190 -chop 0x1+0+249 -chop 0x1+0+278 -chop 0x1+0+307 -chop 0x1+0+366 -chop 0x1+0+395 result.png
If I understand this correctly then you want to find a way of removing the white lines and then still get it to go through an OCR?
The best way would be by eye and connect the dots so to speak so the last pixel of the characters line up.
A programitcal way would be to remove the white line ad then duplicate the line above (or below) and shift it into place.
康 家 月 而 视 , 喝 道
" 你 想 做 什 么 !"
秦 微 微 一 笑 , 轻 声 道
不 知 道 看 着 些 亲 死 眼 前 ,
前 辈 会 不 会 有 痛 的 感 觉 。"
说 , 伸 手 一 指 , 一 位 少 妇
身 形 一 顿 , 小 出 现 了 一 个 血 洞
倒 地 身 广 。
康 家 相 又 惊 又 , 痛 声 道
I don't read Chinese but this is what it got machine translated as
Kang Jia month and watch, drink
"What do you want to do !"
Qin Weiwei smiled, softly
I don't know. look at some dead eyes. ,
Predecessors will not feel pain ."
And said, stretch out a finger , a young woman.
In The Shape of a meal, a small blood hole appeared
Down to the ground wide.
The Kang family was shocked and sore

Imagemagick Batch operation conditional on Filesize - How?

I'm running Imagemagick on a command line Ubuntu terminal in Windows 10 - using the built in facility in Windows 10 - the Ubuntu App.
I am a complete linux novice but have installed imagemagick in the above environment.
My task - Auto remove the black(ish) border and deskew the images of thousands of scanned 35mm slides.
I can successfully run commands such as
mogrify -fuzz 35% -deskew 80% -trim +repage *.tif
The problem is:-
The border is not crisply defined nor is completely black, hence the -fuzz. Some images are over-trimmed at a certain fuzz, while others are not trimmed enough.
So what I want to do is to have two passes at this, with different fuzz %, for these reasons:-
1st pass with a low Fuzz%. Many images will not be trimmed at all but I have found that the ones that are susceptible to over-trimming will trim Ok with low %
Since all the images start with an identical filesize, the ones that have trimmed Ok will have a lower filesize (note these are tifs not jpgs)
So what I need to do is set a file size condition for the second pass at higher fuzz% THAT IGNORES file sizes below a certain value and does not perform any operation.
In this way, with few errors, all the images will be trimmed correctly.
So the question
- How can I adjust the command line to have 2 passes and to ignore a lower file size on the second pass?
I have a horrible feeling the the answer will be a script. I have no idea how to construct or set up Ubuntu to run this so if so, please can you point me to help for that also!!
In ImageMagick, you could do something like the following:
Get the input filesize
Use convert to deskew and trim.
Then find the new file
Then compare the new to the old to compute the percentdifference to some percent threshold
If the percent difference is less than some threshold, then the processing did not trim enough
So reprocess with a higher fuzz value and write over the input; otherwise keep the first one only and do not write over the old one.
Unix syntax.
Choose two fuzz values
Choose a percent change threshold
Create a new empty directory to hold the output (results)
cd
cd desktop/Originals
fuzz1=20
fuzz2=40
threshpct=10
list=`ls`
for img in $list; do
filesize=`convert -ping $img -precision 16 -format "%b" info: | sed 's/[B]*$//'`
echo "filesize=$filesize"
convert $img -background black -deskew 40% -fuzz $fuzz1% ../results/$img
newfilesize=`convert -ping ../results/$img -precision 16 -format "%b" info: | sed 's/[B]*$//'`
test=`convert xc: -format "%[fx:100*($filesize-$newfilesize)/$filesize<$threshpct?1:0]" info:`
echo "newfilesize=$newfilesize; test=$test;"
[ $test -eq 1 ] && convert $img -background black -deskew 40% -fuzz $fuzz2% ../results/$img
done
The issue is that you need to be sure you set your TIFF compression for the output the same as for the input so that the file sizes are equivalent and presumably the new size is not larger than the old one as happens with JPG.
Note that the sed is used to remove the letter B (bytes) from the file size, so they can be compared as numerals and not strings. The -precision 16 forces "%b" to report as B and not KB or MB.

Image optimization for OCR

I'm using tesseract v 3.02
I have the following image
Download Image (right click -- save link as)
I would like to get the text from it. I'm using tesseract for the purpose.
When writing this command:
tesseract cropped.png tess -psm 7
The result I get is "suackea I 30 10193020 NL 3 e 1 64 :23 23% 34% 120". While the end is ok, the beginning is incorrect. The expected result is:
"Strackea III €0.10/€0.20 NL 6 6 1 €4 €23 23% 34% 120"
I have tried to do some transformation using imageMagick before tesseract to get an image with text written in black on a white background:
convert cropped.png -fuzz 28000 -fill black -opaque white cropped.png
convert cropped.png -fuzz 25000 -fill white -opaque rgb(118,118,118) cropped.png
The resulting image is
Download Image (right click -- save link as)
tesseract cropped.png tess -psm 7
but the result is the same.
What transformation or other command line tool would you use to recognize the text correctly ?)
The font the text is written in is Microsof Sans Serif

Resources