Page 1 of 1

Create stack of identical images with transparent overlap

Posted: 2019-09-17T04:24:08-07:00
by im101
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)
Image

Desired output - Something like this (1000x1000)
Image

My Fail (with only two images in stack) (1000x1000)
Image

ImageMagick-7.0.8-Q16 on Windows

Re: Create stack of identical images with transparent overlap

Posted: 2019-09-17T06:32:30-07:00
by snibgo
Why don't you show the command you used?

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
Then t.png can be composited over itself. You can do the whole job in a single command.

Re: Create stack of identical images with transparent overlap

Posted: 2019-09-17T06:53:48-07:00
by im101
Thanks for the quick reply.
snibgo wrote: 2019-09-17T06:32:30-07:00 Why don't you show the command you used?
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
snibgo wrote: 2019-09-17T06:32:30-07:00 You need to make the white background transparent. But the background isn't exactly white.
That's what I thought. That's why I tried "-fuzz 40% -transparent white" but it didn't work either
snibgo wrote: 2019-09-17T06:32:30-07:00 Then t.png can be composited over itself. You can do the whole job in a single command.
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?

Re: Create stack of identical images with transparent overlap

Posted: 2019-09-17T07:48:53-07:00
by GeeMack
im101 wrote: 2019-09-17T06:53:48-07:00It 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?
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
That starts by setting the fill and background colors to "none".

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

Posted: 2019-09-17T10:43:34-07:00
by im101
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.