将4个独立的单色TIFF结合成1个CMYK TIFF
-
23-12-2019 - |
题
我具有以这样的方式存储的TIFF图像,即我将每个平面(颜色)存储在单独的文件中。每个文件(c,m,y,k)是一个块状tiff,作为每像素文件的单色8位存储。
我想使用python成像库(pil)
将这4个文件组合成一个CMYK彩色的TIFF这是我到目前为止的代码,但输出产生的tiff是不正确的,tiff正在组合成一个主要只是黑色的文件。我已经用另一个实用程序合并了这些文件,结果是正确的,所以我知道输入文件没有问题。
这是我到目前为止的代码:
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"
.
注意:我已经在没有任何一个.corver函数的情况下做了相同的。结果是相同的结果。
解决方案
我发现解决方案是,所需的分离文件中的所有值都需要反转,即255 - 值。
将每个文件转换为numpy数组,然后从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))
.
我不知道为什么需要这一点,但如果有人可以解释它会很棒。
不隶属于 StackOverflow