Combining 4 separate monochrome tiffs into 1 CMYK tiff
-
23-12-2019 - |
Question
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.
Solution
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.