我必须在VIPS(和Python)中对16位tiff文件的不同色调范围应用各种转换。我已经设法做到了,但我是vip的新手,我不相信我这样做是有效的。这些图像每个都有几百兆字节,删除每个多余的步骤可以为每张图像节省几秒钟。
我想知道是否有一种更有效的方法来实现我从下面的代码中获得的相同结果,例如使用查找表(我不能真正弄清楚它们在VIPS中是如何工作的)。代码分离红色通道中的阴影,并将它们通过转换。
im = Vips.Image.new_from_file("test.tiff")
# Separate the red channel
band = im[0]
# Find the tone limit for the bottom 5%
lim = band.percent(5)
# Create a mask using the tone limit
mask = (band <= lim)
# Convert the mask to 16 bits
mask = mask.cast(band.BandFmt, shift = True)
# Run the transformation on the image and keep only the shadow areas
new_shadows = (65535 * (shadows / lim * 0.1)) & mask
在为每个色调范围(高光,阴影,中音)运行或多或少相似的代码后,我将所有生成的图像加在一起以重建原始波段:
new_band = (new_shadows.add(new_highlights).add(new_midtones)).cast(band.BandFmt)
我做了一个演示程序,展示如何使用vip直方图函数做类似的事情:
import sys
import pyvips
im = pyvips.Image.new_from_file(sys.argv[1])
# find the image histogram
#
# we'll get a uint image, one pixel high and 256 or
# 65536 pixels across, it'll have three bands for an RGB image source
hist = im.hist_find()
# find the normalised cumulative histogram
#
# for a 16-bit source, we'll have 65535 as the right-most element in each band
norm = hist.hist_cum().hist_norm()
# search from the left for the first pixel > 5%: the position of this pixel
# will give us the pixel value that 5% of pixels fall below
#
# .profile() gives back a pair of [column-profile, row-profile], we want index 1
# one. .getpoint() reads out a pixel as a Python array, so for an RGB Image
# we'll have something like [19.0, 16.0, 15.0] in shadows
shadows = (norm > 5.0 / 100.0 * norm.width).profile()[1].getpoint(0, 0)
# Now make an identity LUT that matches our original image
lut = pyvips.Image.identity(bands=im.bands,
ushort=(im.format == "ushort"))
# do something to the shadows ... here we just brighten them a lot
lut = (lut < shadows).ifthenelse(lut * 100, lut)
# make sure our lut is back in the original format, then map the image through
# it
im = im.maplut(lut.cast(im.format))
im.write_to_file(sys.argv[2])
它对源图像进行一次查找直方图操作,然后进行一次映射直方图操作,所以它应该很快。
这只是调整阴影,你需要稍微扩展它来做中色和高光,但是你可以从单个初始直方图进行所有三个修改,所以它不应该更慢。
如果您有任何问题,请在libvips跟踪器上打开一个issue:
https://github.com/libvips/libvips/issues