Unicode symbol to image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
federicorico
Posts: 6
Joined: 2018-05-07T22:37:23-07:00
Authentication code: 1152

Unicode symbol to image

Post by federicorico »

This is about transforming any symbol to an image, so the rich catalog of numbers, characters and icons is available in a graphic format. The `convert` command does the job nicely, like this:

Code: Select all

convert -size 50x50 xc:transparent -pointsize 30 -annotate +10+33 "O" output.png
Now, this generates a black character on a transparent canvas. In order to also handle colors, there should be foreground and background colors, mapping the symbol's black to a specified color, and the area surrounded by black to any other color. For example, this would generate a (black over transparent) football ball:

Code: Select all

convert -size 50x50 xc:transparent -font "/usr/share/fonts/truetype/ancient-scripts/Symbola_hint.ttf" -pointsize 30 -fill black -annotate +10+33 "⚽" ball.png
What would be the parameters to change black, and inner white into any given colors, leaving the surrounding area transparent?

snibgo
Posts: 12648
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Unicode symbol to image

Post by snibgo »

Most fonts have only one colour, the "-fill" colour (typically black). I don't think IM can render fonts in more than one colour.
snibgo's IM pages: im.snibgo.com

federicorico
Posts: 6
Joined: 2018-05-07T22:37:23-07:00
Authentication code: 1152

Re: Unicode symbol to image

Post by federicorico »

Not sure I get this answer. This is about transforming a symbol (of a given font) into a graphic file. I can change the color to white, for example, with `-fill white`. But `-fill red` wont' work, I don't know why. Besides, there are ways to detect that an area is surrounded by black, if IM can do it, then it's only colouring it differently.
Last edited by federicorico on 2018-05-08T08:04:37-07:00, edited 1 time in total.

snibgo
Posts: 12648
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Unicode symbol to image

Post by snibgo »

I thought your question was about using fonts. Perhaps it isn't.

If you have any image that uses black, white and shades of gray you can change it to any pair of colours (and colours between those) with "+level-colors". See http://www.imagemagick.org/script/comma ... vel-colors

Does that help?
snibgo's IM pages: im.snibgo.com

federicorico
Posts: 6
Joined: 2018-05-07T22:37:23-07:00
Authentication code: 1152

Re: Unicode symbol to image

Post by federicorico »

It is not about using fonts in the usual way, but to provide a collection of characters and icons that can be transformed into graphic files, to be used in games, for example. The source just has black and levels of transparency to represent any symbol. I fail to see how to use `-level-colors` here...
To be precise, given the first example:

Code: Select all

convert -size 50x50 xc:transparent -pointsize 30 -annotate +10+33 "O" output.png
how to parameterize the command to obtain a PNG file where the 'O' is colored in blue, and the inner part of the 'O' is colored in yellow, for example, while the outer part remains transparent.

muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: Unicode symbol to image

Post by muccigrosso »

This slightly simpler command works for me and uses whatever color I want with -fill:

Code: Select all

convert -size 50x50 xc:transparent -pointsize 30 -fill red -annotate +10+33 "O" show:
ImageMagick 7.0.7-30 Q16 x86_64 2018-05-06

User avatar
fmw42
Posts: 26050
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Unicode symbol to image

Post by fmw42 »

You cannot fill the inside of the O with anything but the xc: color unless you specifically use -draw to flood fill the center of the O. So you need to know where the center of the O is located. Here I create two versions of the O with different background colors (transparent and yellow). I extract the alpha channel of the first one and flood fill the center of the O with white and modify the size of the filled O so that it is smaller for the yellow background when composited.

Unix syntax:

Code: Select all

convert -size 50x50 xc:transparent -pointsize 30 -fill blue -annotate +10+33 "O" \
\( -size 50x50 xc:yellow -pointsize 30 -fill blue -annotate +10+33 "O" \) \
\( -clone 0 -alpha extract -fill white -draw "color 5,17 floodfill" -alpha off -morphology dilate diamond:1 -negate \) \
-compose over -composite result.png
For windows, remove the \ from \( .. \) and replace the end of line \ with ^ or close up the multiline command into one line.

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

federicorico
Posts: 6
Joined: 2018-05-07T22:37:23-07:00
Authentication code: 1152

Re: Unicode symbol to image

Post by federicorico »

That answers the second part of the question: inner areas cannot be filled automatically with IM.
As for the first question, the `-fill` command seems to be the key here. Agreed, but the result seems to be different depending on the color used. While `black` and `white` Image values respect the gradual transparency in the source symbol, colors like `red` Image or `yellow` do not. How can this be fixed as to obtain the same result with any color?

snibgo
Posts: 12648
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Unicode symbol to image

Post by snibgo »

federicorico wrote:... inner areas cannot be filled automatically with IM.
That's true because IM doesn't have a definition for "inside".

For you, "inside" might mean a (partially) transparent pixel that is not connected to top-left by a (partially) transparent path.

You want the inside to be yellow and the O to be blue with proper anti-aliasing, the outsize to be transparent, again with anti-aliasing. Windows BAT script:

Code: Select all

magick ^
  -size 50x50 xc:transparent -pointsize 30 -annotate +10+33 "O" ^
  ( +clone ^
    -background White ^
    -layers Flatten ^
    +level-colors blue,yellow ^
    -write mpr:YB ^
    +delete ^
  ) ^
  -alpha extract ^
  -negate -bordercolor Black ^
  ( +clone ^
    -fill Black -draw "color 0,0 filltoborder" ^
  ) ^
  -compose Difference -composite ^
  -negate ^
  mpr:YB ^
  +swap ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  f.png
snibgo's IM pages: im.snibgo.com

federicorico
Posts: 6
Joined: 2018-05-07T22:37:23-07:00
Authentication code: 1152

Re: Unicode symbol to image

Post by federicorico »

You're absolutely right, implementing "inside", that's the point.
I'm trying to test it, but am not familiar with BAT, could you please also provide a Linux version?

snibgo
Posts: 12648
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Unicode symbol to image

Post by snibgo »

Bash script:

Code: Select all

magick \
  -size 50x50 xc:transparent -pointsize 30 -annotate +10+33 "O" \
  \( +clone \
    -background White \
    -layers Flatten \
    +level-colors blue,yellow \
    -bordercolor Blue \
    -fill Blue -draw "color 0,0 filltoborder" \
    -write mpr:YB \
    +delete \
  \) \
  -alpha extract \
  -channel RGB \
  -negate \
  +channel \
  -bordercolor Black \
  \( +clone \
    -fill Black -draw "color 0,0 filltoborder" \
  \) \
  -compose Difference -composite \
  -channel RGB -negate +channel \
  mpr:YB \
  +swap \
  -compose CopyOpacity -composite \
  -scale 1000% \
  fillo.png
This scales up 10 times to help us see the result:
Image
EDIT: I've fixed a bug in the bash script (but not the Windows script).
snibgo's IM pages: im.snibgo.com

federicorico
Posts: 6
Joined: 2018-05-07T22:37:23-07:00
Authentication code: 1152

Re: Unicode symbol to image

Post by federicorico »

It's amazing how IM can be adapted to basically any need.
The only thing that remains: how it would be for more than one regions? For example, '8', or the ball symbol in the second example.
Also, it would be great to have the script commented, as to understand how the whole process is implemented. (For example, I can't see what is related to the antialiasing.)

PS: thanks for providing the Linux version.

Post Reply