Page 1 of 1

Draw text North not appear at top

Posted: 2016-07-06T23:26:01-07:00
by rotem
Hey,

I'm trying to draw text in the far most top of the picture and there is a gap for some reason. When I'm drawing an image it's looks great.

Here is the code:

Code: Select all

convert -quality 100 thumb10.jpg  -font assets/impact.ttf -pointsize 100 -fill "white" -stroke "none" -gravity North -draw "text 0,0 'Hello'" result.jpg
Here is the result:

Image

What am I doing wrong?

Re: Draw text North not appear at top

Posted: 2016-07-07T00:06:14-07:00
by Bonzo
It could be an invisible line height/margin built into the text?

I would try creating a image with the text on a transparent background, trim it and them composite it onto your image and see if that works.

You can adjust the position within the draw operator but that may not be a satisfactory method.

Re: Draw text North not appear at top

Posted: 2016-07-07T00:11:05-07:00
by rotem
Yeah this is what I did and there is an invisible line. However, I do not want to use a new layer with trim and composite since it cost more time. Is there a way to remove this invisible line for the first row?

Re: Draw text North not appear at top

Posted: 2016-07-07T00:27:53-07:00
by snibgo
Yes, there is a margin above and below text, because lines of text shouldn't touch each other (or other objects) vertically.

You might do something clever with font metrics, see http://www.imagemagick.org/Usage/text/#font_info , but I would do as Bonzo suggests.

Re: Draw text North not appear at top

Posted: 2016-07-07T01:48:49-07:00
by rotem
Well, I need it to be super fast so I need to draw it directly.

This is a command and output for drawing the character H.

Code: Select all

convert -debug annotate "-font" "./assets/impact.ttf" -pointsize 150 -gravity Center label:H +write info: x.png
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/RenderFreetype/1421/Annotate
  Font ./assets/impact.ttf; font-encoding none; text-encoding none; pointsize 150
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/GetTypeMetrics/843/Annotate
  Metrics: text: H; width: 83; height: 183; ascent: 151; descent: -32; max advance: 193; bounds: 6.15625,0.359375  77.0469,119; origin: 83,0; pixels per em: 150,150; underline position: -4; underline thickness: 1.59375
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/RenderFreetype/1421/Annotate
  Font ./assets/impact.ttf; font-encoding none; text-encoding none; pointsize 150
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/GetTypeMetrics/843/Annotate
  Metrics: text: H; width: 83; height: 183; ascent: 151; descent: -32; max advance: 193; bounds: 6.15625,0.359375  77.0469,119; origin: 83,0; pixels per em: 150,150; underline position: -4; underline thickness: 1.59375
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/RenderFreetype/1421/Annotate
  Font ./assets/impact.ttf; font-encoding none; text-encoding none; pointsize 150
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/GetTypeMetrics/843/Annotate
  Metrics: text: H; width: 83; height: 183; ascent: 151; descent: -32; max advance: 193; bounds: 6.15625,0.359375  77.0469,119; origin: 83,0; pixels per em: 150,150; underline position: -4; underline thickness: 1.59375
2016-07-07T11:44:47+03:00 0:00.000 0.000u 6.9.4 Annotate convert[48251]: annotate.c/RenderFreetype/1421/Annotate
  Font ./assets/impact.ttf; font-encoding none; text-encoding none; pointsize 150
label:H LABEL 84x184 84x184+0+0 16-bit sRGB 0.000u 0:00.000
This is the image that was created:

Image

How can I calculate and find the gap from the top (16 pixels) and the bottom (49 pixels)?

*** EDIT ***
I know that I'm using here gravity center. This is because I'm using gravity center when drawing the text.

Re: Draw text North not appear at top

Posted: 2016-07-07T03:01:07-07:00
by rotem
I found a way.

I'm using this command (with or without gravity center):

Code: Select all

convert "-font" "./assets/impact.ttf" -pointsize 150 label:y -trim info:
The output is:

Code: Select all

label:y LABEL 69x111 70x184+0+54 16-bit sRGB 0.000u 0:00.000
So in this case 54 is the gap from above, and 184 - 111 - 54 = 19 is the gap from bottom.

Re: Draw text North not appear at top

Posted: 2016-07-07T03:08:50-07:00
by snibgo
Yes, trimming is the easiest way. If you only want the numbers, not the actual image, this will be faster:

Code: Select all

convert "-font" "./assets/impact.ttf" -pointsize 150 label:y -format %@ info:

Re: Draw text North not appear at top

Posted: 2016-07-07T03:22:20-07:00
by rotem
Yeah but then I won't get the not trimmed values and wouldn't be able to calculate the right and bottom gaps

Image

Re: Draw text North not appear at top

Posted: 2016-07-07T04:06:09-07:00
by snibgo
%w and %h give you those, eg:

Code: Select all

convert -pointsize 150 label:y -format "WW=%w\nHH=%h\nTRIMMED=%@" info:

Re: Draw text North not appear at top

Posted: 2016-07-07T04:29:16-07:00
by rotem
Cool, thanks