offsetting the image but using its center point

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?".
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

offsetting the image but using its center point

Post by rs13 »

Hello,

I have a quick question:

I am using '-composite' to compose the image A on top of another image B. I know x,y coordinates of image B where I want to place the image A at. However, what I need is for the center point of that image A to appear at those x,y coordinates. How do you do something like this as currently I am using 'geometry +offsetX+offsetY' but that only positions top left corner of the image A to that offset.

Your help is greatly appreciated in advance.

Regards,

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

Re: offsetting the image but using its center point

Post by fmw42 »

IM will place only the upper left corner of the overlay image at a given coordinate, but you can use -gravity and -geometry. If you want the two centers to match, then you need to use -gravity center and then -geometry -X-Y, where X=W/2 and Y=H/2 in the overlay image.

IM does not to my knowledge have a center to center composite geometry directly without computing as above.

Someone can correct me though, if I am wrong.

If the background image is a constant color, then it would be easier to use -border or -extent to extend the image so that it is centered in some constant color larger background.

So another way to do this would be to either use -border or -extent to extend the smaller image to the same size as the larger image using transparency (none) to fill it out so that your small image is in the center of a larger transparent image and then overlay this image on your larger image.

convert largeimage \( smallimage -gravity center -background none -extent WxH \) -compose over -composite result

where WxH = size of larger image.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: offsetting the image but using its center point

Post by anthony »

to position center to center use -gravity center and -geometry +0+0

But I supose you have a specific point in the destination image in mind.

One method would be to use -layering to position the image relative to each other.
However layering does not understand 'gravity'.

for example -page -100-100 will place pixel 100,100 at the 0,0 point.
you can then use -repage +X+Y! to make relavtive movements of the 'page' or vitrtual offset of the image.
http://www.imagemagick.org/Usage/basics/#page
http://www.imagemagick.org/Usage/layers/#merge
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: offsetting the image but using its center point

Post by fmw42 »

to position center to center use -gravity center and -geometry +0+0
I defer to Anthony's expertise. I am mistaken.

But you can compute the location for the center similar to what I have by referencing relative to the upper left corner of each image.

convert largeimage smallimage -gravity northwest -geometry +X+Y -compose over -composite result

where

X=x-w/2 where x=position in larger image where center of small image is to appear and w is the width of the small image

Similarly for Y.
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: offsetting the image but using its center point

Post by rs13 »

Thank you all for your prompt responses.

Anthony, you are correct, I have a specific point in the destination image (image B) in mind and it is not the center point.

Also, I am doing the positioning of image A onto the destination image (image B) on the fly so I will not know A's dimensions which will be changing constantly.

All I need to do is to position center pixel of image A onto specific offset in the image B.

Any ideas?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: offsetting the image but using its center point

Post by Bonzo »

How are you using the code as you should be able to calculate the center of the images and use it in a variable.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: offsetting the image but using its center point

Post by fmw42 »

you can always compute the middle of the small image using

#define offsets where center of small image should be in large image
xoffset=XXX
yoffset=YYY
#compute offsets for IM to use adjusting for size of small image
offsetx=`convert smallimage -format "%[fx:$xoffset-w/2]" info:`
offsety=`convert smallimage -format "%[fx:$yoffset-h/2]" info:`
convert largeimage smallimage -gravity northwest -geometry +{offsetx}+{offsety} -compose over -composite resultimage

the above is for Mac/Linux. Windows users see http://www.imagemagick.org/Usage/windows/
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: offsetting the image but using its center point

Post by rs13 »

Thank you for your suggestion but I do not know 'w' and 'h' of the small image. It keeps changing depending what image the user uploads.

Robert
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: offsetting the image but using its center point

Post by Bonzo »

If you tell us how you are running the code we can tell you how to get the small image dimensions into a variable !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: offsetting the image but using its center point

Post by fmw42 »

rs13 wrote:Thank you for your suggestion but I do not know 'w' and 'h' of the small image. It keeps changing depending what image the user uploads.

Robert

The code I provided above computes w and h automatically and uses it to compute the correct location for IM to use in -geometry to get the image centered at XXX and YYY relative to the upper left corner of the larger image that you provide.

see string formats at http://www.imagemagick.org/script/escape.php and fx calculations at http://www.imagemagick.org/Usage/transform/#fx_escapes

But Bonzo has a good question to understand what platform and API if any you are using other than the command line. Someone can probably help further if we know where/how you are going to use this.

Fred
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: offsetting the image but using its center point

Post by rs13 »

Thank you guys. Here is more details:

I am running a site that allows users to upload their logo at which point it needs to render on the glamour image of the product (destination image) tilted in a center of a circle that is off center. The site logic is build in php but I am intending to use just a command line to achieve it. What I am doing is:

% convert destination.png \( logo.png -resize 80 -background none -shear 10x05 \) -geometry +70+140 -composite result.png

Since the destination.png is constant, the coordinates where I want to place the logo.png are well known. The problem is that the logo.png is user dependent so if I resize it to 80px width (to fit into my product circle) and place it at the geometry coordinates (with top left point of logo.png aligned) it will not work for all logos as some logos will be higher and some smaller making the logo appear not on the product circle. If I can however specify a center point of the logo.png to align at the geometry coordinates, then I would be guaranteed that the logo will always be centered.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: offsetting the image but using its center point

Post by Bonzo »

What about this:

Code: Select all

<?php
$logo = "finished.png";
$background = "yorkshire.JPG";

// Using Imagemagick or you could use getimagesize()
$offsetx = exec("convert $logo -format \"%[fx:$xoffset-w/2]\" info:");
$offsety = exec("convert $logo -format \"%[fx:$yoffset-h/2]\" info:");

// Comment out or remove next two lines when happy with the code
echo "Offset x = $offsetx <br>";
echo "Offset y = $offsety <br>";

exec("convert $background \( $logo -resize 80 -background none -shear 10x05 \) -geometry +{$offsetx}+{$offsety} -composite result.png");

// Comment out or remove next two lines when happy with the code
echo "<img src=\"result.png\">";
?>
EDIT: Added the escapes to the ( )
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: offsetting the image but using its center point

Post by rs13 »

Thank you. Let me try it out. So there is no way to be able to get those coordinates within a single convert statement?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: offsetting the image but using its center point

Post by Bonzo »

So there is no way to be able to get those coordinates within a single convert statement?
No I do not think so.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: offsetting the image but using its center point

Post by Bonzo »

I am not sure how a command line works but could you create a script to run on the server you just input the logo name when the script is called ?
Post Reply