Saving Canvas to an image file?

480 views
Skip to first unread message

Matthew Bellis

unread,
Feb 21, 2014, 8:51:31 PM2/21/14
to vi...@googlegroups.com
Hi all,

  I've modified the galaxy.py example to read in from an n-body simulations output file and it works great! I'm actually going to have to run over many of these files, but I'd like to save the image automatically to a file in the script. The format doesn't matter so much (.png, .pdf, eps, etc.) just so long as it looks halfway decent. 

   Is there any way to do this? Thanks for anyone who can help!

Matt

Almar Klein

unread,
Feb 22, 2014, 1:22:27 AM2/22/14
to vi...@googlegroups.com
Hi Matt,


> I've modified the galaxy.py example to read in from an n-body simulations output file and it works great!

How big is the data file? Would there be a way to create an example based on it? We would like that very much :)



> I'd like to save the image automatically to a file in the script.

Yes, we actually do something similar for generating the online galery: https://github.com/vispy/vispy/blob/master/make/make.py#L203
You need to make a screenshot using gloo._screenshot().

Regards,
  Almar



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

Matthew Bellis

unread,
Feb 23, 2014, 3:25:14 PM2/23/14
to vi...@googlegroups.com
Hi Almar,
 
> I've modified the galaxy.py example to read in from an n-body simulations output file and it works great!

How big is the data file? Would there be a way to create an example based on it? We would like that very much :)


   It's a 25MB text file I'm reading in and right now I've totally hacked the galaxy.py example, without really grokking everything that's going on in there.  :)   I'm happy to send you a slightly cleaned up version code and datafile. If you'd like to post it as an example that's fine with me! I've attached the image of the n-body simulation I made with vispy. Looks pretty good to me!
 

> I'd like to save the image automatically to a file in the script.

Yes, we actually do something similar for generating the online galery: https://github.com/vispy/vispy/blob/master/make/make.py#L203
You need to make a screenshot using gloo._screenshot().


   Ah, excellent! So I've modified the galaxy.py example so that __main__ looks like

if __name__ == '__main__':
    c = Canvas()
    c.show()
    app.run()

    from vispy.gloo import _screenshot
    from vispy.util.dataio import imsave
    im = _screenshot((0, 0, c.size[0], c.size[1]))
    imsave('gloo_screenshot.png', im)

 and it saves the first image of the galaxy as a .png. To exit though, I need to close the canvas manually in my workspace. How would I get the script to exit, once the canvas is drawn and the image saved? I'm afraid I don't actually know what is happening in app.run().

   Thanks again. Vispy looks like a *great* project!

Matt

Luke Campagnola

unread,
Feb 25, 2014, 5:52:40 PM2/25/14
to vi...@googlegroups.com, gael....@gmail.com
What does everyone think about adding a new repository for large demos? I'd like to see the galaxy sim, Gael's molecular viewer (perhaps he'll let us post that wicked micelle simulation?), MRI data, electrophys recordings, etc. 


On Sun, Feb 23, 2014 at 3:25 PM, Matthew Bellis <matthew...@gmail.com> wrote:
 
> I've modified the galaxy.py example to read in from an n-body simulations output file and it works great!

How big is the data file? Would there be a way to create an example based on it? We would like that very much :)
 
   It's a 25MB text file I'm reading in and right now I've totally hacked the galaxy.py example, without really grokking everything that's going on in there.  :)   I'm happy to send you a slightly cleaned up version code and datafile. If you'd like to post it as an example that's fine with me! I've attached the image of the n-body simulation I made with vispy. Looks pretty good to me!
 

Almar Klein

unread,
Feb 26, 2014, 6:48:11 AM2/26/14
to vi...@googlegroups.com, gael....@gmail.com
> How would I get the script to exit, once the canvas is drawn and the image saved? I'm afraid I don't actually know what is happening in app.run().

app.quit()  should do the trick.


