Draw text North not appear at top

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
rotem
Posts: 46
Joined: 2016-04-15T13:34:32-07:00
Authentication code: 1151

Draw text North not appear at top

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Draw text North not appear at top

Post 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.
rotem
Posts: 46
Joined: 2016-04-15T13:34:32-07:00
Authentication code: 1151

Re: Draw text North not appear at top

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw text North not appear at top

Post 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.
snibgo's IM pages: im.snibgo.com
rotem
Posts: 46
Joined: 2016-04-15T13:34:32-07:00
Authentication code: 1151

Re: Draw text North not appear at top

Post 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.
rotem
Posts: 46
Joined: 2016-04-15T13:34:32-07:00
Authentication code: 1151

Re: Draw text North not appear at top

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw text North not appear at top

Post 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:
snibgo's IM pages: im.snibgo.com
rotem
Posts: 46
Joined: 2016-04-15T13:34:32-07:00
Authentication code: 1151

Re: Draw text North not appear at top

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Draw text North not appear at top

Post by snibgo »

%w and %h give you those, eg:

Code: Select all

convert -pointsize 150 label:y -format "WW=%w\nHH=%h\nTRIMMED=%@" info:
snibgo's IM pages: im.snibgo.com
rotem
Posts: 46
Joined: 2016-04-15T13:34:32-07:00
Authentication code: 1151

Re: Draw text North not appear at top

Post by rotem »

Cool, thanks
Post Reply