How to generate CMYK images via the command line under Linux

color-managementcommand lineimage manipulation

I have an application I have developed which is generating RGB PNG images with text on them via imageMagic, like so:

convert -size 1000x1000 -density 300 xc:white -pointsize 24 \
  -fill "rgb(0,0,0)" -annotate +500+500 'Josh Test' Jtest.png

It's working great. However I need to be able to generate CMYK output as well, the same images, but in CMYK rather than RGB. InkScape can't do this. I thought I could just use a CMYK color:

convert -colorspace cmyk -density 300 xc:white -pointsize 24 \
  -fill "cmyk(0,0,0,0)" -annotate +500+500 'Josh Test' Jtest.pdf

However the resulting image is not 100% black, but 100% cyan, 100% magenta, 100% yellow and 0% black. Actual colors look even worse. This is because ImageMagic does all drawing in an RGB space and converts to CMYK:

Drawing requires the RGBA color model. Internally, images are stored as RGB(A) or CMY(A)K.

I cannot generate RGB images and convert to CMYK, the colors won't be right. They must be CMYK the whole way through. How can I generate CMYK images under linux?

Best Answer

Save a python script like this:

#!/usr/bin/python
from PIL import Image, ImageFont, ImageDraw
import sys

im = Image.new('CMYK', (1000,1000), (0, 0, 0, 255))

f = ImageFont.load_default()
d = ImageDraw.Draw(im)
d.text((500, 500), sys.argv[1], font = f, fill = (0, 0, 0, 0))
del d

im.save(sys.argv[2])

Dependencies are python and the python imaging library. Then you can create your images with this command:

python cmyktext.py "Josh test" Jtest.pdf

Don't forget that cmyk is subtractive, so (0,0,0,0) is actually white. It is of course also possible to use any font you like, as documented here.