What does everyone think about adding a new repository for large demos? I'd like to see the galaxy sim, Gael's molecular viewer (perhaps he'll let us post that wicked micelle simulation?), MRI data, electrophys recordings, etc. 

Yes, and we need a system to download+store it. I think this needs a generic approach that can also be used for libraries (e.g. angle, freetype).

- Almar

 


On Sun, Feb 23, 2014 at 3:25 PM, Matthew Bellis <matthew...@gmail.com> wrote:
 
> I've modified the galaxy.py example to read in from an n-body simulations output file and it works great!

How big is the data file? Would there be a way to create an example based on it? We would like that very much :)
 
   It's a 25MB text file I'm reading in and right now I've totally hacked the galaxy.py example, without really grokking everything that's going on in there.  :)   I'm happy to send you a slightly cleaned up version code and datafile. If you'd like to post it as an example that's fine with me! I've attached the image of the n-body simulation I made with vispy. Looks pretty good to me!
 

--

Matt

unread,
Feb 26, 2014, 9:47:05 AM2/26/14
to vi...@googlegroups.com, gael....@gmail.com
Hi Almar,


> How would I get the script to exit, once the canvas is drawn and the image saved? I'm afraid I don't actually know what is happening in app.run().

app.quit()  should do the trick.


   No, that doesn't seem to work. Again, if I just return to the galaxy.py example, __main__ looks like this:

if __name__ == '__main__':
    c = Canvas()
    c.show()
    app.run()
    app.quit()
    from vispy.gloo import _screenshot
    from vispy.util.dataio import imsave

    im = _screenshot((0, 0, c.size[0], c.size[1]))
    imsave('gloo_screenshot_galaxy.png', im)
    print "Saved!"

   The Canvas with the galaxy image pops up, but won't go away unless I close it by hand. Once I close it, the .png is saved properly however, so that's good. But it won't get to app.quit() unless I close it by hand. 

 

What does everyone think about adding a new repository for large demos? I'd like to see the galaxy sim, Gael's molecular viewer (perhaps he'll let us post that wicked micelle simulation?), MRI data, electrophys recordings, etc. 

   If this moves forward, I'm happy to upload the data and the sample nbody viewer I'm working on! 

--
-- 
----------------------------
Matt Bellis
Siena College
http://www.mattbellis.com
(cell)    412-310-4586
----------------------------

Luke Campagnola

unread,
Feb 26, 2014, 10:26:30 AM2/26/14
to vi...@googlegroups.com
On Wed, Feb 26, 2014 at 9:47 AM, Matt <mbe...@siena.edu> wrote:
What does everyone think about adding a new repository for large demos? I'd like to see the galaxy sim, Gael's molecular viewer (perhaps he'll let us post that wicked micelle simulation?), MRI data, electrophys recordings, etc. 

   If this moves forward, I'm happy to upload the data and the sample nbody viewer I'm working on! 

I just created a vispy/demos repository. You are welcome to make a PR against that (maybe put everything inside a 'galaxy' directory).

To avoid excessive growth in this repository, it is best to compress the data and commit it once. New code may be committed in the future, but every time the data changes it forces the repository to grow. 

 

Almar Klein

unread,
Feb 26, 2014, 2:59:58 PM2/26/14
to vi...@googlegroups.com, gael....@gmail.com
app.quit()  should do the trick.


   No, that doesn't seem to work. Again, if I just return to the galaxy.py example, __main__ looks like this:

Ah, doh. You don't want to enter the main loop, because it will block. Instead run app.process_events() a couple of times. And c.close() at the end.

Matt

unread,
Feb 26, 2014, 4:15:38 PM2/26/14
to vi...@googlegroups.com, Gael Goret
Ah, excellent! Thanks, Almar!

And yes, if there's an appropriate spot, I can upload this example! And maybe someone will help make it look better.  :)

Matt


--
You received this message because you are subscribed to a topic in the Google Groups "vispy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vispy/vgynqRasCtE/unsubscribe.
To unsubscribe from this group and all of its topics, send an email to vispy+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages