I have been fighting for hours trying -layers, -composite, -transparent, converting to PNG and back, etc. but I can't get this to work. I should add, I really don't know much about image processing so it's not really a surprise!
I have an original image in JPG format with a (nearly) white background. I want to resize it to 800px (maintaining aspect ratio) and then create a stack of the same image, let's say four copies, and have the output as a JPG again on a 1000x1000 white canvas.
No matter what I try, I can't get the images to stack with the area around the outside remaining transparent. To make it more complicated, I also don't want any white areas inside the object to be transparent as that lets the images underneath show through (as you can also see in my failed attempt).
ImageMagick-7.0.8-Q16 on Windows
Original (2360x2812)
Desired output - Something like this (1000x1000)
My Fail (with only two images in stack) (1000x1000)
ImageMagick-7.0.8-Q16 on Windows
Create stack of identical images with transparent overlap
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Create stack of identical images with transparent overlap
Why don't you show the command you used?
You need to make the white background transparent. But the background isn't exactly white.
Then t.png can be composited over itself. You can do the whole job in a single command.
You need to make the white background transparent. But the background isn't exactly white.
Code: Select all
magick orig1skdd.jpg -fill White -fuzz 10% -draw "color 0,0 floodfill" -fuzz 0 -transparent White t.png
snibgo's IM pages: im.snibgo.com
Re: Create stack of identical images with transparent overlap
Thanks for the quick reply.
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" ^
-size 1000x1000 xc:white ^
( orig.jpg -strip -background white -resize 800x800 -fuzz 40% -transparent white ) -gravity northwest -composite ^
( orig.jpg -strip -background white -resize 800x800 -fuzz 40% -transparent white ) -gravity southeast -composite ^
output.jpg
Well, I tried so many things but none of it worked. It was all things like this:
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" ^
-size 1000x1000 xc:white ^
( orig.jpg -strip -background white -resize 800x800 -fuzz 40% -transparent white ) -gravity northwest -composite ^
( orig.jpg -strip -background white -resize 800x800 -fuzz 40% -transparent white ) -gravity southeast -composite ^
output.jpg
That's what I thought. That's why I tried "-fuzz 40% -transparent white" but it didn't work either
It seems to be the compositing that I'm having a problem with. As soon as I composite the images, the white border comes back again. A single command would be ideal. Do you have an example I can use as a startung point?
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Create stack of identical images with transparent overlap
Here's a command with ImageMagick v7 that does pretty much what you're trying to do.
Code: Select all
magick orig1skdd.jpg -background none -fill none -fuzz 5% -draw "color 0,0 floodfill" ^
-resize 800x800 -extent 1000x1000 -duplicate 3 -distort affine "0,0 %[fx:t*100],%[fx:t*60]" ^
-background white -flatten output.png
It sets the fuzz to 5% so the "floodfill" can change the white background to transparent as near as possible to the image.
Next it resizes the input to 800 pixels wide and extends the canvas to 1000x1000 with the input image in the upper left corner. After that it duplicates that image 3 times.
Then the "-distort affine" operation essentially slides those copies within the canvas, each one 100 pixels to the right and 60 pixels down from the previous. It calculates those distances using FX expressions.
To finish, set the background to white, flatten all four images onto the background, and write the output file.
Tweak the fuzz, dimensions, and distort distances to suit your specific need.
To use this sort of command in a BAT script you'll need to change the single percent signs "%" to doubles "%%".
Re: Create stack of identical images with transparent overlap
Thank you so much. That's exactly what I was looking for.
It still didn't answer why nothing I tried was working and then I read the last sentence. I can't believe I was so stupid! I've been running this from a batch file the whole time and didn't think about those % signs. Of course I need to double them up. I suspect a lot of what I'd been trying would have worked if I'd done that. Having said that, it wasn't half as neat as your solution.
Many thanks.
It still didn't answer why nothing I tried was working and then I read the last sentence. I can't believe I was so stupid! I've been running this from a batch file the whole time and didn't think about those % signs. Of course I need to double them up. I suspect a lot of what I'd been trying would have worked if I'd done that. Having said that, it wasn't half as neat as your solution.
Many thanks.