Screenshot produces black image
Hello,
I am using the following code to plot. How do I save the plot to image like JPG? Thanks.
v = vv.volshow(volRGB, renderStyle="iso")
v.transformations[1].sz = 0.5 # Z was twice as deep during training
l0 = vv.gca()
l0.light0.ambient = 0.9 # 0.2 is default for light 0
l0.light0.diffuse = 1.0 # 1.0 is default
a = vv.gca()
a.camera.fov = 0 # orthographic
vv.use().Run()
vv.screenshot should do the trick.
@almarklein
Thanks for the reply, But actually it is not, the saved image is totally black. I am using:
vv.screenshot('screenshot.jpg', vv.gcf(), sf=2, bg='w')
Are you using visvis interactively (using Spyder, Pyzo, IPython), or as a script. If you add the call to screenshot before Run, then it indeed wont work.
An alternative could be to use vv.callLater
vv.callLater(1.0, vv.screenshot, "~/screenshot.jpg", vv.gcf(), sf=2)
vv.use().Run()
@almarklein
Thanks for the reply. But it is still not working on my side.
Is it possible to save the plot without displaying on the screen. So we can save a lot plots for many files without closing the plot.
Regards,
Thanks for the reply. But it is still not working on my side.
So the screenshot is still all black? And is the plot shown in the figure ok?
Is it possible to save the plot without displaying on the screen.
Nope. The screenshot depends on a GL screen buffer.
Thanks for the reply. Yes, the saved screenshot is still black but the plot shown in the figure ok.
Hey, was this ever resolved? I am seeing the same behavior on Ubuntu where I can display a figure just fine but attempting to capture a screenshot of it yields a black image .... Thanks for any pointers!
Is it working in an interactive session? My best guess is that GL has not had a chance to write to the framebuffer yet.
Thanks for responding. I have been trying it in a script, but I just tried it in an ipython session and again got a black image.
This is the script I am trying to run:
import visvis as vv
vv.plot([2,3,1])
vv.screenshot('plot.jpg')
I saw this post on SO and have tried the solutions there without success.
If you are interested, here's what I am trying to do and what I have tried so far. Perhaps you can point out what I am missing:
I have a figure that I want to capture some screenshots of. I can do this with visvis on my laptop (MacOS) both interactively and as a script. However, due to the nature of my application, this needs to take place in a docker container. My initial attempts revealed that I needed to give the container a screen, which I did as per this post. Once that was done, and I got some other dependencies installed through trial and error (pyqt5, opengl, ...) I was able to run the above code without error in the container. BUT as I have said, the result is a completely black image. Curious to see if visvis would at least plot to the virtual screen, I ran the following script:
import visvis as vv
vv.plot([2,3,1])
vv.use().Run()
I then used the import command from imagemagick to capture a screenshot of the virtual display:
So the figure is being drawn to the screen. But for a reasons I don't understand, trying to capture a screenshot of that results in an empty image (all pixels black).
Unable to do what I wanted with visvis, I tried using mayavi. As it turns out, the behavior is very similar. I can plot to the virtual display (and capture a picture with imagemagick) but attempts to capture the image with mlab.savefig result in a black image. It is interesting to note that matplotlib.pyplot.savefig seems to work as expected (image saved is normal) but I cannot use this for displaying my figure as it is a surface in 3D. As both visvis and mayavi are experiencing the same issue, it makes me think I am missing something in my container. Any thoughts on what could be causing this?
Also, in trying to save an image with visvis, mayavi, and matplotlib.pyplot, I noticed that part of the process for the first two libraries involves briefly opening a window (as if a figure were to be displayed) which is then quickly closed. (I have seen this on my mac). Matplotlib.pyplot does not do this. I wonder what's going on there?
If you got this far, thanks for reading :) If you have any thoughts, I would appreciate them. Thanks for your time.
Have you tried adding some vv.processEvents() (call it e.g. 10 times) after setting up the viz and before taking the screenshot?
Hello, I had the same issue, and I found out that the problem is vv.getframe() takes the screen too fast. So the previous recommendation of using a delay worked for me:
import visvis as vv app = vv.use('glfw') vv.plot([2,3,1]) vv.callLater(2, vv.screenshot, "screenshot.jpg", vv.gcf(), sf=1) app.Run()