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 9197999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:11:27+00:00 2026-06-17T22:11:27+00:00

I am reading a video frame by frame using: vc = cv2.VideoCapture(‘test.avi’) and later

  • 0

I am reading a video frame by frame using:

vc = cv2.VideoCapture('test.avi')

and later on I check whether the frame has been read using:

if vc.isOpened():
    rval,frame = vc.read()
else:
    rval = False

Now the problem is , if I try to convert this frame to a numpy array using the following code:

PILImage = Image.fromstring("L",cv.GetSize(frame),frame.tostring())
NumPyArray = np.array(PILImage)

I get an error saying:

CvArr argument 'arr' must be IplImage, CvMat or CvMatND. Use fromarray() to convert numpy arrays to CvMat or cvMatND

From Documentation of VideoCapture::read I found that it returns two things [retVal,Image] . How do I get the Image part only and how do I convert it to Numpy Array?

Further Info:
Basically the whole point in doing this is that ,I am trying to write a program which allows me to go through each frame of the video by pressing spacebar, and select a particular region from any frame and save it as a jpg. Here is the Code:

from ITMS import ITMS
import cv2
from cv2 import cv
import numpy as np
import matplotlib.pyplot as plt
import Image
import matplotlib.widgets as widgets

def onselect(eclick, erelease):
    if eclick.ydata>erelease.ydata:
        eclick.ydata,erelease.ydata=erelease.ydata,eclick.ydata
    if eclick.xdata>erelease.xdata:
        eclick.xdata,erelease.xdata=erelease.xdata,eclick.xdata
    ax.set_ylim(erelease.ydata,eclick.ydata)
    ax.set_xlim(eclick.xdata,erelease.xdata)
    fig.canvas.draw()

def subImager(arr):
    fig = plt.figure()
    ax = fig.add_subplot(111)

    plt_image=plt.imshow(arr,cmap="Greys_r")
    rs=widgets.RectangleSelector(
        ax, onselect, drawtype='box',
        rectprops = dict(facecolor='red', edgecolor = 'red', alpha=0.2, fill=True))
    plt.show()


cv2.namedWindow("preview")
vc = cv2.VideoCapture('test.avi')

if vc.isOpened():
    rval,frame = vc.read()
else:
    rval = False

while rval:
    key = cv2.waitKey(30)
        if key==32:
            cv2.imshow("preview", frame)
            NumPyArray=ITMS.CVtoNPArray(frame)
            subImager(NumPyArray)
            rval,frame = vc.read()
        elif key==27:
            break

cv2.destroyAllWindows()

ITMS Class:

from cv2 import cv
import cv2
import numpy as np
from PIL import Image

class ITMS:
    def __init__(self):
        pass

    def CVtoNPArray(CVImage):
        PILImage = Image.fromstring("L",cv.GetSize(CVImage),CVImage.tostring())
        NumPyArray = np.array(PILImage)
        return NumPyArray
        CVtoNPArray=staticmethod(CVtoNPArray)
  • 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-17T22:11:27+00:00Added an answer on June 17, 2026 at 10:11 pm

    You are over-complicating things, the image returned in your frame name is already a numpy array. If you want to convert it to PIL, simply do Image.fromarray(frame).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to play a video using opencv (the video is an avi file
I am reading a video file using AVFoundation framework and getting the image buffers
I'm reading Apple's documentation on using QTKit to capture streaming audio and video from
I am reading a video file using DataInputStream. I am then using a byte
I've been reading the android api for capture video and reading some code and
I'm having memory issues while reading video frames from an existing video chosen from
I am reading docs for VLC Command line programming. there I saw YUV video
I am trying to do some computer video by reading the webcam stream in
I have a problem that I am getting IllegalArgumentException while reading a video file
I am using OpenCV via Matlab to detect faces in a video and then

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.