Capture and Save images from output

914 views
Skip to first unread message

Raju Gupta

unread,
Jun 18, 2014, 7:25:47 PM6/18/14
to vpytho...@googlegroups.com
Hi everyone!!!

I have written a program and it runs successfully. My program is to increase size of the spheres in the form of loop. I have made it to increase 5 times and now I want my program to capture the image of the output window every time it increases the size of the sphere so that I can have it for future presentation. 

Thus, can anyone help me on how to have my program take screenshot of output window and save it?

I really appreciate your time and effort.

Thank you!!!

Aaron Titus

unread,
Jun 18, 2014, 7:58:47 PM6/18/14
to vpytho...@googlegroups.com
You can use Python to run a command that you would run from the command line (or prompt). For example, Python can call a screencapture utility or an image program like imagemagick.

On vpython.org there is a contributed program that shows how to do this on a Mac.



Aaron


--
You received this message because you are subscribed to the Google Groups "VPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Raju Gupta

unread,
Jun 18, 2014, 8:20:48 PM6/18/14
to vpytho...@googlegroups.com


Hey Aaron!!

Thank you for the reply. I really appreciate it. I looked at the example program on vpython.org but kind of did not get it. I run it on my laptop and it ran successfully, but I could not find out where did it save the frames on my computer coz I could not find it. I actually just need to capture the screen and save it as .jpg or .png file. If possible, can you please help me by giving a simple example? Also, let me tell you that I am using Linux.

Thank you again!!!

Raju

Bruce Sherwood

unread,
Jun 18, 2014, 8:50:55 PM6/18/14
to vpytho...@googlegroups.com
There isn't anything built into VPython for saving a screen image, though conceivably (I don't know) it would be possible to do this with the Python module PIL (Python Image Library).

Another option is to use this program in the Contributed section of vpython.org:

povexport-2012-07-10.zip A module by Ruth Chabay to export a VPython scene to POV-Ray, which can create a high-resolution ray-traced image as a targa file, which can then be further manipulated in image-processing applications such as Photoshop. Markus Gritsch, Bruce Sherwood, Scott David Daniels, and Guy Kloss also contributed.



Raju Gupta

unread,
Jun 18, 2014, 10:59:10 PM6/18/14
to vpytho...@googlegroups.com
Hi Bruce!!

Thank you for the suggestion. I just tried some code using PIL 
from PIL import ImageGrab
import time

time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")

but on my Linux, it gives this error message

>>> import ImageGrab
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/dist-packages/PIL/ImageGrab.py", line 34, in
import _grabscreen
ImportError: No module named _grabscreen

Also, everytime I try to use Imagegrab, it gives same error message. Can you suggest any changes that I need to make?

Bruce Sherwood

unread,
Jun 19, 2014, 12:52:48 AM6/19/14
to vpytho...@googlegroups.com
Sorry, I don't know much about PIL, only that it seems plausible that it could be used for your purposes. However, the error message suggests that something went wrong in the installation of PIL, so you might try reinstalling.

Aaron Titus

unread,
Jun 19, 2014, 2:43:28 PM6/19/14
to vpytho...@googlegroups.com
This program is an example that might help you. I wrote it a long time ago and haven't looked at it in years. The program was meant to show the Sun moving along the ecliptic in the celestial sphere model.

In each iteration of the loop, the program calls a utility to capture the screen and save it as a pdf file. It does 367 screen captures, so it might take a little while to run. I think it will work with Linux because it uses the utility:

/usr/sbin/screencapture

which probably exists in Linux as well.

You'll have to use imagemagick or some other utility (along with a script) to easily crop all of the images.

I've pasted the program here. It's based on Chu's program on vpython.org. If you don't understand the program, then simplify it. To test it, you only need one line of code that calls the GetScreenShot function one time. You can pretty much erase everything else.

AT

---------------------------
from __future__ import division
from __future__ import print_function
from visual import *
from time import sleep
from commands import *

def GetScreenShot(FrameNumber):         # Take a screenshot and write
                                        # it to a numbered file.
    tmp = getoutput('/usr/sbin/screencapture celestial_sphere.%03d.pdf' % FrameNumber)
    print('Frame: %d' % (FrameNumber))
    return


