0

I have tiff images stored in such a way that i have each plane(color) stored in a separate file. Each file(C,M,Y,K) is a chunky tiff stored as a monochrome 8 bits per pixel file.

I want to combine these 4 files into one CMYK colored tiff using the python Imaging library(PIL)

This is the code I have so far but the output tiff produced is not correct, the tiff is being combined into a file that is mostly just black. I have merged these files with another utility and the result is correct, so I know there isnt a problem with the input files.

This is the code I have so far:

if len(sys.argv) <= 1:
    print "ERROR: Usage !"
    exit(1)

try:
    cFile = str(sys.argv[1])+"Cyan.tif"
    mFile = str(sys.argv[1])+"Magenta.tif"
    yFile = str(sys.argv[1])+"Yellow.tif"
    kFile = str(sys.argv[1])+"Black.tif"

    print "Opening files:"
    print cFile
    print mFile
    print yFile
    print kFile

    c_img = Image.open(cFile)
    c_img = c_img.convert("L")

    m_img = Image.open(mFile)
    m_img = m_img.convert("L")

    y_img = Image.open(yFile)
    y_img = y_img.convert("L")

    k_img = Image.open(kFile)
    k_img = k_img.convert("L")

except Exception, e:
    print "ERROR: Unable to open file..."
    print str(e)
    exit(1)
try:
    mergedRaster = Image.merge('CMYK', (c_img, m_img, y_img, k_img))
    mergedRaster = mergedRaster.convert("CMYK")

except Exception, e:
    print "ERROR: Merging plates"
    print str(e)
    exit(0)
#exit(0)
try:
    mergedRaster.save("output.tif", format="TIFF")

except Exception, e:
    print "ERROR: Writing tiff"

NOTE: I have done the same without any of the .convert functions and found the result to be the same.

user434885
  • 1,988
  • 6
  • 29
  • 51

1 Answers1

0

I found the solution to be that all the values in the separated files needed to be inverted, i.e. 255 - value.

Converting each file to a numpy array and then subtracting it from 255.\

try:
    cArray = numpy.array(c_img)
    mArray = numpy.array(m_img)
    yArray = numpy.array(y_img)
    kArray = numpy.array(k_img)
except Exception, e:
    print "ERROR: Converting to numpy array..."
    print str(e)
    exit(1)

try:
    toSub = 255
    cInvArray = toSub - cArray
    mInvArray = toSub - mArray
    yInvArray = toSub - yArray
    kInvArray = toSub - kArray

except Exception, e:
    print "ERROR: inverting !"
    print str(e)

try:
    cPlate = Image.fromarray(cInvArray)
    mPlate = Image.fromarray(mInvArray)
    yPlate = Image.fromarray(yInvArray)
    kPlate = Image.fromarray(kInvArray)

except Exception, e:
    print "ERROR: Creating image from numpy arrays"
    print str(e)
    exit(1)

try:
    mergedRaster = Image.merge('CMYK', (cPlate, mPlate, yPlate, kPlate))

I don't know why this was required, but if someone can explain it that would be great.

user434885
  • 1,988
  • 6
  • 29
  • 51
  • Sorry about my answer (now deleted). Somehow I missed the bottom part of your question (it was late, very late, my time). Anyway you don't need to use numpy to invert the images. `Image` objects have a `point()` method that can easily do this. My guess as to why this has to be done is that the CMYK colorspace is subtractive, not additive like RGB because it has to to with ink colors not light. – martineau Nov 19 '13 at 05:23
  • 1
    CMYK is a [subtractive color space](http://en.wikipedia.org/wiki/Subtractive_color) used for printing. "Subtractive" means that larger values (of ink) translate into darker colors (subtracting light). If your input images are mostly white when viewed as "additive" (the default), that would be translated to mostly black when viewed as CMYK -- unless you apply the numerical inversion. – Brent Bradburn Sep 26 '14 at 23:20