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

Convenience methods for Pillow integration #486

Open
jonashaag opened this issue Jul 15, 2024 · 4 comments
Open

Convenience methods for Pillow integration #486

jonashaag opened this issue Jul 15, 2024 · 4 comments

Comments

@jonashaag
Copy link

Proposal: Add convenience integration with PIL/Pillow, eg. converting back and forth. Something along the lines of

pyvips.Image.from_pil(pil_img)

and

vips_img.pil()  # -> PIL.Image.Image

Happy to contribute a first draft if this is accepted.

@jcupitt
Copy link
Member

jcupitt commented Jul 15, 2024

Hi @jonashaag, sure this sounds useful. What would it add over going via numpy?

@jonashaag
Copy link
Author

Probably nothing on the technical side, just a few convenience methods.

@jcupitt
Copy link
Member

jcupitt commented Jul 15, 2024

It should be pretty easy right now:

import PIL.Image
pil_image = PIL.Image.new('RGB', (60, 30), color = 'red')
image = pyvips.Image.new_from_array(pil_image)

and:

import pyvips
import PIL.Image
image = pyvips.Image.black(100, 100, bands=3)
pil_image = PIL.Image.fromarray(image.numpy())

I don't know what PIL does with image metadata, maybe there's a simple way to bring that over as well?

@jonashaag
Copy link
Author

jonashaag commented Jul 17, 2024

You're right that it's already quite simple... my suggestion of from_pil is the same as new_from_array (it's just a bit confusingly named for that purpose), and my convenience method for the other direction would be

    def pil(self):
        try:
            from PIL import Image
        except ModuleNotFoundError as e:
            raise ModuleNotFoundError("Pillow must be installed") from e

        return Image.fromarray(self.numpy())

Honestly not sure anymore if it's worth adding this because it's a very thin wrapper...

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