Page 1 of 1

CYMK PDF, AI or EPS to PNG

Posted: 2016-06-27T03:12:36-07:00
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.

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-27T08:59:25-07:00
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

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-27T23:52:30-07:00
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();

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-28T00:30:58-07:00
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".

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-28T08:40:05-07:00
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

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-28T10:05:03-07:00
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.

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-29T05:36:17-07:00
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.

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-29T08:42:06-07:00
by magick
Try this command:
  • convert -colorspace sRGB test_cmyk.pdf test_cmyk.png

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-29T09:30:13-07:00
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

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-29T11:02:47-07:00
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.

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-06-29T11:16:52-07:00
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.

Re: CYMK PDF, AI or EPS to PNG

Posted: 2016-07-01T01:30:55-07:00
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.