scene.width=500
scene.height=500

R=10.0
thick=R/100
tilt=23.45#23.45 #degrees

earth=sphere(radius=1,color=color.blue)
ceq=ring(radius=R, axis=(0,1,0), thickness=thick, color=color.cyan)
caxis=cylinder(radius=thick, pos=(0,-R/4,0), axis=(0,R/2,0), color=color.white)
ecliptic=ring(radius=R, axis=(0,1,0), thickness=thick, color=color.yellow)
#tilt the ecliptic
ecliptic.axis=rotate(ecliptic.axis, angle=-pi/180*tilt)
sun=sphere(radius=1.0/2, color=color.yellow, pos=(0,0,R))
season = label(pos=(0,1.2*R,0), text='Season', xoffset=0, yoffset=0, space=0, height=20, border=10)
# day_label = label(pos=(0,-1.2*R,0), text='Day 1', xoffset=0, yoffset=0, space=0, height=20, border=10)
# northern_hem = label(pos=(-R,R,0), text="Northern Hemisphere", xoffset=0, yoffset=0, space=0, height=20, border=10)
# southern_hem = label(pos=(-R,-R,0), text="Southern Hemisphere", xoffset=0, yoffset=0, space=0, height=20, border=10)

scene.autoscale=0
scene.forward=rotate(vector(0,0,-1), angle=5*pi/180,axis=(0,1,0))

theta=0 #initial theta
dtheta=360/365*pi/180 #theta step
tol=dtheta/2 #tolerance 

rotationrate=1
maxcameraangle=15
cameraangle=0
FrameNumber=1

while cameraangle<maxcameraangle:
    rate(10)
    cameraangle=cameraangle+rotationrate
    scene.forward=rotate(scene.forward, angle=-rotationrate*pi/180,axis=(1,0,0))

# **movie** By positioning the VPython window in the top left hand corner, we
# know its position on the screen and then can correctly crop it out
# of the screenshot afterwards.
#
print('This program works only on MacOSX but could be extended to Unix/Linux.')
print('This program does NOT work on Microsoft Windows.')
raw_input('Position VPython window at top left hand corner. Then hit enter in this window.')
#

while theta<361*pi/180:
    rate(10)

    cycles=int(theta/2/pi)+1e-10
    phi=theta-(2*pi)*cycles
    
    if((phi>0-tol and phi<tol) or phi>2*pi-tol):
        season.text="Autumnal Equinox (~ Sept. 22)"
        sleep(3)
#       scene.mouse.getclick()
    elif(phi>(pi/2-tol) and phi<(pi/2+tol)):
        season.text="Winter Solstice (~ Dec. 21)"
        sleep(3)
#       scene.mouse.getclick()
    elif(phi>(pi-tol) and phi<(pi+tol)):
        season.text="Vernal Equinox (~ Mar. 20)"
        sleep(3)
#       scene.mouse.getclick()
    elif(phi>(3*pi/2-tol) and phi<(3*pi/2+tol)):
        season.text="Summer Solstice (~ June 21)"
        sleep(3)
#       scene.mouse.getclick()
    else:
        season.text=""

    sun.pos.x=R*sin(theta)*cos(tilt*pi/180)
    sun.pos.z=R*cos(theta)
    sun.pos.y=-R*sin(theta)*sin(tilt*pi/180)
    
#   day=int(phi*180/pi*365/360+1)+1.
#   day_text="Day day" + day
#   day_label.text=day_text
    
    theta=theta+dtheta

# **movie** Use the screencapture utility to get a screenshot.  
    GetScreenShot(FrameNumber)
    FrameNumber = FrameNumber + 1
#

----------------------

Raju Gupta

unread,
Jun 19, 2014, 5:37:40 PM6/19/14
to vpytho...@googlegroups.com
Hey Aaron,

Thank you very much for this program. I was kind of looking for same kind of program. I hope it works for me. Thank you again. I really appreciate it.


--
You received this message because you are subscribed to a topic in the Google Groups "VPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vpython-users/vJtHyHRePs8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vpython-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages