Reputation: 10911
This question: Use ImageMagick to place an image inside a larger canvas is something I've already done, but I'd like to modify it such that the new area is of a different colour without having the transparent part of the original image becoming the same colour.
How would this be accomplished?
Input image:
xxxxxxxx
xx...xxx
xxxxxxxx
Output image:
xxxxxxxxiii
xx...xxxiii
xxxxxxxxiii
iiiiiiiiiii
x
) represent the original image..
) represent transparent pixels.i
) represent the background colour of the new canvas.My command that I've thought of is something like this:
dwidth = newWidth-origWidth
dheight = newHeight-origHeight
convert in.png -background yellow -splice (dwidth)x(height)+(origHeight)+(0) -splice (width)x(dheight)+(0)+(origHeight) out.png
Parentheses are for clarity only. Actual numbers would show up instead without parentheses.
That code actually crashed. Using -gravity stopped the crash for some reason.
Using ImageMagick 6.9.1-Q16
.
Upvotes: 0
Views: 611
Reputation: 208052
I think you are confusing yourself, Kurt and me by saying you want to place your new image "inside a larger canvas" when you actually don't want to affect the existing canvas (i.e its transparent areas must remain transparent) at all. What, I think, you want to do, from your diagram, is append some new canvas around the existing one - and if that is the case, you need -splice
to add canvas rather than -composite
to overlay ontop of the existing one.
So, if you start off with this hollow green rectangle with a transparent centre:
you actually want to splice (add) some extra canvas around it without affecting the original transparent area and canvas, so you need this:
convert a.png -background pink -gravity southeast -splice 100x200 b.png
Of course, I may be hopelessly wrong and as confused as Kurt :-)
In case you wanted some help with understanding -splice
, I'll give some examples:
To splice pink onto the top of your image, as below, use -gravity north
:
convert a.png -gravity north -background pink -splice x10 n.png
To splice pink onto the bottom of your image, as below, use -gravity south
:
convert a.png -gravity south -background pink -splice x10 s.png
To splice pink onto the left side of your image, as below, use -gravity west
and note that the extra width is before the x
this time:
convert a.png -gravity west -background pink -splice 10x w.png
To splice pink onto the right side of your image, as below, use -gravity east
and note that the extra width is before the x
this time:
convert a.png -gravity east -background pink -splice 10x e.png
To splice onto both bottom and left, use -gravity southwest
and put the extra width before the x
and the extra height after the x
:
convert a.png -gravity southwest -background pink -splice 10x50 sw.png
Upvotes: 1
Reputation: 90335
If you place an image with transparent parts on top of another image, the previously transparent parts of the top image will assume the color of the underlying image.
Unless you explicitly state what color(s) you want the transparent sections to become (if not the colors from the underlying image), there is no way around this!
Since you do not provide example images, I'll have to provide my own. I'll generate them with these commands:
convert -size 310x400 xc:red 310x400red-bg.png
convert -size 200x300 xc:gray +noise gaussian 200x300noise.png
Here are the results:
Now put them on top of each other:
convert 310x400red-bg.png 200x300noise.png -gravity center -composite result.png
Result:
Upvotes: 0