Transparency Issues (PNG on JPG)

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
immortal26

Transparency Issues (PNG on JPG)

Post by immortal26 »

Just going to use the smallest script I got to show you what's happening.
Here is the background jpg:
http://www.kgcore.com/highcotton/swan/back/Swan.jpg

Here is the php file that puts a name together using append using transparent png files located in an alphabet folder.

Code: Select all

<?php

$text2 = "plus";
$bg = 'back/Swan.jpg';
$cache = 'cache/';
$ext = '.png';
$alphabet = 'alphabet/';
$final = 'final/';

$text = "amanda";
$text3 = "chris";

$text = strtolower($text);
$text3 = strtolower($text3);


$textarray = str_split($text);
$createolay1 = 'convert ';
foreach ($textarray as $t) {
	$createolay1 .= $alphabet.$t.$ext.' ';
}

$createolay1 .= $alphabet.'space'.$ext.' '.$alphabet.$text2.$ext.' '.$alphabet.'space'.$ext.' ';

$textarray = str_split($text3);

foreach ($textarray as $t) {
	$createolay1 .= $alphabet.$t.$ext.' ';
}
$createolay1 .= '-background none +append name'.$ext;
exec($createolay1, $result);
$olay1='name.png';

list($width, $height) = getimagesize($olay1);


while($width > 1610) {
	$width--;
}

$resize = "convert -resize ".$width." name.png name.png";
exec($resize, $result);

$finalout = 'convert  ".$bg." -gravity north  name.png -geometry +0+360 -composite '.$final.$i.'final.pdf';

exec($finalout, $result);
exec('rm name.png');
as you can see... at:
http://www.kgcore.com/highcotton/swan/final/final.pdf

That the outside transparency stays but some of the inside of the letters has a black background ... so it's not preserving all of the transparency, just some of it.
I'm pretty sure this worked flawlessly in an older version of IM, I'm running on the latest version.

EDIT:
I commented out the rm at the end of the script so you can see the name.png
http://www.kgcore.com/highcotton/swan/name.png
So as you can see there, all transparency is there... even through it in photoshop to make sure, worked fine.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparency Issues (PNG on JPG)

Post by fmw42 »

A couple of minor things that probably do not make a difference. But the preferred syntax for IM 6 is

convert input options output

with the exception of when one has to make a setting before reading an image such as defining the density or colorspace for a pdf input image.

so

some of your IM command should be rearrange.

You could try increasing the density before creating the PDF.

As for the transparency, you may have to change the device for your delegates.xml file for pdf to preserve transparency. Unfortunately, I can never remember what it is (pngalpha?). But I am not an expert on this matter. You might search the archives. Or perhaps one of the IM folks can clarify further.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Transparency Issues (PNG on JPG)

Post by anthony »

Not certain where the black is commign from. At first I thought you used a bad compose method, but you don't specify any so 'over' should be in use.


Can you have it output a PNG instead of a PDF?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
immortal26

Re: Transparency Issues (PNG on JPG)

Post by immortal26 »

These images will be made for print. So I need to stick with the original cmyk from the jpg. Converting to png i will lose colors (rgb).
As a note, mentioning outputting it as a png does preserve "all" of the transparency.
Another note besides pdf... jpg does the exact same thing as pdf (black still there).
immortal26

Re: Transparency Issues (PNG on JPG)

Post by immortal26 »

tried composite method as well with compose over ... same results ...
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Transparency Issues (PNG on JPG)

Post by Drarakel »

I'm also not sure where the problem originates.. It almost looks as if the fully opaque areas within name.png were treated as fully transparent again (with background color black). Or maybe the alpha channel values are somehow gettin into the black channel of the final CMYK image.

Anyway, I think you can avoid potential problems if your input files are in the same colorspace. So, with your example, create the 'name' files as CMYK (e.g. as TIFF) right at the start - or convert them to CMYK afterwards (before composite).
Something like that works:

Code: Select all

convert swan.jpg -gravity north ( name.png -colorspace CMYK ) -geometry +0+360 -composite -compress zip -depth 8 final.tif
(I chose TIFF as output format as it preserves the full quality. You can use PDF of course.)
immortal26

