text to image problem
text to image problem
I am trying to create a square image of a chessboard by converting a text string using a special chess font:
convert -size 300x300 -fill black -font chessfont.TTF caption:@boardstring.txt out.png
As you can see, the image is not a nicely filled square, but instead has blank space on the right and bottom that I'm forced to chop off. What's more, the chopped image isn't perfectly square, so I'm forced to do a resize to adjust for that.
Setting the font size instead of the image size didn't help:
convert -pointsize 36 -fill black -font chessfont.TTF caption:@boardstring.txt out.png
Having to work out the correct chop numbers for every image size is a big problem. Could someone let me know why the basic converted image isn't coming out right?
convert -size 300x300 -fill black -font chessfont.TTF caption:@boardstring.txt out.png
As you can see, the image is not a nicely filled square, but instead has blank space on the right and bottom that I'm forced to chop off. What's more, the chopped image isn't perfectly square, so I'm forced to do a resize to adjust for that.
Setting the font size instead of the image size didn't help:
convert -pointsize 36 -fill black -font chessfont.TTF caption:@boardstring.txt out.png
Having to work out the correct chop numbers for every image size is a big problem. Could someone let me know why the basic converted image isn't coming out right?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: text to image problem
"-trim" trims it. Other than that, without chessfont.TTF and boardstring.txt, it's difficult to guess. Can you provide those?
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
Try making the board size 8 * the square dimension of your font pointsize. Presumably your font is a monospaced font.
Or leave off the size, use label: with newline breaks \n every eight characters and whatever pointsize you want.
But as user snibgo suggested, it would be best if you provide your font and textfile. You can upload to some free hosting service such as dropbox.com and put the URLs here.
Or leave off the size, use label: with newline breaks \n every eight characters and whatever pointsize you want.
But as user snibgo suggested, it would be best if you provide your font and textfile. You can upload to some free hosting service such as dropbox.com and put the URLs here.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
Here is a simple test, not meant to be realistic.
I got the font from http://www.dafont.com/chess.font
Code: Select all
convert -background white -pointsize 48 -gravity center -font /Library/fonts/CHEQ_TT.TTF label:"BBBBBBBB\nHHHHHHHH\nLLLLLLLL" chess_test.png
I got the font from http://www.dafont.com/chess.font
Re: text to image problem
Thanks guys,
The -trim option fixes everything except for a one pixel wide strip on the right (it unfortunately doesn't look right when a border is added to the image):
I ran the pointsize test with my font and got this:
convert -background white -pointsize 48 -gravity center -font GC2004D_.TTF label:"21212121\n12121212" chess_test.png
The string I'm using is a set of eight character sequences separated by newlines. I've put it here along with the font:
https://www.dropbox.com/s/jgwoxz6epsj4b ... g.txt?dl=0
https://www.dropbox.com/s/gdwnop730jvfy ... _.TTF?dl=0
The -trim option fixes everything except for a one pixel wide strip on the right (it unfortunately doesn't look right when a border is added to the image):
I ran the pointsize test with my font and got this:
convert -background white -pointsize 48 -gravity center -font GC2004D_.TTF label:"21212121\n12121212" chess_test.png
The string I'm using is a set of eight character sequences separated by newlines. I've put it here along with the font:
https://www.dropbox.com/s/jgwoxz6epsj4b ... g.txt?dl=0
https://www.dropbox.com/s/gdwnop730jvfy ... _.TTF?dl=0
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
try
where XX is some small number between 0 and 100 that will allow near white colors to be removed. Start with 0 and if that does not work, try 1, 5 or 10 and see what is the smallest that works.
Code: Select all
convert -background white -pointsize 48 -gravity center -font GC2004D_.TTF label:"21212121\n12121212" -fuzz XX% -trim +repage chess_test.png
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
P.S. Please always provide your IM version and platform/OS, since syntax may vary.
The more current your version of IM, the better label: has been optimized.
The more current your version of IM, the better label: has been optimized.
Re: text to image problem
macOS 10.12.3
ImageMagick 6.9.7-7 Q16 x86_64 2017-02-11
Setting the fuzz factor all the way to 40% got rid of the blank space:
convert -size 300x300 -trim -fill black -fuzz 40% -font GC2004D_.TTF label:@boardstring.txt +repage out.png
I just realized however that this method results in a nonsquare final image (280x288). When I resize it (to 288x288) I find that the image size increases by over a third (from 14KB to 19KB), even after crushing it. I really need to keep these sizes down and this is a problem. Is there anything that can be done about this?
Is there something wrong with the font that is causing those blank spaces?
ImageMagick 6.9.7-7 Q16 x86_64 2017-02-11
Setting the fuzz factor all the way to 40% got rid of the blank space:
convert -size 300x300 -trim -fill black -fuzz 40% -font GC2004D_.TTF label:@boardstring.txt +repage out.png
I just realized however that this method results in a nonsquare final image (280x288). When I resize it (to 288x288) I find that the image size increases by over a third (from 14KB to 19KB), even after crushing it. I really need to keep these sizes down and this is a problem. Is there anything that can be done about this?
Is there something wrong with the font that is causing those blank spaces?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
Your font glyph may not be perfectly square. Try the font I used above and see if you still have trouble. My example was using Mac OSX SnowLeopard with IM 6.9.7.8. You have your -trim in the wrong place. it should be just after label: and before +repage. See if that helps at all. You also did not follow my command syntax, where I said to leave off the size and just use a pointsize. Try that. Why does it have to be exactly 300x300?
When you resize, you may be introducing more shades of gray. So add PNG8:output.png at the end.
When you resize, you may be introducing more shades of gray. So add PNG8:output.png at the end.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
If I check your font glyph, it is not square.
The following shows:
Metrics: text: 1; width: 38; height: 36
The following shows:
Metrics: text: 1; width: 38; height: 36
Code: Select all
convert -debug annotate -pointsize 36 -font /Library/fonts/GC2004D_.TTF label:1 tmp.png
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/RenderFreetype/1468/Annotate
Font /Library/fonts/GC2004D_.TTF; font-encoding none; text-encoding none; pointsize 36
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/GetTypeMetrics/888/Annotate
Metrics: text: 1; width: 38; height: 36; ascent: 28; descent: -9; max advance: 36; bounds: 0,-8.9375 36,27; origin: 36,0; pixels per em: 36,36; underline position: -1.71875; underline thickness: 0.3125
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/RenderFreetype/1468/Annotate
Font /Library/fonts/GC2004D_.TTF; font-encoding none; text-encoding none; pointsize 36
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/GetTypeMetrics/888/Annotate
Metrics: text: 1; width: 38; height: 36; ascent: 28; descent: -9; max advance: 36; bounds: 0,-8.9375 36,27; origin: 36,0; pixels per em: 36,36; underline position: -1.71875; underline thickness: 0.3125
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/RenderFreetype/1468/Annotate
Font /Library/fonts/GC2004D_.TTF; font-encoding none; text-encoding none; pointsize 36
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/GetTypeMetrics/888/Annotate
Metrics: text: 1; width: 38; height: 36; ascent: 28; descent: -9; max advance: 36; bounds: 0,-8.9375 36,27; origin: 36,0; pixels per em: 36,36; underline position: -1.71875; underline thickness: 0.3125
2017-02-19T17:34:00-08:00 0:00.010 0.000u 6.9.7 Annotate convert[88365]: annotate.c/RenderFreetype/1468/Annotate
Font /Library/fonts/GC2004D_.TTF; font-encoding none; text-encoding none; pointsize 36
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
If I specify PNG8:, your file size is reduced further.
identify out.png out2.png
out.png PNG 280x288 280x288+0+0 8-bit sRGB 256c 14.1KB 0.000u 0:00.000
out2.png PNG 288x288 288x288+0+0 8-bit sRGB 16c 8.7KB 0.000u 0:00.000
Code: Select all
convert -size 300x300 -fill black -font /Library/fonts/GC2004D_.TTF label:@boardstring.txt -fuzz 40% -trim +repage out.png
convert out.png -resize 288x288! PNG8:out2.png
identify out.png out2.png
out.png PNG 280x288 280x288+0+0 8-bit sRGB 256c 14.1KB 0.000u 0:00.000
out2.png PNG 288x288 288x288+0+0 8-bit sRGB 16c 8.7KB 0.000u 0:00.000
Re: text to image problem
Sorry about the code - I was experimenting with a bunch of different things in my scripts and on the command line.
The 8 bit PNG fixes the file size problem and the fact the the glyphs themselves aren't even square solves the problem with the aspect ratio, so everything is cleared up.
Thanks a lot for your help, this has been very educational for me.
The 8 bit PNG fixes the file size problem and the fact the the glyphs themselves aren't even square solves the problem with the aspect ratio, so everything is cleared up.
Thanks a lot for your help, this has been very educational for me.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: text to image problem
Just a note about Imagemagick syntax. It is not like the usual Unix command syntax and IM 7 will be less forgiving that IM 6. But typically for raster images, you read the input image right after convert, then any settings that are needed for the subsequent operator(s) and then the output image. Order in IM 7 is especially important. See
http://www.imagemagick.org/Usage/basics/#why
http://www.imagemagick.org/Usage/basics/#why