Putting text in a rectangle or an overalyed 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?".
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Putting text in a rectangle or an overalyed image

Post by him21sri »

There are two things which I wanna do -
1. So I have an image, I want to draw a rectangle over it with some opacity and put some text in the center of that rectangle
2. Overlay a small image on the original image and then write some text in the center of that overlayed image.

Text will be a dynamic attribute so it can be long or short.

I want to use C api to do this. Please suggest.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Putting text in a rectangle or an overalyed image

Post by snibgo »

What version of IM? Which C API, MagickCore or MagickWand?

I suggest you prototype at the command level first. Find the command that does what you want. Then, for each operation and setting, find the C function or data structure.
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

I have ImageMagick 6.8.9-10 version.

Please help in prototyping at command level I am not sure how I am gonna place the text in the center of rectangle or an overlayed image, I can find the corresponding C function later.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Putting text in a rectangle or an overalyed image

Post by fmw42 »

You did not say what platform you are using! Please always provide your IM version and platform. Syntax is different for windows and unix. There are many ways to do this. This is only one. It uses the internal IM image logo:. Replace logo: with your image.suffix.

Unix

Code: Select all

convert logo: \( -background none -fill red -font arial -pointsize 48 label:"This Is Text" -bordercolor green1 -border 5 \) -gravity center -compose over -composite logo_text_rectangle.jpg
Windows

Code: Select all

convert logo: ( -background none -fill red -font arial -pointsize 48 label:"This Is Text" -bordercolor green1 -border 5 ) -gravity center -compose over -composite logo_text_rectangle.jpg
Image

see
http://www.imagemagick.org/Usage/text/
http://www.imagemagick.org/Usage/draw/#primitives
http://www.imagemagick.org/Usage/windows/

For novices, see
viewtopic.php?f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

Sorry for not mentioning the platform I am using, I am doing all this on linux. I want the rectangle to cover the whole width of image and text should be placed in center. I also want to overlay a bookmark type small image on top right of my image and display some text in the center of that overlayed image as present in the sample. Sample : http://cdn.atomex.net/sample.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Putting text in a rectangle or an overalyed image

Post by fmw42 »

This forum does not host image. But you can post to any free hosting service and put the URL here. This other post my be of some help. viewtopic.php?f=1&t=31762.

What is a bookmark type small image?
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

I was able to host this image and have shared the same in my old comment, here I am attaching again http://cdn.atomex.net/sample.png. In this sample image look at the top right corner there is a small black image on which discount is displayed. So I want to overlay that image on my original image and place some text on it
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Putting text in a rectangle or an overalyed image

Post by fmw42 »

Here is a simple example:

Input:
Image

Image

Code: Select all

wid=`convert logo2.gif -format "%w" info:`

convert logo2.gif A_image2.gif -gravity northeast -compose over -composite \
\( -size ${wid}x -background black -fill white -gravity center label:"This Is Some Text" \) \
-append result.png
Image
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

Thanks this worked well. I also want to know what is the best way to make the background of lower rectangle in the above image translucent or set some opacity to it.

As well as for some reason, for me the text in the black rectangle is not center aligned its touching the upper edge of the rectangle but in your case it is coming proper.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Putting text in a rectangle or an overalyed image

Post by Bonzo »

Try using a rgba colour instead of black: rgba\(0,0,0,0.5\)
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

Ok that worked, this is essentially the alpha value is 0.5 which is making it translucent, cant we set transparency without using rgba color?

Because in many instances I might have to use colors other than black, so I will prefer putting the color name as required and setting the transparency with some other option.
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

him21sri wrote: 2017-04-18T02:55:57-07:00 As well as for some reason, for me the text in the black rectangle is not center aligned its touching the upper edge of the rectangle but in your case it is coming proper.
What about this how can I fix for my ImageMagick version?
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

Anyway I was able to set the transparency externally by using

-alpha on -channel A -evaluate set 50% +channel

But now I am stuck on another problem say if the text is small say just two letters then the text becomes very large and covers the whole width of image, cant we simply specify the font size and text stays in center and background covers the width of image as present in the sample image I shared in previous comments.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Putting text in a rectangle or an overalyed image

Post by Bonzo »

What about this how can I fix for my ImageMagick version?
Could be a problem with you version

You might be able to move the text a bit more with e.g. -geometry 0+5 but I am not sure it will work with label

Have a look at -annotate for more control of the text
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Putting text in a rectangle or an overalyed image

Post by him21sri »

By my understanding annotate will just put text on an image of a defined size but I want the background with some transparency as well, if I set background it will set for the whole image which I don't want. I just want to have a small rectangle at the bottom of image with some color and transparency with some center aligned text.
Post Reply