Re: Transparency Issues (PNG on JPG)

Post by immortal26 »

Drarakel wrote:I'm also not sure where the problem originates.. It almost looks as if the fully opaque areas within name.png were treated as fully transparent again (with background color black). Or maybe the alpha channel values are somehow gettin into the black channel of the final CMYK image.

Anyway, I think you can avoid potential problems if your input files are in the same colorspace. So, with your example, create the 'name' files as CMYK (e.g. as TIFF) right at the start - or convert them to CMYK afterwards (before composite).
Something like that works:

Code: Select all

convert swan.jpg -gravity north ( name.png -colorspace CMYK ) -geometry +0+360 -composite -compress zip -depth 8 final.tif
(I chose TIFF as output format as it preserves the full quality. You can use PDF of course.)
Strangely enough, that worked!
I didn't know png's could be cmyk... guess they can hmmm. Strange, well as long as it works lol.
Thanks Drarakel!
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Transparency Issues (PNG on JPG)

Post by Drarakel »

PNG can't store CMYK. But it doesn't matter as long as you're not saving the result to PNG. :wink:
immortal26

Re: Transparency Issues (PNG on JPG)

Post by immortal26 »

Ok now this one just blows my mind... why? Because it's using the same exact code but just different images.
Original background CMYK image http://www.kgcore.com/highcotton/lakeside/back/Lake.jpg

Final output http://www.kgcore.com/highcotton/lakesi ... /final.pdf

Code: Select all

<?php

$text = $name1;
$text2 = "plus";
$text3 = $name2;
$bg = 'back/Lake.jpg';
$cache = 'cache/';
$ext = '.png';
$alphabet = 'alphabet/';
$final = 'final/';

$text = "amanda";
$text3 = "chris";

$text = strtolower($text);
$text3 = strtolower($text3);


$textarray = str_split($text);
$createolay1 = 'convert ';
foreach ($textarray as $t) {
	$createolay1 .= $alphabet.$t.$ext.' ';
}

$createolay1 .= $alphabet.'space'.$ext.' '.$alphabet.$text2.$ext.' '.$alphabet.'space'.$ext.' ';

$textarray = str_split($text3);

foreach ($textarray as $t) {
	$createolay1 .= $alphabet.$t.$ext.' ';
}
$createolay1 .= '-background none +append name'.$ext;
exec($createolay1, $result);
$olay1='name.png';

list($width, $height) = getimagesize($olay1);


while($width > 1462) {
	$width--;
}

$resize = "convert -resize ".$width." name.png name.png";
exec($resize, $result);

$finalout = 'convert '.$bg.' -gravity north  name.png -colorspace CMYK  -geometry +0+642 -composite  '.$final.$i.'final.jpg';

exec($finalout, $result);
//exec('rm name.png', $result);
?>
If I leave out the -colorspace CMYK I get the same results from the last one... black inside the letters (stars this time).
Example of using without -colorspace CMYK: http://www.kgcore.com/highcotton/lakesi ... ithout.pdf

You can take a look at the stars before being put on the background:
http://www.kgcore.com/highcotton/lakeside/name.png (you'll have to put it on a background, hard to see)
WTF? lol
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Transparency Issues (PNG on JPG)

Post by Drarakel »

OK.. Seems you have to avoid some more pitfalls. :)

With the 'swan'-example, you had a 32bit name.png. This time, it's grayscale+alpha (2x8bit). Apparently, the basic '-colorspace CMYK' conversion doesn't work for this one. (The gray channel is correctly mapped to the 'K' channel. But the alpha channel is not used - it gets fully opaque.)
To be on the safe side, use something like this:

Code: Select all

convert lake.jpg -respect-parentheses -gravity north ( name.png -type TruecolorMatte -colorspace CMYK ) -geometry +0+642 -composite -compress zip -depth 8 final.tif
Once name.png is 'blown up' to TruecolorMatte, the CMYK conversion is successful. Note that you definitely need the parentheses this time, and also '-respect-parentheses' - or otherwise lake.jpg gets converted to RGB! It's possible that you have to escape the parentheses with '\' or '^' - depends on your system.
Post Reply