Skip to content

Add feature to store visualizations as JPG images#2400

Open
lingachen wants to merge 3 commits into
pymor:mainfrom
lingachen:chenla-dev
Open

Add feature to store visualizations as JPG images#2400
lingachen wants to merge 3 commits into
pymor:mainfrom
lingachen:chenla-dev

Conversation

@lingachen

@lingachen lingachen commented Jan 19, 2025

Copy link
Copy Markdown

This PR predates #2405 and may need to be updated to reflect the VectorArray interface changes in #2405.

This pull request is for issue #2146, which wants to have an option to store visualization results as image files.

Features

For both PatchVisualizer and OnedVisualizer, the visualization result could be saved as image files by specifying the img_filename argument as calling the function visualize, as shown below:

visualize(U, img_filename="IMG_NAME")
  1. Single image output: a single JPG file named with the argument will be stored in the working directory.
  2. Multiple image output: a folder named with the argument will be created in the working directory first. The frames will be stored as sequentially numbered JPG files inside the folder (e.g. 0.jpg, 1.jpg, etc.).

Testing

I have tested the code with :

  1. The example provided in the issue for PatchVisualizer.
  2. The following code for OnedVisualizer:
from pymor.models.examples import heat_equation_1d_example

fom_tb = heat_equation_1d_example()
mu = 0.1
U = fom_tb.solve(mu)
fom_tb.visualize(U, img_filename="1D_test")

If there is any problems or feedbacks, please let me know.

@github-actions

Copy link
Copy Markdown
Contributor

Mirroring external branch in external_pr_2400

@lingachen

Copy link
Copy Markdown
Author

The animation output is now saved not only as individual frame images but also as an MP4 file.

@sdrave sdrave left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lingachen, thanks a lot for your contribution.
I did not have the time to really test your code, but it looks good!

One change I'd suggest is to merge the filename and img_filename argument into a single filename argument and dispatch based on the provided file extension. I think this pattern is quite common in similar libraries.

Also, it would be nice if png output would be supported as well.

config.require('MATPLOTLIB')



Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@lingachen

Copy link
Copy Markdown
Author

Hi @sdrave, thanks for your suggestion!

Currently, the visualize function has a single filename argument that supports .vtk, .mp4, and all file formats compatible with matplotlib.pyplot.savefig (e.g. .png, .jpg, .pdf, etc.).

Here are some details about the implementation:

  • In PatchVisualizer, to keep the existing behavior, data is saved as a VTK file by default if no file extension is specified.
  • In OnedVisualizer, data is saved as PNG file(s) by default if no file extension is specified.
  • For animations, if the specified file extension is .mp4, a single video file is created; otherwise, a folder containing individual frame images is generated.
  • For single-frame visualizations, if the specified file extension is .mp4, the output is saved as a PNG file instead.

@sdrave sdrave linked an issue Mar 24, 2025 that may be closed by this pull request
@sdrave sdrave self-assigned this Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Storing visualizations as images or videos

2 participants