fusion-flap / flap

Fusion Library of Analysis Programs
MIT License
11 stars 5 forks source link

plot.py cv2 video saving: #62

Closed thelampire closed 4 years ago

thelampire commented 4 years ago

There could be an option of "Video saving only" where the animation is not plotted but only saved in a video file. I tried to implement this by not running the plt.show() when the _options['Video saving only'] is True but then there is nothing in the buffer and no video is produced. This feature would be quite useful as the computer cannot be used at all when an anim-image or anim-contour is running. Furthermore, the shotnumber should be in the title of the anim-image/anim-contour.

The main problem is that the computer is rendered useless, when the video is saved, because the matplotlib window is overlayed on top of everything. Furthermore, if I click somewhere, the resulting video frame will be smeared.

Originally posted by @thelampire in https://github.com/fusion-flap/flap/issues/40#issuecomment-540225376

thelampire commented 4 years ago

I found a solution for this, but it is not a very straightforward one. The following needs to be called:

import matplotlib current_backend=matplotlib.get_backend() matplotlib.use('agg') flap.plot() matplotlib.use(current_backend)

The backend is kind of a global matplotlib variable which needs to be set back after setting it to a non-GUI backend like the 'agg'.

The problem is, that inside the flap.plot() this cannot be realized, because the backend needs to be set before 'import matplotlib.pyplot as plt'. There is no way around it according to the matplotlib documentation.

What do you think?

thelampire commented 4 years ago

Also, video saving can fail at the buf.shape = ( h,w,3 ) line as for scaled dispays the numpy buffer is twice as big as the canvas (in pixels). I added the following lines, but this is probably not the best long term solution. (It would fail for 8K displays :D):

            if buf.shape[0] == h*2 * w*2 * 3:
                buf.shape = ( h*2, w*2, 3 ) #ON THE MAC'S INTERNAL SCREEN, THIS COULD HAPPEN NEEDS A MORE ELEGANT FIX
            else:
                buf.shape = ( h, w, 3 )
thelampire commented 4 years ago

I am closing this issue as that is the only solution except saving just the data as a plot without the axes. The actual solution is the following (the former one didn't set the backend back to its original):

import matplotlib current_backend=matplotlib.get_backend() matplotlib.use('agg') flap.plot() import matplotlib matplotlib.use(current_backend)