change the background of an image in image magick - imagemagick

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

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.

ImageMagick script takes JPG and places on canvas but changes colour of JPG

I would like to take this logo and place it on a black background.
I am using the below script
convert -size 4000x4000 canvas:#000000 -background \#000000 background.jpg
composite rev.jpg -gravity center background.jpg output.jpg
open output.jpg
Which generates. As you can see the colours have changed and I cannot work out why?
Just set the colorspace.
composite 03wp2.jpg -colorspace srgb -gravity center background.jpg output.jpg
Possibly a colour profile or are you using CMYK for the rev.jpg?
Try this which will shorten your code:
convert rev.jpg -background black -gravity center -extent 4000x4000 output.jpg

imagemagick: center and resize multiple images an keep original filename

Good morning,
I'd like to center and resize multiple images in a folder with different aspect ratios and keep the filename. The following is nearly what I like to have (it works perfect for the particular picture), but there I have to name every specific pic.
convert -size 100x100 xc:black -gravity center originalpic.jpg -thumbnail 300x300 -composite newpic.jpg
I tried to work with * to keep the original file name and to process every file in the folder but without success. Does anybody know how to do that?
Thank you!
Use the mogrify command to work with multiple files.
mogrify -size 100x100 xc:black -gravity center -thumbnail 300x300 -composite *.jpg
Another way would be to iterate over the images in bash and use the same name as output to overwrite:
for f in *.jpg
do convert -size 100x100 xc:black -gravity center $f -thumbnail 300x300 -composite $f
done
I think I got it for me in general:
First size down to the height you want, e.g. to 364px:
mogrify -resize x364 *.jpg
And then, e.g. you want to get a dimension of 546x364px, this:
mogrify -extent 546x364 -gravity center *.jpg
But at image with original size of 512x768 background is getting filled with white color, so I tried
mogrify -extent 546x364 -gravity center -background black *.jpg
and
mogrify -extent 546x364 -gravity center -fill black *.jpg
but background is still white :-(

How to rotate a composite image with fixed center?

So, I have this background image:
And I have a 1px x 1px image that I want to overlay on that. If I run:
convert bg.png \( -size 80x240 -background none -rotate 30 -geometry +120+88 tile:red.png \) -composite result.png
I get this:
But I want to rotate it with fixed center. The expected result should be something like this:
Do you know how can I accomplish that?
Thank you.
Looking into the comment by #frostyterrier I think his problem is he needs +distort and is using -distort?
This page should help: http://www.imagemagick.org/Usage/distorts/#srt
Rotate an image and have a transparent background
convert input.jpg -background none -virtual-pixel background +distort ScaleRotateTranslate 30 output.png

Use ImageMagick to place an image inside a larger canvas

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.

Resources