Using UTF text by ImageMagick - image

I use Ubuntu 14.0.4. I want to use a file with UTF8 by a command of ImageMagick. Here you can see:
If the UTF-8 text you wanting to draw has already been generated you
can read it directly from a file using '#filename'.For example here I
create a Chinese label from a UTF-8 encoded Chinese text file (without
a final newline in the file).
convert -background lightblue -fill blue -pointsize 48 \
-font ZenKaiUni label:#chinese_words.utf8 label_utf8.gif
Now I test a command that is look like above command for a text file with name label1.txt which it's content is a Persian word:
سلام
Command is:
convert -background lightblue -fill blue -pointsize 48 label:#label1.txt label_utf8.png
But I got this image:
You can see ? characters instead of desired ones. How can I solve this problem?

Firstly, you need to specify the font for ImageMagick to use, so you want something like:
convert -background lightblue -fill blue -pointsize 48 -font XYZ ...
In order to make sure that your selected font is known to ImageMagick, you need to look in the list of known fonts:
identify -list font
or, more specifically in your case:
identify -list font | grep -i Kacst
If the font is listed, use that name. If not listed, refer to this answer.

Related

ImageMagick - batch file - Font does'n change

I have this code in ImageMagick, it works except for changing the font. I tried all kinds of options, I don't understand what I could do to change the font to the font I want.
Besides that, how can I give opacity to ``-undercolor "#E70E0E"```?
magick convert 13.jpg -fill white -undercolor "#E70E0E" -gravity Center -font "Comic Sans MS" -pointsize "75" -annotate +0+5 "Title-text" -gravity Center -pointsize "25" -font "Calibri Light" -annotate +0+100 "SubTitle-Text" -crop 1920x1080+0+0 -scale 1920x1080+0+0 picture.jpg
thank you so much
Possibly missing dashes in the font names (Calibri-Light).
You can list the fonts known to IM using magick identify -list font.

Use greek letters in adding annotation in convert imagemagick

I need to annotate some label/text containing greek letters on a figure using convert from imagemagick on linux.
What is the best option?
Trivial choices such as:
ii=1
label="α β $ii"
convert in.png -fill black -annotate "$label" out.png
won't work.
I am not familiar with font coding options.
One way that works for me in ImageMagick 6.9.9.23 Mac OSX Sierra is to use a Greek font. (The symbol font also works)
ii=1
label="a b $ii"
convert logo: -font GreekMathSymbols -pointsize 64 -gravity center -fill red -annotate +0+0 "$label" out.png
Alternately, use a UTF-8 compatible text editor and type your text using a font that supports those characters. For example I put your alpha beta one characters into a text file using the same GreekMathSymbols font.
Then
convert logo: -font GreekMathSymbols -pointsize 64 -gravity center -fill red -annotate +0+0 "#greek.txt" out2.png
See also http://www.imagemagick.org/Usage/text/#unicode

Split a Bash script with ImageMagick instructions into smaller pieces?

I have this bash script, feeding drawing information into ImageMagick, which starts like this:
#!/bin/bash
convert -size 2200x2200 xc:white \
-fill '#FFFEFF' -draw 'point 1112,1111' \
-fill '#FFFEFE' -draw 'point 1112,1112' \
-fill '#FFFEFE' -draw 'point 1111,1112' \
-fill '#FFFEFE' -draw 'point 1110,1112' \
-fill '#FFFEFE' -draw 'point 1110,1111' \
******ON & ON 4.2 MILLION LINES MORE*******
spectrumspiral.png;
My problem is I keep getting warnings about argument list too long, terminal says 'Killed', warning about 'fork: cannot allocate memory etc.
I've tried adjusting ulimit -s to a much higher value to no avail. Really want to make this image. Any idea how I can feed the terminal chunks of this script at a time? Or something to that end.
I've heard xargs can be used for things like this, but I haven't been able to find a specific implementation that fits the nature of this problem.
If you create an image with 4 pixels in it (1 red, 1 white, 1 blue and 1 black), you can tell ImageMagick to output the resulting image as text file as follows:
convert -size 1x1 xc:red xc:white xc:blue xc:black +append -depth 8 -colorspace RGB image.txt
# ImageMagick pixel enumeration: 4,1,255,rgb
0,0: (255,0,0) #FF0000 rgb(255,0,0)
1,0: (255,255,255) #FFFFFF rgb(255,255,255)
2,0: (0,0,255) #0000FF rgb(0,0,255)
3,0: (0,0,0) #000000 rgb(0,0,0)
By the same token, if you feed that text file into ImageMagick, it can recreate the image:
cat image.txt | convert txt:- output.img
So, all we need to do is convert your -fill commands into a text file of the format that ImageMagick likes. So we can do this to yourFile
sed -E 's/.*(#[0-9A-F]+).* ([0-9]+)\,([0-9]+).*/\2,\3:\1/g' yourFile
and we will get something like this:
0,0:#3C4AD8
0,1:#269531
0,2:#CF2C7C
...
...
which we can then pipe into awk to rearrange how ImageMagick likes it:
awk -F: '
BEGIN{print "# ImageMagick pixel enumeration: 2200,2200,255,rgb"}
{
coords=$1;colour=$2
rh="0x" substr(colour,2,2);
gh="0x" substr(colour,4,2);
bh="0x" substr(colour,6,2);
r=strtonum(rh);
g=strtonum(gh);
b=strtonum(bh);
printf "%s: (%d,%d,%d) %s\n",coords,r,g,b,colour;
}'
So, if we put all that together, the following script should be able to create your beloved image into the file spectrumspiral.png:
#!/bin/bash
sed -E 's/.*(#[0-9A-F]+).* ([0-9]+)\,([0-9]+).*/\2,\3:\1/g' yourFile | awk -F: '
BEGIN{print "# ImageMagick pixel enumeration: 2200,2200,255,rgb"}
{
coords=$1;colour=$2
rh="0x" substr(colour,2,2);
gh="0x" substr(colour,4,2);
bh="0x" substr(colour,6,2);
r=strtonum(rh);
g=strtonum(gh);
b=strtonum(bh);
printf "%s: (%d,%d,%d) %s\n",coords,r,g,b,colour;
}' | convert txt:- spectrumspiral.png
Try using the shell's here-document
#!/bin/bash
convert - <<EOS spectrumspiral.png
-size 2200x2200 xc:white
-fill '#FFFEFF' -draw point '1112,1111'
-fill '#FFFEFE' -draw 'point 1112,1112'
-fill '#FFFEFE' -draw 'point 1111,1112'
-fill '#FFFEFE' -draw 'point 1110,1112'
-fill '#FFFEFE' -draw 'point 1110,1111'
# ******ON & ON 4.2 MILLION LINES MORE*****
EOS
My system doesn't have convert so I can test that this works.
Most linux programs (including convert) can accept input from StdIn, which in the cmd-line above is represented by the - char. The - tells the command to expect input, not from file, but as if it was being typed at the keyboard, ie StdIn. The <<EOS .... EOS is the here-doc, and it represents bash 'typing' in all of that text into the commands stdIn input.
You may or may not need all of the quoting, if you already have there, I don't think it will hurt. If this doesn't work as is, use a small sample file of input (like above) and test various scenarios until it creates a file for you.
IHTH

Converting Text to Image

I want to convert given text, say TEST, to image in different font and style.
I am using Imagemagick's convert command
convert -pointsize 120 -font Courier label:'TEST' test.png
But when I am using -style in the same, it is not changing into different style like Normal, Bold, Italic.
Please suggest how to get all 3 style.
I think -style (e.g. Italic) and -weight (e.g. Bold) only work for certain fonts like postscript ones (unable to quote you a definitive source though).
What I use is fonts like Courier-New-Bold, Courier-New-Italic, Courier-New-Bold-Italic.
e.g.:
convert -pointsize 120 -font Courier label:'TEST' test.png
convert -pointsize 120 -font Courier-New-Bold label:'TEST' testbold.png
convert -pointsize 120 -font Courier-New-Italic label:'TEST' testitalic.png
convert -pointsize 120 -font Courier-New-Bold-Italic label:'TEST' testbolditalic.png
Most fonts have the -Bold, -Italic, and -Bold-Italic versions.
To see what fonts you can use, do convert -list font (or convert -list type if your ImageMagick is older). I do convert -list font | grep Font: to get a condensed list of just font names.

make all values but one in large raster file 0

I have a large raster file downloaded from Earth Engine. I want to turn it to a boolean file, keeping only one value (13) and make all other values either NA or 0. The file is so large it crashes QGIS and ArcMap when I try to process it, is there a way to do this using GDAL or bash? The file is a tif file.
You can do that with ImageMagick which is installed on most Linux distros and is available for macOS and Windows.
Make a test image at the command line - value 13 in the middle, red and blue around that:
convert -size 30x20 xc:"gray(13)" \
-bordercolor red -border 10 \
-bordercolor blue -border 10 start.tif
Now fill with black, everything that is not value 13:
convert start.tif -fill black +opaque "gray(13)" result.tif
Or, somewhat easier to see - fill everything that is not value 13 with cyan and change everything that is value 13 to yellow:
convert start.tif \
-fill cyan +opaque "gray(13)" \
-fill yellow -opaque "gray(13)" result.tif
Here's a gdal solution:
Your input is input.tif:
gdal_calc.py --calc="A==13" -A input.tif --type=Byte --outfile=output.tif
With R you can do
library(raster)
library(rgdal)
r <- raster("input.tif")
x <- calc(r, function(i){ i==13 }, filename="output.tif", datatype="INT1U")
Or use raster::reclassify

Resources