2

I was able to generate python bindings for a camera library using SWIG and I am able to capture and save image using the library's inbuilt functions. I am trying to obtain data from the camera into Python Image Library format, the library provides functions to return camera data as unsigned char* . Does anyone know how to convert unsigned char* image data into some data format that I can use in Python? Basically am trying to convert unsigned char* image data to Python Image Library format.

Thank you.

fish2000
  • 4,289
  • 2
  • 37
  • 76
Chenna V
  • 10,185
  • 11
  • 77
  • 104

3 Answers3

1

I believe you should use the fromstring method, as described here:

How to read a raw image using PIL?

Also, there's a good article on capturing data from the camera using python and opencv which is worth reading: http://www.jperla.com/blog/post/capturing-frames-from-a-webcam-on-linux

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • well. the camera is a 1394 camera and I dont think I would be able to obtain images using OpenCV. In the fromstring example, python is loading from a file into a binary format which is straightforward. In my case I have a custom structure from the 1394library that contains the image and the only way to access the data is through unsigned char*. I just got some ideas on using OpenCV for bypassing the pointer mechanism, i'll try and keep posted – Chenna V Aug 27 '10 at 18:10
  • How about the frombuffer method? – karlphillip Aug 27 '10 at 19:52
  • Here's my Code 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) – Chenna V Aug 27 '10 at 21:35
  • ERROR: Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1853, in frombuffer core.map_buffer(data, size, decoder_name, None, 0, args) TypeError: expected string or buffer – Chenna V Aug 27 '10 at 21:35
  • Hey KarlPhillip, I finally undestood that inorder for python to understand the datatype unsigned char* image, I need to typemap it using a custom data structure. Once I got that I was able to get binary data and then I used the Image.frombuffer as you suggested. Thank you. – Chenna V Aug 29 '10 at 21:27
1

Okay Guys, so finally after a long fight (maybe because am a newbie in python), I solved it.

I wrote a data structure that could be understood by python and converted the unsigned char* image to that structure. After writing the interface for the custom data structure, I was able to get the image into Python Image Library image format. I wanted to paste the code here but it wont allow more tha 500 chars. Here's a link to my code

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

I've also attached files so that you can use it.

Chenna V
  • 10,185
  • 11
  • 77
  • 104
0

I'd assume those unsigned chars are the actual image bytes, so you could store those directly via:

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

(As long as you already have a file named filename in the current working directory.)

Humphrey Bogart
  • 7,423
  • 14
  • 52
  • 59