# Extracting Window Movie Frame without Creating Window

**URL:** <https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580>\
**Category:** Coding\
**Created:** [May 26, 2018, 10:19am UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580 "2018-05-26T10:19:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RylanSchaeffer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rylanschaeffer/32/2543_2.png) [@RylanSchaeffer](https://discourse.psychopy.org/u/RylanSchaeffer)\
**Post date:** [May 26, 2018, 10:19am UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580/1 "2018-05-26T10:19:34Z")

</div>

I want to use PsychoPy to generate stimuli (DotStim, specifically) and then subsequently extract the sequence of PIL images. I am currently instantiating a window, calling dot\_stim.draw() and then calling window.getMovieFrame(), but this still requires having a window that pop ups every time the window is instantiated. Is there a way I can extract the sequence of PIL images of my stimuli without instantiating a window or without having the window be visually displayed?

Thank you!

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [May 26, 2018, 11:26pm UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580/2 "2018-05-26T23:26:15Z")

</div>

You should be able to do it without the window being displayed using something like `xvfb-run`.

For example, if this is `grating_cap.py`:

```python
import psychopy.visual

win = psychopy.visual.Window((400, 400),fullscr=False)

grating = psychopy.visual.GratingStim(win=win)

grating.draw()

win.getMovieFrame(buffer="back")

cap_path = # put filename to save here

win.saveMovieFrames(cap_path)

win.close()

```

then you should be able to run it without seeing a window with:

```bash
xvfb-run grating_cap.py

```

N.B. it doesn’t seem to work for me (captured image is all black) when capturing the window after flipping, but works before flipping with `buffer="back"`.

---

<div class="post-metadata">

**Author:** ![RylanSchaeffer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rylanschaeffer/32/2543_2.png) [@RylanSchaeffer](https://discourse.psychopy.org/u/RylanSchaeffer)\
**Post date:** [June 1, 2018, 4:03pm UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580/3 "2018-06-01T16:03:44Z")

</div>

Is there another approach that doesn’t require writing the frames to disk? I’d like to immediately use the frames within the same python script.

---

<div class="post-metadata">

**Author:** ![RylanSchaeffer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rylanschaeffer/32/2543_2.png) [@RylanSchaeffer](https://discourse.psychopy.org/u/RylanSchaeffer)\
**Post date:** [June 1, 2018, 4:49pm UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580/4 "2018-06-01T16:49:59Z")

</div>

I’ve now tried and failed to get either pyvirtualdisplay or xvfbwrapper to work. I’ll keep googling and trying.

---

<div class="post-metadata">

**Author:** ![RylanSchaeffer](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rylanschaeffer/32/2543_2.png) [@RylanSchaeffer](https://discourse.psychopy.org/u/RylanSchaeffer)\
**Post date:** [June 1, 2018, 7:09pm UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580/5 "2018-06-01T19:09:13Z")

</div>

It seems like editing `psychopy.visual.window.py` to pass `visible=False` to `pyglet.window.Window` works, but this is hacky and I don’t like modifying a library’s code. Is there another way?

---

<div class="post-metadata">

**Author:** ![djmannion](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/djmannion/32/37535_2.png) [@djmannion](https://discourse.psychopy.org/u/djmannion)\
**Post date:** [June 1, 2018, 11:27pm UTC](https://discourse.psychopy.org/t/extracting-window-movie-frame-without-creating-window/4580/6 "2018-06-01T23:27:38Z")

</div>

> [@RylanSchaeffer](#):
>
> Is there another approach that doesn’t require writing the frames to disk? I’d like to immediately use the frames within the same python script.

You aren’t required to write the frames to disk; `getMovieFrame` returns a PIL image.
