def save_animation(self): if self.last_result is not None: filename, format_str = self.file_dialog.getSaveFileName(self, self.tr("Save the animation of this SSU result"), None, self.tr("MPEG-4 Video File (*.mp4);;Graphics Interchange Format (*.gif)")) if filename is None or filename == "": return progress = QProgressDialog(self) progress.setRange(0, 100) progress.setLabelText(self.tr("Saving Animation [{0} Frames]").format(self.last_result.n_iterations)) canceled = False def save_callback(i, n): if progress.wasCanceled(): nonlocal canceled canceled = True raise StopIteration() progress.setValue((i+1)/n*100) QCoreApplication.processEvents() self.show_result(self.last_result) # plt.rcParams["savefig.dpi"] = 120.0 if "*.gif" in format_str: if not ImageMagickWriter.isAvailable(): self.normal_msg.setWindowTitle(self.tr("Error")) self.normal_msg.setText(self.tr("ImageMagick is not installed, please download and install it from its offical website (https://imagemagick.org/index.php).")) self.normal_msg.exec_() else: self.animation.save(filename, writer="imagemagick", fps=30, progress_callback=save_callback) elif "*.mp4" in format_str: if not FFMpegWriter.isAvailable(): self.normal_msg.setWindowTitle(self.tr("Error")) self.normal_msg.setText(self.tr("FFMpeg is not installed, please download and install it from its offical website (https://ffmpeg.org/).")) self.normal_msg.exec_() else: self.animation.save(filename, writer="ffmpeg", fps=30, progress_callback=save_callback) # plt.rcParams["savefig.dpi"] = 300.0 if not canceled: progress.setValue(100)
def save_animation(self, filename: str = None): if self._animation is None: return self._animation.pause() if filename is None: filename, format_str = QtWidgets.QFileDialog.getSaveFileName( self, self. tr("Choose a filename to save the animation of this SSU result" ), ".", "MPEG-4 Video File (*.mp4);;Html Animation (*.html);;Graphics Interchange Format (*.gif)" ) if filename is None or filename == "": return progress_dialog = QtWidgets.QProgressDialog( self.tr("Saving Animation Frames..."), self.tr("Cancel"), 0, 100, self) progress_dialog.setWindowTitle("QGrain") progress_dialog.setWindowModality(QtCore.Qt.WindowModal) def callback(frame_number, total_frames): if progress_dialog.wasCanceled(): raise StopIteration() progress_dialog.setValue(int(frame_number / total_frames * 100)) QtCore.QCoreApplication.processEvents() try: if filename[-5:] == ".html": if not FFMpegWriter.isAvailable(): self.show_error(self.tr("FFMpeg is not installed.")) else: self.show_info( self. tr("Rendering the animation to a html5 video, it will take several minutes." )) html = self._animation.to_html5_video() with open(filename, "w") as f: f.write(html) elif filename[-4:] == ".gif": if not ImageMagickWriter.isAvailable(): self.show_error(self.tr("ImageMagick is not installed.")) else: self._animation.save(filename, writer="imagemagick", fps=10, progress_callback=callback) elif filename[-4:] == ".mp4": if not FFMpegWriter.isAvailable(): self.show_error(self.tr("FFMpeg is not installed.")) else: self._animation.save(filename, writer="ffmpeg", fps=10, progress_callback=callback) except StopIteration: self.logger.info("The saving task was canceled.") finally: progress_dialog.close()
def _anim_rst(anim, image_path, gallery_conf): from matplotlib.animation import ImageMagickWriter # output the thumbnail as the image, as it will just be copied # if it's the file thumbnail fig = anim._fig image_path = image_path.replace('.png', '.gif') fig_size = fig.get_size_inches() thumb_size = gallery_conf['thumbnail_size'] use_dpi = round(min(t_s / f_s for t_s, f_s in zip(thumb_size, fig_size))) # FFmpeg is buggy for GIFs if ImageMagickWriter.isAvailable(): writer = 'imagemagick' else: writer = None anim.save(image_path, writer=writer, dpi=use_dpi) html = anim._repr_html_() if html is None: # plt.rcParams['animation.html'] == 'none' html = anim.to_jshtml() html = indent(html, ' ') return _ANIMATION_RST.format(html)