[HELP] Transparency problems

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?".
Post Reply
CAFxX

[HELP] Transparency problems

Post by CAFxX »

hi all, this is my first post here.
it has probably been asked before, but I wasn't able to find it. my problem is that I have to rasterize a vector image (say, in EPS format) with a transparent background and save it as a transparent PNG24.
what happens is that the rasterized image background becomes solid white, regardless of -type truecolormatte and similar.
can anyone explain why it happens? does workarounds exist?
thank you, CAFxX.
Last edited by CAFxX on 2007-09-03T23:24:05-07:00, edited 1 time in total.
CAFxX

Re: rasterizing vector images

Post by CAFxX »

this is the problematic snippet:

Code: Select all

<?php
		$ys = 20;
		$xe = 1000;
		$ye = 200;
		$wd = 600;
		$hd = 220;
		$file = 'vettoriali/header-back.eps';
		$temp = '/tmp/c4d-'.md5('c4d'.time().microtime().rand()).'.png';
		$cmd =  "convert -type truecolormatte -size ${xe}x$hd xc:transparent ".
				"-fill \"rgba(0,0,0,0.50)\" -draw \"rectangle 0,$ys $xe,$ye\" ".
				"-draw \"image src-over ".($xe-$wd).",0 $wd,$hd $file\" ".
				"$temp";
		error($cmd);
		exec($cmd);
		header("Content-Type: image/png");
		readfile($temp);
		unlink($temp);
?>
and this is the result:
Image
as you can see, the vector image is rasterized with a solid white background, even though the rest is correctly transparent.
moreover, even the rectangle fill isn't transparent at all, so I guess I'm missing something here...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [HELP] Transparency problems

Post by anthony »

set -background none

See IM Examples, Text to Image Handling, Postscript
http://www.imagemagick.org/Usage/text/#postscript
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
CAFxX

Re: [HELP] Transparency problems

Post by CAFxX »

Tried, but without luck.
What puzzles me is that if $file is the image in vector format, the solid backgorund appears. But if it is a png file with transparency (made with photoshop from the vector image, or even by convert itself in a separate call), there's no solid background.

The only way found so far is to do tho passes:

Code: Select all

convert -channel RGBA -background none $file -size ${wd}x$hd $temp-1.png;
convert -type truecolormatte -size ${xe}x$hd xc:transparent \
    -matte -fill '#00000080' -draw "rectangle 0,$ys $xe,$ye" \
    -draw "image src-over ".($xe-$wd).",0 $wd,$hd $temp-1.png" \
    $temp-2.png
still, it looks weird that I can't do it in just one call, without saving/reopening the intermediate image...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [HELP] Transparency problems

Post by anthony »

What is you difference between temp-1 and temp-2?
You should be able to do it in one command.
You may have to turn off the channel setting later using +channel or it could effect other image operators.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
CAFxX

Re: [HELP] Transparency problems

Post by CAFxX »

temp-1 is where the rasterized vector image is placed, temp-2 is where the final composite image is stored to be read and sent by php's readfile.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [HELP] Transparency problems

Post by anthony »

It seems to me that the EPS bay be drawing the white background. Can you give a link to the image being used.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
CAFxX

Re: [HELP] Transparency problems

Post by CAFxX »

the problem occurs with svg as well...

here you go: http://cafxx.strayorange.com/tmp/header-back.svg
CAFxX

Re: [HELP] Transparency problems

Post by CAFxX »

not quite related to this: how can I specify the "native" rasterizing resolution?
"convert -size w,h" does not work... the only way is to specify "convert -density [bignumber] -resize w,h" but this does not look that elegant...
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [HELP] Transparency problems

Post by anthony »

The -density setting sets the native rasterizing resolution, -size is not used for SVG.
The -resize is usually only used for postscript which often comes out badly from the ghostscript delegate. The same is for the -channel setting, whcih could interfer with many other operations. The -type truecolormatte is a setting used for input and output of file formats.

I figrued out your problem... I have been caught out by this myself!
-draw draws on ALL images in the current image sequence!

If you read the SVG first, then try to draw on a second image, you draw on both!
try this....

Code: Select all

convert -size 1000x220 xc:transparent \
        -draw "fill #0008 rectangle 0,20 1000 200" \
        -background none header-back.svg \
        -geometry 600x220+400+0 -composite   result.png
The -geometry is a special operator that resizes the LAST image only
ans sets the composition offset (gravity effected). -composite uses the default
composition method 'Over'.

You were probably using src-over to replace pixels to try and fix the problem of -draw drawing all over the original SVG image.

If you wanted to read the SVG image first. Put the -draw processing in parenthesis to limit it to just the second image...

Code: Select all

convert -background none header-back.svg \
         \( -size 1000x220 xc:transparent \
             -draw "fill #0008 rectangle 0,20 1000 200" \
         \) +swap  -geometry 600x220+400+0 -composite   result.png
the +swap swaps the two images before -geometry resizes the LAST image.

I suggest you read BASICS in IM Examples (see signature). and remember
IN general, operators work on ALL images in the current image sequence
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply