从 Python 字符串创建 16 位 TIFF 图像
我需要在 16 位深度和颜色模式下使用扫描仪,因此我修改了 python-imaging-sane (不支持 16 位深度的 RGB tiff)以从扫描仪(epson v500)接收 Python 字符串中的图像。
这是我用来将数据从扫描仪发送到 python 字符串的修改后的函数:
#define READSIZE 32768
static PyObject *
SaneDev_read(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
unsigned char c_buf[READSIZE];
SANE_Int len, maxlen;
maxlen = READSIZE;
if (!PyArg_ParseTuple(args, ""))
{
return NULL;
}
st = (int) sane_read(self->h, (SANE_Byte *) c_buf, maxlen, &len);
return Py_BuildValue("is#", st, c_buf, len);
}
我使用这个 python 脚本来接收和使用数据:
import sane
import pgmagick
import cStringIO
sane.init()
s = sane.open(sane.get_devices()[0][0])
s.mode = 'Color'
s.source = 'Transparency Unity'
s.tl_x = 15
s.tl_y = 30
s.br_x = 52
s.br_y = 55
s.x_resolution = 1600
s.y_resolution = 1600
s.depth = 16
fbuffer = cStringIO.StringIO()
s.start()
par = s.get_parameters()
print "par = ", par
st = 0
while st is 0:
st, buf = s.read()
fbuffer.write(buf)
s.cancel()
data = fbuffer.getvalue()
fbuffer.close()
px = par[2][0]
py = par[2][1]
bytesperlines = par[4]
depth = par[3]
size = "%sx%s" % (px, py)
blob = pgmagick.Blob(data)
im = pgmagick.Image()
im.density("1600x1600")
im.depth(depth)
im.size(size)
im.magick('RGB')
im.resolutionUnits(gm.ResolutionType.PixelsPerInchResolution)
im.read(blob)
im.write("img.tiff")
该脚本在 8 位深度下工作得很好,但是当深度设置为 16 位时,我获得了图像颜色错误;
这是两个示例:
问题出在哪里?
编辑:我使用pgmagick,一个graphicsmagick的python包装器; Graphicsmagick 是在量子深度设置为 16 位的情况下编译的。
I need to use scanner in 16 bit depth and color mode, so i modified python-imaging-sane (that doesn't support RGB tiff with 16 bit depth) to receive from a scanner (epson v500) an image in a Python string.
this is the modified function that i use to send data from the scanner to a python string:
#define READSIZE 32768
static PyObject *
SaneDev_read(SaneDevObject *self, PyObject *args)
{
SANE_Status st;
unsigned char c_buf[READSIZE];
SANE_Int len, maxlen;
maxlen = READSIZE;
if (!PyArg_ParseTuple(args, ""))
{
return NULL;
}
st = (int) sane_read(self->h, (SANE_Byte *) c_buf, maxlen, &len);
return Py_BuildValue("is#", st, c_buf, len);
}
i use this python script to receive and use data:
import sane
import pgmagick
import cStringIO
sane.init()
s = sane.open(sane.get_devices()[0][0])
s.mode = 'Color'
s.source = 'Transparency Unity'
s.tl_x = 15
s.tl_y = 30
s.br_x = 52
s.br_y = 55
s.x_resolution = 1600
s.y_resolution = 1600
s.depth = 16
fbuffer = cStringIO.StringIO()
s.start()
par = s.get_parameters()
print "par = ", par
st = 0
while st is 0:
st, buf = s.read()
fbuffer.write(buf)
s.cancel()
data = fbuffer.getvalue()
fbuffer.close()
px = par[2][0]
py = par[2][1]
bytesperlines = par[4]
depth = par[3]
size = "%sx%s" % (px, py)
blob = pgmagick.Blob(data)
im = pgmagick.Image()
im.density("1600x1600")
im.depth(depth)
im.size(size)
im.magick('RGB')
im.resolutionUnits(gm.ResolutionType.PixelsPerInchResolution)
im.read(blob)
im.write("img.tiff")
the script works very well with 8 bit depth, but with depth set to 16 bit i obtain an image with wrong colors;
these are two examples:
where is the problem?
EDIT: i use pgmagick, a python wrapper to graphicsmagick; graphicsmagick is compiled with quantum depth set to 16 bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的字节顺序可能错误。尝试交换组成十六位数据的两个字节,看看会得到什么。
You probably have the byte order wrong. Try swapping the two bytes that make up the sixteen bit data and see what you get.