imagemagick trim but keep canvas size and *position* - imagemagick

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.

Related

imagemagick change canvas to square without using -extent (retaining the longest edge)

So there are many questions similar to this, but none that I can find that answers this exact scenario:
After batch trimming a folder of images, how to then make the canvas square, whilst retaining the longest edge? I don't want to use -extent and make them all a fixed width.
Examples of desired output:
800x1200 becomes 1200x1200
1000x600 becomes 1000x1000
1625x1600 becomes 1625x1625 etc....
So for example, if -squared was a function, it would be something like:
mogrify -path squared/ -trim -background white -gravity center quality 75 -squared *.jpg
How to achieve this?
If on ImageMagick 7, you can do the following with -extent to get the max of w and h.
Input:
magick barn.jpg -background black -gravity center -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" x.png
In ImageMagick 6, you can do something similar by using the viewport computations with -distort SRT, but you have to add the offset computations, since -gravity does not work with -distort SRT.
convert barn.jpg -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]+%[fx:(w-max(w,h))/2]+%[fx:(h-max(w,h))/2]" -virtual-pixel black -filter point -distort SRT 0 +repage y.png

ImageMagick engraving effect on glass

I want to add logo on product image with engraving effect.
The original logo is
After adding it on the product it should look like this.
how to do this with imagemagick.
Using this as the trophy:
Then something along these lines:
convert trophy.jpg -gravity center \
\( G.png -colorspace gray -channel a -evaluate multiply 0.2 -resize 120x120 \) -composite result.png
So, I am basically loading the trophy, then in some "aside processing" in parentheses, loading the Google logo, converting it to greyscale, reducing the opacity by multiplying it by 0.2, resizing it and compositing it on top of the trophy.
By the way, if you were using GraphicsMagick, which doesn't have the parentheses I used to make sure I only convert the logo to greyscale and not the trophy, you would do it in a different order. First load the logo and process it (greyscale, resize etc), then load the trophy, then swap the order so the trophy goes to the background, like this:
gm convert G.png -colorspace gray -resize ... trophy.jpg -swap -composite result.png

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/

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

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