Black background (instead of transparent) after drop shadow

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Black background (instead of transparent) after drop shadow

Post by nandinga »

Hi!

I'm trying to drop a shadow to a composition of two images: one JPEG/PNG and one SVG vector. With the PNG image everything works seamlessly. Now, trying with the JPEG, I get a black background instead of the expected transparent background.

The command looks like this:

Code: Select all

convert image.jpeg -background white -flatten \( -background none vector.svg \) -compose CopyOpacity -gravity center -composite \( \+clone -background \#111 -shadow 80x5\+5\+5 \) -background none -compose DstOver -flatten result_from_jpeg.png
We start from this JPEG
Image
or this PNG
Image

Then we compose the vector, that because is white, you'll not be able to see raw, so here it is with a grid below:
Image
the real vector is this one: http://cl.ly/Xymi/vector.svg

An then we drop the shadow. Starting with the PNG we get it right (transparent background):
Image
but with JPEG, the background is black instead:
Image

All files can be downloaded here in a ZIP: http://cl.ly/Xz8V/Archive%202014-10-10% ... 5%20am.zip

Any ideas of what could be happening?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Black background (instead of transparent) after drop sha

Post by snibgo »

Running your command on your files with IM 6.8.9-5, with Inkscape v0.48, the result from the JPEG looks the same as from the PNG.

What version of IM are you using? What is the SVG renderer?
snibgo's IM pages: im.snibgo.com
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

Hi! Thx!! Not sure what you mean about the SVG renderer, but:

Code: Select all

Version: ImageMagick 6.8.8-9 Q16 x86_64 2014-03-28 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib freetype jng jpeg lcms ltdl png tiff xml zlib
I'm on OsX.
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

I've upgraded IM to this version (a bit newer than yours):

Code: Select all

Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-10-10 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib freetype jng jpeg lcms ltdl lzma png tiff xml zlib
It still fails the same.

One interesting test:

Code: Select all

convert result_from_png.png test.jpg
flattens out the image, obviously, and makes the background black! Thinking about hidden color behind transparency...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Black background (instead of transparent) after drop sha

Post by snibgo »

nandinga wrote:Not sure what you mean about the SVG renderer ...
What is the output from:

Code: Select all

convert -background None vector.svg s.png
Does it mention Inkscape?

What does s.png look like? Is it a white cutout shape against a transparent background, or entirely opaque white?
nandinga wrote:... flattens out the image, obviously, and makes the background black!
Those pixels in the png are transparent black. If you want a different colour, eg red:

Code: Select all

convert result_from_png.png -background red -flatten test.jpg
snibgo's IM pages: im.snibgo.com
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

Code: Select all

convert -background None vector.svg s.png
outputs nothing to console (nothing about Inkscape, in fact never seen anything related, and wasn't expecting any output). The generated s.png looks correct: the vector rendered white over transparent background.

Code: Select all

identify -verbose s.png | grep -i inkscape
outputs nothing either.

Code: Select all

convert result_from_png.png -background red -flatten test.jpg
yup... tried putting -background none about everywhere in the command but wasn't able to fix the problem doing so :s
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Black background (instead of transparent) after drop sha

Post by snibgo »

Sorry, I meant, what is the text output from:

Code: Select all

convert -verbose -background None vector.svg s.png
As you get the expected s.png, I doubt that is the problem.

Does your shell need "+" and "#" to be escaped?

Please insert "+write shad_x1.png" after "-composite", so the command is now:

Code: Select all

convert image.jpeg -background white -flatten \( -background none vector.svg \) -compose CopyOpacity -gravity center -composite +write shad_x1.png \( \+clone -background \#111 -shadow 80x5\+5\+5 \) -background none -compose DstOver -flatten result_from_jpeg.png
Is shad_x1.png like this:
Image
snibgo's IM pages: im.snibgo.com
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

Ah, yes, there is Inkscape... :p

Code: Select all

$ convert -verbose -background None vector.svg s.png
"inkscape" "vector.svg" --export-eps="/var/tmp/magick-33126aSUB52u1R4o0" --export-dpi="90,90" --export-background="rgb(0%,0%,0%)" --export-background-opacity="0" > "/var/tmp/magick-331261TvU31zBnx9Z" 2>&1
mvg:/var/tmp/magick-33126Q6WfzqePU2mI=>/var/tmp/magick-33126Q6WfzqePU2mI MVG 500x500 500x500+0+0 16-bit sRGB 1.25KB 0.040u 0:00.039
vector.svg SVG 500x500 500x500+0+0 16-bit sRGB 1.25KB 0.000u 0:00.000
vector.svg=>s.png SVG 500x500 500x500+0+0 8-bit sRGB 8.76KB 0.020u 0:00.019
I can't see your version of your shad_x1.png image but here is mine:
Image
This is the correct output at this stage. The problems come when drawing the shadow. In fact, the sublist that generates the shadow itself works, and if you look closely the final image, the shadow is there, composed in the right place. The only problem we have is a black background instead of transparent.
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

The above makes the vectorial part look like there's no problem there. Do you know a way to check and compare our libjpeg versions? and/or maybe libpng?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Black background (instead of transparent) after drop sha

Post by snibgo »

Your verbose output show it is trying Inkscape, but then using MVG. MVG sometimes give bad results, but not in this case.
nandinga wrote:I can't see your version of your shad_x1.png image but here is mine:
Yours looks correct, like mine. (I don't know why you can't see mine. I probably copied the wrong URL. Sorry. I'm having a bad day.)

I think the problem is the final "flatten". This shouldn't (I think) leave any pixels transparent. I don't know why it does for me.

Instead of:

Code: Select all

-background none -compose DstOver -flatten
try:

Code: Select all

+swap -background None -layers merge
This is the "approved" technique for adding shadows.
snibgo's IM pages: im.snibgo.com
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

Did that work for you? I get this:
Image

BUT ...looks like the right direction to go :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Black background (instead of transparent) after drop sha

Post by snibgo »

Yeah, the single command needs a "-compose Copy". Sorry. It is a very bad day. My command is now:

Code: Select all

%IM%convert ^
  image.jpeg ^
  -background white -flatten ^
  ( -background none vector.svg ) ^
  -compose CopyOpacity -gravity center -composite ^
  +write shad_x1.png ^
  ( +clone -background #111 -shadow 80x5+5+5 ) ^
  +swap -background None -compose Copy -layers merge ^
  shad_x4.png
(This is Windows BAT syntax. Adjust as required.)
snibgo's IM pages: im.snibgo.com
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

Nop :s ..now the shadow is lost.
Image
are you getting same results as me? or all your results where good?

PD: can't be such a bad day... you're helping me a lot!! :)
nandinga
Posts: 10
Joined: 2014-10-10T02:14:03-07:00
Authentication code: 6789

Re: Black background (instead of transparent) after drop sha

Post by nandinga »

btw, this version of the command doesn't work with the png either :s
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Black background (instead of transparent) after drop sha

Post by snibgo »

snibgo wrote:-compose Copy
Snibgo, you idiot. You need to restore the default compose, after the first "-compose" changed it to "CopyOpacity". The default isn't "Copy" but "Over". So that second compose should be not "-compose Copy" but "-compose Over".

I'm a double idiot for not seeing that the shadow wasn't there.

Code: Select all

%IM%convert ^
  image.jpeg ^
  -background white -flatten ^
  ( -background none vector.svg ) ^
  -compose CopyOpacity -gravity center -composite ^
  +write shad_x1.png ^
  ( +clone -background #111 -shadow 80x5+5+5 ) ^
  +swap -background None -compose Over -layers merge ^
  shad_x4.png
snibgo's IM pages: im.snibgo.com
Post Reply