2

I am getting some frame data from a web cam in the form of rgb values.

import numpy as np    
frame = get_video()
print np.shape(frame)

The output is (480, 640, 3). Now I want to construct image from these values. So, I want to use

im = Image.new("RGB", (480, 640),frame)

But, here the third argument takes a tuple. I get this error

SystemError: new style getargs format but argument is not a tuple

So, my question is what is the best way to convert this frame data to a tuple, so that I can construct my image.

2
  • What does get_video() give? Commented Jun 20, 2015 at 5:35
  • get_video() gets the RGB values of the video frame. So basically frame contains 480x640 RGB values. Commented Jun 20, 2015 at 5:37

2 Answers 2

1

I am assuming here that you are importing the class Image from PIL.

The documentation for Image.new(), accessed with console command Image.new? is:

In [3]: Image.new?
Type: function
Base Class:
String Form:
Namespace: Interactive
File: /usr/lib/python2.7/dist-packages/PIL/Image.py
Definition: Image.new(mode, size, color=0)
Docstring: Create a new image

The third parameter is an RGB color, such as (255,255,255), to fill the hole image. You can't initialize individual pixels with this function.

I am also assuming that frame is a 3D array. As your output suggests, it's 480 lines and 640 rows of RGB tuples.

I don't know if there is a simpler way to do this, but I would set the pixel values by the putpixel() function, something like:

im = Image.new( "RGB", (480, 640), (0, 0, 0) ) #initialize 480:640 black image
for i in range(480):
    for j in range(640):
        im.putpixel( (i, j), tuple(frame[i][j]) )

Always check the docstring via console, it saves a lot of time. I also recommend using ipython as your console.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I will accept your answer if you replace im.putpixel( (i, j), frame[i][j] ) with im.putpixel( (i, j), tuple(frame[i][j]) ). It works well, but I am actually trying to live stream this video and it works very slow. Is there a better way to do this?
Done. It could be a better way, I've never actually used PIL.
0

I found this implementation to be faster

from PIL import Image

im = Image.new("RGB", (480, 640), (0, 0, 0) ) #initialize 480:640 black image
while True:
 frame = get_video()
 im = Image.fromarray(frame)
 im.save('some-name', 'JPEG')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.