Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8141915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:40:03+00:00 2026-06-06T12:40:03+00:00

I am using Cython to grab images from a USB camera and convert them

  • 0

I am using Cython to grab images from a USB camera and convert them into a PIL image that is returned to the caller.

The data for the image is in a character array pointed at by the “convert_buffer” member of the structure returned by the image grabbing function:

struct FlyCaptureImage:
   /// stuff
  char * convert_buffer
   /// more stuff

Right now, I am doing this to turn it into a PIL image:

cdef unsigned char *convert_buffer
cdef Py_ssize_t byte_length
cdef bytes py_string

// get the number of bytes into a Py_ssize_t type
byte_length = count

// slice the char array so it looks like a Python str type
py_string = convert_buffer[:byte_length]

// create the PIL image from the python string
pil_image = PILImage.fromstring('RGB', (width, height), py_string)

That procedure of converting the data into a python string takes 2ms for what sounds like it could be a zero-copy event. Is it possible to get PIL to create my image just from the char * image data pointer that the camera API provided?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:40:04+00:00Added an answer on June 6, 2026 at 12:40 pm

    As of PIL 1.1.4, the Image.frombuffer method supports zero-copy:

    Creates an image memory from pixel data in a string or buffer object,
    using the standard “raw” decoder. For some modes, the image memory
    will share memory with the original buffer (this means that changes to
    the original buffer object are reflected in the image). Not all modes
    can share memory; supported modes include “L”, “RGBX”, “RGBA”, and
    “CMYK”.

    The problem is that your camera data appears to be 24-bit RGB, where PIL wants 32-bit RGBA/RGBX. Can you control the pixel format coming from the camera API?

    If not, there still may be an advantage to using Image.frombuffer, since it will accept a buffer instead of requiring you to build a python string from the pixel data.

    Edit: looking at the source for frombuffer, it is a light wrapper on fromstring, and zero-copy requires a pixel format in the Image._MAPMODES list (i.e. RGBX). At a minimum, you would have to copy/convert the RGB data to an RGBX buffer to get a zero-copy compatible pixel format.

    I don’t have a better way to get the raw bytes into PIL, but here are some interesting references:

    • Cython automatic type conversions
    • Efficient indexing of objects supporting the Python buffer interface (for numpy/PIL)
    • Converting malloc'ed buffers from C to Python without copy using Cython?
    • PyMemoryView_FromMemory (python 3.3)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python files are compiled to bytecode (*.pyc). Using Cython you can compile them to
I am trying to start using cython and and attempting to compile my first
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I have bumped into this concept using Python distutils2/packaging . I did google it,
I have just managed to build my first C extension for Python, using Cython
I am trying to compile a simple cython extension from the example page here
I am attempting to call ffmpeg to create an image from a frame in
Sage is supposed to be able to create compiled code using Cython. I have
I have some trouble handling custom C++ exceptions when calling from Cython. My situation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.