3

I know about extend and gravity. I have an image and I want to increase its canvas size to 400x400 AND I want to take the existing image and place it at a specific X and Y in the canvas. I have the Specific values, but I need the ability to move the image there.

Here is an Example.

 width = 52 
 page.x = 158
 400 - width - page.x = 190

 height = 107
 page.y = 146
 400 - height - page.y = 147

In this example the image needs to be resize from 52x107 to 400x400 and the image needs to start at 158x146 in this need 400x400 canvas.

Case
  • 4,244
  • 5
  • 35
  • 53
  • It is always a good idea to attach an expected final image. 52x107 does not scale up to 400x400 so do you want the image cropped or distorted or is the 400x400 a different canvas not from the original image? – Bonzo Nov 10 '16 at 19:47

2 Answers2

2

If your not distorting the original 52x107 image, then it might be as simple as -repage +{left}+{top}.

# Given a 52x107 image.
convert -size 52x107 xc:orange 52x107.png

52x107.png

# Create a blank canvas to act as the padding.
convert -size 400x400 xc:lime \( 52x107.png -repage +158+146 \) -flatten out.png

out.png

In this example the image needs to be resize from 52x107 to 400x400 and the image needs to start at 158x146 in this need 400x400 canvas.

However it sounds like you do want to resize the small image to 400x400, and adjust offset w/ padding.

# Using rose: for example
convert -size 400x400! xc:lime \
        \( rose: -resize 400x400! -repage +158+146 \) \
        -flatten out2.png

out2.png

... or ... attempting something more dynamic

OFFSET=$(identify -format '+%[fx:(400-w)/2]+%[fx:(400-h)/2]' rose:)
convert rose: -repage $OFFEST \
        \( +clone -resize 400x400! -repage +0+0 \) \
        -swap 0,1 -flatten out3.png

out3.png

But, perhaps, I'm not understanding the OP question.

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Thank you very much for your help. I need to convert this into a slightly different style. This is what I have convert ' . $img . '.png /gradient/gradient.png -clut NOW I have that image and I want to apply what you have given to me to this image. How would I do so? – Case Nov 10 '16 at 22:13
2

The "-extent" option, with a specified offset, does exactly what you want:

magick -size 52x107 xc:orange -background lime \
       -extent 400x400-158-146 52x107-400x400.png

The -extent 400x400-158-146 option means "extend the image to 400x400, and place the upper left corner of the extended image at location (-158,-146) with respect to the original image."

Glenn Randers-Pehrson
  • 11,940
  • 3
  • 37
  • 61