Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Record a plot to a image #386

Closed
balbok0 opened this issue Dec 1, 2023 · 4 comments
Closed

[feature request] Record a plot to a image #386

balbok0 opened this issue Dec 1, 2023 · 4 comments

Comments

@balbok0
Copy link

balbok0 commented Dec 1, 2023

Hi,

First of all, congrats on a great project. It's quite amazing how fast this package is, and easy to use it is as well. Even with limited examples, documentation makes is pretty easy to use.

I'm working on generating some images for human labeling for ML workflows. These, due to tooling need to be saved as .png, but it seems that it is a missing functionality. How hard would it be to implement such functionality, and if possible, would someone be able to point me in the right direction? I am happy to contribute, but unsure where to start.

It looks like rendering to a video file is implemented: #171, and this seems like an easier task, so hopefully a newbie to project (like me) can help!

@kushalkolar
Copy link
Member

Hi! Thanks for posting an issue! 😄 That's definitely the types of use case that inspired fastplotlib in the beginning!

Do you bacially want to save the data of an image graphic to a PNG? Can you post a screenshot or example of what you're looking for?

@balbok0
Copy link
Author

balbok0 commented Dec 2, 2023

Hi, here is a screenshot of the plot I'm conceptually looking for:
Screenshot from 2023-12-02 13-56-13

I would prefer to save it as png, but other popular image formats should work too (jpeg etc.).

Here is a relevant code:

import fastplotlib as fplt
import numpy as np

grid_plot = fplt.GridPlot((2, 3))

# Image
img = (np.random.random((720, 1280, 3)) * 255).astype(dtype=np.int8)
grid_plot[0, 0].add_image(data=img)

# Predicted Image with features over-layed on top
pred_img = (np.random.random((200, 200)) * 255).astype(dtype=np.int8)
grid_plot[0, 1].add_image(data=pred_img)
grid_plot[0, 1].set_title("Pred Image with features over-layed on top")
grid_plot[0, 1].add_line(data=np.arange(10, 60).reshape(-1, 2), colors="yellow")
grid_plot[0, 1].add_line(data=np.random.rand(1, 2) * 200, colors="red")

# PDF, but there is no histogram.
# Implemented as a heatmap, with color serving the scaling purpose
probs = np.linalg.norm(np.random.rand(2, 1), axis=1, keepdims=True)
grid_plot[0, 2].add_heatmap(data=probs, vmin=0, vmax=1.0)
grid_plot[0, 2].set_title("Cost Histogram")


# Predicted line with more features
grid_plot[1, 0].set_title("Pred Line with bounds")
x = np.arange(1, 200)
y = np.sqrt(x)
dy = 3 * np.log(y)
grid_plot[1, 0].add_line(np.array([x, y]).T, colors="blue")
grid_plot[1, 0].add_line(np.array([x, np.maximum(0, y - dy)]).T, colors="blue", alpha=0.5)
grid_plot[1, 0].add_line(np.array([x, y + dy]).T, colors="blue", alpha=0.5)

# Show angles. In matplotlib this was also implemented as histogram/bar chart.
grid_plot[1, 1].add_heatmap(data=np.random.rand(2, 1) * 90, vmin=-45, vmax=45)
grid_plot[1, 1].set_title("Angles")

grid_plot[1, 2].add_heatmap(data=np.random.randn(9, 1), vmin=-25, vmax=25)
grid_plot[1, 2].set_title("More distributions")

# Here I would like to
# grid_plot.render() # <- Optionally, or some other call
# grid_plot.save_to_file(file_path)  # file_path should accept str and pathlib.Path types preferably

grid_plot.show()

# Small note: It's weird that there is discrepancy between arguments here: (width, height)
# and add_image function data shape (height, width)
grid_plot.canvas.set_logical_size(1280, 720)

for subplot in grid_plot:
    subplot.auto_scale()

fplt.run()

Overall, there are a few more features that are needed for me to use this library (unrelated to this issue, but most of them are listed on this roadmap: #55), but just wanted to call this one out, since I haven't noticed it being called out anywhere.

@kushalkolar
Copy link
Member

kushalkolar commented Dec 3, 2023

[deleted simpler solution below]

@kushalkolar
Copy link
Member

You can actually just call figure.renderer.snapshot() which returns an [x, y, rgba] array that you can then save using your desired image writer library.

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

No branches or pull requests

2 participants