CYMK PDF, AI or EPS to PNG

Do you need consulting from ImageMagick experts and are willing to pay for their expertise? Or are you well versed in ImageMagick and offer paid consulting? If so, post here otherwise post elsewhere for free assistance.
Post Reply
bobby solo
Posts: 4
Joined: 2016-06-27T02:51:56-07:00
Authentication code: 1151

CYMK PDF, AI or EPS to PNG

Post by bobby solo »

When a user uploads a CMYK vector file either as a PDF, AI or EPS. I want to extract a small PNG without a white background or colour loss for use in a browser. However this could actually be a bug I have posted all the information to describe this problem here: http://stackoverflow.com/questions/3769 ... background We are willing to pay for a functional code snippet, as we are not sure if this is actually a bug or not. Just a note, linear PDF's in CYMK seem to work ok.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CYMK PDF, AI or EPS to PNG

Post by fmw42 »

PNG does not support CMYK, so you need to convert to sRGB. This works for me

Code: Select all

convert -background none -colorspace sRGB test_cmyk.pdf test_cmyk.png
bobby solo
Posts: 4
Joined: 2016-06-27T02:51:56-07:00
Authentication code: 1151

Re: CYMK PDF, AI or EPS to PNG

Post by bobby solo »

This is what we are doing, but it's unfortunately not working with the conversion. I Have been told this a possible bug? We can't force our users to covert CYMK to sRGB before they upload their files. Here is the snippet of code I'm using to convert to sRBG:

Code: Select all

$f = $_FILES['imageLoader']['name'];    
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImage("upload/$t/$f");

$width = $im->getImageWidth(); 

if (pathinfo($f, PATHINFO_EXTENSION) == 'psd') {
    $im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
}

if (pathinfo($f, PATHINFO_EXTENSION) == 'svg') {
    $im->setBackgroundColor(new ImagickPixel('transparent'));
    $svg = file_get_contents("upload/$t/$f");
    $im->readImageBlob($svg);
}

if (pathinfo($f, PATHINFO_EXTENSION) == 'ai') {
    $im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
}

if (pathinfo($f, PATHINFO_EXTENSION) == 'pdf') {
    $im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
}


$im->thumbnailImage(149, 240, true);
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(72,72); 
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,0); 
$im->setImageFormat('png32');
$im->writeImage($outFile);

// clean up
$im->clear();
$im->destroy();
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: CYMK PDF, AI or EPS to PNG

Post by snibgo »

Under IM v6.9.2-5, GS 9.15:

Code: Select all

convert -verbose test_cmyk.pdf x2.tiff
x2.tiff has no transparency, although Adobe Reader shows the background is transparent.

"-verbose" shows IM is using "-sDEVICE=pamcmyk32". I don;t know if that is significant.

Working from test_cmyk_web_enabled.pdf, the conversion does have a transparent background. I notice that IM uses "-sDEVICE=pngalpha".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CYMK PDF, AI or EPS to PNG

Post by fmw42 »

We can't force our users to covert CYMK to sRGB before they upload their files
I am not suggesting that, but you can convert it for them.

If you need cmyk with transparency, then you cannot output to png, but you can output to tif. So this works for me

Code: Select all

convert -background none -colorspace sRGB test_cmyk.pdf -profile /Users/fred/Images/profiles/USWebCoatedSWOP.icc test_cmyk.tif
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CYMK PDF, AI or EPS to PNG

Post by fmw42 »

"-verbose" shows IM is using "-sDEVICE=pamcmyk32". I don;t know if that is significant.

Working from test_cmyk_web_enabled.pdf, the conversion does have a transparent background. I notice that IM uses "-sDEVICE=pngalpha".
Yes, I think this is significant. You need an sDEVICE that permits transparency. I suspect that pamcmyk32 is not alpha compatible. But I am no expert on this. PNG works for RGB since it use sDEVICE=pngalpha, which permits transparency to be retained. One would need to look at the NetPBM pamcmyk32 docs to see if it is alpha compatible and if not, find an alpha compatible alternative for cmyka.
bobby solo
Posts: 4
Joined: 2016-06-27T02:51:56-07:00
Authentication code: 1151

Re: CYMK PDF, AI or EPS to PNG

Post by bobby solo »

If you need cmyk with transparency, then you cannot output to png, but you can output to tif. So this works for me
I think you've maybe misunderstood the question? I do not need CMYK with transparency, I need a PNG (thumbnail) with transparency from a user uploaded CYMK PDF, EPS or AI file. This thumbnail will be displayed in a web browser. Of course the PNG will be in RGB. Tiff's are unfortunately not widely supported in most browsers.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: CYMK PDF, AI or EPS to PNG

Post by magick »

Try this command:
  • convert -colorspace sRGB test_cmyk.pdf test_cmyk.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CYMK PDF, AI or EPS to PNG

Post by fmw42 »

I think you've maybe misunderstood the question? I do not need CMYK with transparency, I need a PNG (thumbnail) with transparency from a user uploaded CYMK PDF, EPS or AI file. This thumbnail will be displayed in a web browser. Of course the PNG will be in RGB. Tiff's are unfortunately not widely supported in most browsers.
That is exactly what I gave you in my first post above. Since PNG does not support cmyk, you must convert to sRGB. This command worked for me and gives transparency.

Code: Select all

convert -background none -colorspace sRGB test_cmyk.pdf result.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: CYMK PDF, AI or EPS to PNG

Post by snibgo »

bobby solo wrote:Here is the snippet of code I'm using to convert to sRBG:
Your code is different to Fred's command. Fred's command has "-colorspace sRGB" before reading the PDF. Your code converts to sRGB after reading the PDF.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: CYMK PDF, AI or EPS to PNG

Post by fmw42 »

With vector format input images, you must provide a few settings before reading the images. For cmyk vector images, you must tell it to convert to srgb before reading the input. I am not sure about the -background none. Sometimes that is needed. It does not hurt to add it if you do want a transparent background. You might also need to provide the desired density before reading the vector input. That will control the output PNG size. If you have line drawings or such, then using a larger density before reading the input and resizing afterwards to compensate (supersampling technique), will give better resolution. I generally do not use -resample, but prefer to use -density (before reading the input), -resize and then -density again after reading the input to set the output pixel dimensions and the desired print density/resolution.
bobby solo
Posts: 4
Joined: 2016-06-27T02:51:56-07:00
Authentication code: 1151

Re: CYMK PDF, AI or EPS to PNG

Post by bobby solo »

Thanks a lot to both of you, especially for the last comment explaining about vector images. I got the code working now, colorspace had to be set before reading the files. I've updated the original post in Stackoverflow.
Post Reply