【问题标题】:Create a 16 Bit TIFF image from a Python string从 Python 字符串创建 16 位 TIFF 图像
【发布时间】:2011-08-07 17:06:12
【问题描述】:

我需要在 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 位时,我得到的图像颜色错误;

这是两个例子:

with 8 bit depth

with 16 bit depth

问题出在哪里?

编辑:我使用 pgmagick,一个 python 包装器到 graphicsmagick; graphicsmagick 编译时量子深度设置为 16 位。

【问题讨论】:

  • 我已添加我的评论作为答案。请点击它旁边的复选标记以接受它。
  • @agf :谢谢,它有效!我将数据直接保存在python数组中(使用fromstring()),然后使用byteswap()交换字节,然后使用tostring()从数组中提取一个字符串。

标签: python image tiff endianness sane


【解决方案1】:

您可能有错误的字节顺序。尝试交换构成 16 位数据的两个字节,看看会得到什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多