ImageMagick (Magick.NET) 's Pdf to Jpg/Tiff operation gives me wrong color for CMYK to CMYK

2.1k Views Asked by At

Issue

When converting to Jpg/Tiff (CMYK), output images has different CMYK values for same areas from the input:

  • (100)C become (76)C, (7)M, (14)Y
  • (100)M become (87)M, (5)Y
  • (100)Y become (85)Y
  • (100)K become (72)C, (67)M, (67)Y, (88)K
  • Pure white stays (0)C, (0)M, (0)Y, (0)K

History

Sample code

using System;
using System.Collections.Generic;
using ImageMagick;

namespace stackOVERFLOW
{
    class Sample
    {
        public static void Start()
        {
            Rasterize("SOSample.pdf");
        }
        static void Rasterize(string input)
        {
            var settings = new MagickReadSettings
            {
                Density = new Density(300, 300),
                ColorSpace = ColorSpace.CMYK
            };
            var images = new MagickImageCollection();
            images.Read(input, settings);
            images[0].Format = MagickFormat.Jpg; //or .Tiff
            images[0].Write(input[0..^4] + ".jpg"); // or ".tiff"
        }
    }
}

Input

  • Input PDF (CMYK with pure C, M, Y, K and white areas)

Output

Code for channel separation (used for output sample)

List<String> colors = new List<String> { "C", "M", "Y", "K" };
int n = 0;
foreach (IMagickImage<ushort> channel in images[0].Separate(Channels.All))
{
    channel.Negate();
    channel.Write(input[0..^4] + "_" + colors[n] + ".jpg");
    n++;
}

Interpretation of the problem

When converting to PNG (RGB) colors looks right for RGB, the feeling is that it's been converted to RGB before CMYK.

images[0].Format = MagickFormat.Png;
images[0].Write(input[0..^4] + ".png");

Purpose

It's for offset plate setting purpose, it's important the black to stay pure black (and the CMYK values in general) for many reasons including color quality, 1 color printing (B&W), etc.

Finally

  • Is there any way to get the expected result with Magick.net?
  • Is there any way to get the expected result with Ghostscript itself?
  • If you know any way to get CMYK raster image from a PDF, help me, I'm open for any language, etc.

Aditional Information:

Using Magick.NET-Q16-x64 v7.21.0 NuGet package

2

There are 2 best solutions below

0
On BEST ANSWER

dlemstra (Magick.net) answered me in GitHub this output is because PDF decoder of ImageMagick used the option -dUseCIEColor, they will fix it in the next release. For more information check the GitHub Discussion.

4
On

Well, Ghostscript produces the correct output, so I'd have to guess ImageMagick is doing something to it. Or possibly using the wrong device. Obviously I don't know what IM does to get Ghostscript to turn a PDF file into 'something else'.

This:

gs -sDEVICE=jpegcmyk -o out.jpg cmyk.pdf

produces a JPEG file where each of the rectangles is a pure shade of C, M, Y or K. Checked using the eyedropper tool in Adobe Photoshop.

CMYK JJPEG output from Ghostscript