Use ImageMagick to place an image inside a larger canvas - imagemagick

Getting started with ImageMagic and trying to find a way to do this... If an image is less than 50 pixels tall or 50 pixels wide, I'd like to place it (un-scaled) in the horizontal/vertical center of a new 50x50 pixel canvas on top of a white background - and save that as the new image. Anyone know if this is possible with ImageMagick? Thanks!

I used -extent to do this:
convert input.jpg -gravity center -background white -extent 50x50 output.jpg

I wanted to do the same, except shrink the image to 70% inside. I used this:
convert input.png -resize 70%x70% -gravity center -background transparent -extent 72x72 output.png
Not exactly what was requested but hopefully it will help someone ;).

I have once used this code to place an image in the center of a new canvas with white background. hope this will help you
convert -background white -gravity center your_image.jpg -extent 50x50 new_image.jpg

See cutting and bordering for a huge number of examples. Here's one simple way you might do it:
convert input.png -bordercolor Black -border 5x5 output.png
Of course, you'll need to calculate the size of the border to add (if any) based on the dimensions of the input image. Are you using an ImageMagick API, or just the command line tools?

I tried this:
convert test.jpg -resize 100x100 -background black -gravity center -extent 100x100 output.png

You can use single composition to do this. So it would look something like this:
convert -size 50x50 xc:white null: ( my_image.png -coalesce ) -gravity Center -layers Composite -layers Optimize output.png

To modify the source image you need to use mogrify:
mogrify -gravity center -background white -extent 50x50 source.jpg

If an image is less than 50 pixels tall or 50 pixels wide
In my case, the images were much larger than the destination canvas, and weren't square. So I resize them proportionally to fit inside. Example:
convert in.png -resize 46x46 -background none -gravity center -extent 50x50 out.png
The 46x46 limit ensures a 2 pixel margin minimum. Note that the above does not distort the image, e.g. a rectangle does not become a square.
I used background none for a transparent background, but you can choose a solid color instead.

Related

imagemagick trim but keep canvas size and *position*

Friends,
I need to -trim some images but keep the original canvas size. Works like this:
convert in.png -fuzz 10% -trim -background white -set page "%[fx:w]x%[fx:h]" +repage out.png
But how can I position the trimmed image part at it's original position? -gravitiy center is not an option as the to-be-trimmed part usually not at the canvas center.
Any ideas?
You should be able to -trim an image, then use -flatten to lay it back onto its original canvas. Try this command...
convert logo: -background none -trim -flatten trimmed.png
#GeeMack's answer is certainly simpler and more succinct, but if you need more flexibility for dinking around, another way is to get the image height and width and the trimbox in one invocation and use them in the next - maybe with adaptation.
So, starting with this image:
# Get image width and height and the trim-box
read geom trim < <(magick start.png -format "%G %#" info:)
# Make a new white canvas same size as original and trim new image onto it
magick -size $geom xc:white \( start.png -crop $trim \) -flatten result.png
I put an artificial yellow border around it so you can see the extent of it on SO's white background.

How to create an image border?

