4つの別々のモノクロのTIFFを1 CMYK TIFFに組み合わせる
-
23-12-2019 - |
質問
私は、私が別のファイルに記憶されている各平面(色)を持つように、TIFF画像を保存しています。各ファイル(C、M、Y、K)は、ピクセルファイルごとにモノクロ8ビットとして格納されている分圧TIFFです。
これらの4つのファイルをPythonイメージングライブラリ(PIL)を使用して1つの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"
.
注:関数のコニーバンスなしで同じことを行い、結果が同じになるようにしました。
解決
私は、区切りされたファイル内のすべての値が反転される必要があること、すなわち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