【问题标题】:unsigned char* image to Python无符号字符*图像到 Python
【发布时间】:2010-08-27 15:57:43
【问题描述】:

我能够使用 SWIG 为相机库生成 python 绑定,并且能够使用库的内置函数捕获和保存图像。 我正在尝试从相机获取数据为 Python 图像库格式,该库提供了将相机数据返回为 unsigned char* 的函数。 有谁知道如何将 unsigned char* 图像数据转换为我可以在 Python 中使用的某种数据格式? 基本上是在尝试将 unsigned char* 图像数据转换为 Python 图像库格式。

谢谢。

【问题讨论】:

    标签: python c++ python-imaging-library unsigned-char pep3118


    【解决方案1】:

    我认为您应该使用fromstring 方法,如下所述:

    How to read a raw image using PIL?

    另外,还有一篇关于使用python和opencv从摄像头捕获数据的好文章值得一读:http://www.jperla.com/blog/post/capturing-frames-from-a-webcam-on-linux

    【讨论】:

    • 好吧。相机是 1394 相机,我认为我无法使用 OpenCV 获取图像。在 fromstring 示例中,python 正在从文件加载到二进制格式,这很简单。就我而言,我有一个来自 1394library 的自定义结构,其中包含图像,访问数据的唯一方法是通过 unsigned char*。我刚刚得到了一些关于使用 OpenCV 绕过指针机制的想法,我会尽量保持发布
    • frombuffer方法怎么样?
    • 这是我的代码 FC2=__import__('FC2Py') fch = FC2.FCHelper() pycam = FC2.FC2PyCamera() retval = pycam.setCamera(fch.connectCamera(7150499)) err = pycam. StartCapture() img = FC2.Image() err = pycam.cam.RetrieveBuffer(img) from PIL import Image pilimg = Image.frombuffer("L",(img.GetCols(),img.GetRows()),img. GetData(),'raw', "L", 0, 1)
    • 错误:回溯(最近一次调用最后一次):文件“”,第 1 行,在 文件“/usr/lib/python2.6/dist-packages/PIL/Image .py",第 1853 行,在 frombuffer core.map_buffer(data, size, decoder_name, None, 0, args) TypeError: expected string or buffer
    • 嘿 KarlPhillip,我终于明白了,为了让 python 理解数据类型 unsigned char* 图像,我需要使用自定义数据结构对其进行类型映射。一旦我得到它,我就能够获得二进制数据,然后我按照你的建议使用 Image.frombuffer 。谢谢。
    【解决方案2】:

    好的,伙计们,经过长时间的战斗(可能是因为我是 python 新手),我终于解决了。

    我写了一个python可以理解的数据结构,并将 unsigned char* 图像转换为该结构。为自定义数据结构编写接口后,我能够将图像转换为 Python 图像库图像格式。 我想在这里粘贴代码,但它不允许超过 500 个字符。 这是我的代码的链接

    http://www.optionsbender.com/technologybending/python/unsignedcharimagedatatopilimage

    我还附上了文件供你使用。

    【讨论】:

      【解决方案3】:

      我假设那些 unsigned chars 是实际的图像字节,所以您可以通过以下方式直接存储它们:

      with open('filename', mode='wb') as file:
          file.write(image_bytes)
      

      (只要您在当前工作目录中已经有一个名为filename 的文件。)

      【讨论】:

        猜你喜欢
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-26
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        • 2013-05-16
        相关资源
        最近更新 更多