I want to create something like a border around an image with ImageMagick. I want the border to be related to the original size of the image.
Ex:
A 5% border on a 1000x100px image should give me an image of 1050x105px
A 10% border on a 500x400px image should give me an image of 550x440px
So for a 5% white border I got this code after lots of trial and error. But it seems way over complicated:
convert infile.png \
null: \
\( -clone 0 -resize 105% -fill \#fff -colorize 100% \) \
-swap 0,2 -geometry +0+0 -gravity center -layers composite \
outfile.png
How could I simplify this? What am I missing?
NOTE: I do not want to specify static width of the border since I have multiple input images of multiple sizes.
With ImageMagick you can specify the size of a border as a percent of the width and height. Run a command like this on a 500x400 image to see how it works...
convert input.png -border 5x10% result.png
That should produce an output image with the dimensions 550x480. Keep in mind the percentage you specify is added to each edge, so a 5% border will make a 500 pixel wide image 550 pixels wide.
To add a total of 10% to both the width and height you would use a command like this...
convert input.png -border 5% result.png
You could use -extent like this:
convert -size 1000x100 xc:blue -gravity center -background red -extent 105%x105% result.png
Check
identify result.png
result.png PNG 1050x105 1050x105+0+0 8-bit sRGB 2c 350B 0.000u 0:00.000
Use any of your own images in place of -size 1000x100 xc:blue

Imagemagick crop transparent image to mask

So here is my original image (note the transparent border) src.png:
Here's the mask I want to use to crop. White means keep, black means crop mask.png: (Note that it isn't necessarily going to be a square. It could be a heart or a star or anything)
I also have transparent.png, which is a fully transparent image. All three images have the same dimensions.
So, running this command generates the following image:
convert transparent.png src.png mask.png -composite out.png
Which is masking perfectly, but now I want it cropped to the size of the white mask area. Using -trim is sort of close, but it gets rid of the transparent areas that are inside the mask.
How can I resize the masked image to the size of the white area in the mask?
I am not sure I understand what you want for the result. Why do you need the fully transparent image? Do either of these do what you want?
Full sized transparent image with masked area showing:
convert src.png mask.png -alpha off -compose copy_opacity -composite result1.png
Trimmed to just the part you want:
convert src.png mask.png -alpha off -compose copy_opacity -composite -trim +repage result2.png
Not sure I understand what you are trying to do, not least why you need a full size transparent canvas. Your command does not give the same results on my ImageMagick version 7.
This command may be what you are looking for:
convert src.png mask.png -compose darken -composite -trim out.png
It gives this - I have artificially added a red border so you can see the full extent on StackOverflow's white background:
Or maybe you want the trim box from your mask and to use that to crop your source:
convert mask.png -format %# info:
113x113+570+33
convert src.png -crop 113x113+570+33 result.png
If you want to crop a square from picture, you no needs using mask, just crop it:
magick Uq328.png -crop -crop 111x111+570+331 +repage cropped.png
according to https://legacy.imagemagick.org/Usage/crop/

Enlarge image canvas to compensate for blur

I have an image that I am processing with imagemagick to add a blur:
convert input.png -blur 0x16 output.png
However the blur is cropped as it goes outside the frame of the image. How can I compensate for this and enlarge the canvas to allow the blue to be fully shown?
Example image here
The image you have linked is a JPEG, not a PNG, like your command suggests, so I have no idea if it has transparency, or whether it is supposed to have lots of spare canvas around the image of the TV. As such, I am guessing what you want.
You can extend the canvas out from the centre by specifying -gravity center and using -extent so that you have space for the blur like this:
convert image.jpg -gravity center -extent 120%x120% -blur 0x16 output.jpg
However, that introduces more canvas than you need, so you may want to trim it afterwards, like this:
convert image.jpg -gravity center -extent 120%x120% -blur 0x16 -trim output.png
As an alternative, you could add some white border (I chose 500 px all round) like this:
convert image.jpg -bordercolor white -border 500x500 -blur 0x16 output.png
and remove however much you wanted to afterwards with -shave like this:
convert image.jpg -bordercolor white -border 500x500 -blur 0x16 -shave 400x400 output.png

change the background of an image in image magick

I am using this command for imagemagick to extend the size of an image and background to white
convert input.png -extent 495X320 -gravity center -background white output.jpg
The size is doing extent but the background is black every time. I tried many methods but the same black is coming every time.So can some one help me how to solve this. Any help and suggestions will be highly appreciable.
The order is important here, use -background before -extent:
convert input.png -background white -extent 495X320 -gravity center output.jpg
Leaving this for people that have tried the other answer but it still does not work.
In my case I needed
convert image.png -resize 80% -quality 80% -background white image-edited.jpg
Add the -flatten option at the end and it should work. So like:
convert image.jpg -resize 80% -quality 80% -background white -flatten image-edited.jpg

Resources