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 suggestion: .odc.thumbnail() method for generating and exporting low-res thumbnail images #126

Open
robbibt opened this issue Feb 2, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@robbibt
Copy link
Contributor

robbibt commented Feb 2, 2024

It would be very useful to have an easy .odc-accessible method for generating low-resolution thumbnail images, and exporting these to file:

It won't hurt to have something like .odc.compress(...., save_to="local/path/to/image.{png,jpeg,webp}"), or better a separate method like .odc.thumbnail() with some reasonable defaults for image output size with ability to override that, and maybe arguments like webmap=True|False for automatic conversion to epsg:3857? That method could then accept some form of output= argument supporting file, ram or fsspec.

...

Original question:

I'm trying to export a 320 x 320 pixel thumbnail to a compressed JPEG file on disk using odc-geo. I've been able to do the following, however I'm unsure of how to actually save the output of .odc.compress() as a file, not a string (?) representation.

(
    elevation.odc.reproject(
        how=elevation.odc.geobox.zoom_to(320), resampling="min"
    )
    .odc.colorize(vmin=-2.5, vmax=1.5, cmap="viridis")
    .odc.compress("jpeg", 85)
)

image

What do I need to do to save this output as a JPEG file on disk?

@robbibt robbibt added the help wanted Extra attention is needed label Feb 2, 2024
@Kirill888
Copy link
Member

Kirill888 commented Feb 2, 2024

This can be viewed directly in jupyter with something like this:

from IPython.display import Image, display

display(Image(data=<...result of your code...>))

Or you can supply .odc.compress(..., as_data_url=True) and add result of that as an image overlay to a map or use in some HTML output of sorts.

There are no "save to disk" methods as such, it was developed for output to html mostly, with an assumption that the result takes a tiny fraction of the overall memory. One can always save bytes to a file obviously, something like this:

with open(output_path, "wb") as f:
  f.write(data)

But there are no arguments or methods to do that within odc.geo currently. Or maybe you want to use fsspec and write to a zip file or S3 bucket or embed data url into a STAC document.

It won't hurt to have something like .odc.compress(...., save_to="local/path/to/image.{png,jpeg,webp}"), or better a separate method like .odc.thumbnail() with some reasonable defaults for image output size with ability to override that, and maybe arguments like webmap=True|False for automatic conversion to epsg:3857? That method could then accept some form of output= argument supporting file, ram or fsspec.

@robbibt
Copy link
Contributor Author

robbibt commented Feb 5, 2024

Ah, this was all I was looking for, thanks:

with open(output_path, "wb") as f:
  f.write(data)

I think an .odc.thumbnail or .odc.write_thumbnail method would be super useful - it's a super common use case that is currently a bit clunkier than it needs to be. I'll update this issue.

@robbibt robbibt changed the title Exporting JPEG thumbnail to file using odc-geo? Feature suggestion: .odc.thumbnail() method for generating and exporting low-res thumbnail images Feb 5, 2024
@robbibt robbibt added enhancement New feature or request and removed help wanted Extra attention is needed labels Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants