Skip to content

Avoid calling PIL.Image twice to open a file - #1149

Merged
FirefoxMetzger merged 3 commits into
imageio:masterfrom
hmaarrfk:patch-2
Oct 22, 2025
Merged

FirefoxMetzger merged 3 commits into
imageio:masterfrom
hmaarrfk:patch-2

Conversation

@hmaarrfk

@hmaarrfk hmaarrfk commented Oct 5, 2025

Copy link
Copy Markdown
Contributor

Sometimes reading metadata is just slow.... lets avoid it...

Sometimes reading metadata is just slow.... lets avoid it...
Comment thread imageio/plugins/pillow.py Resolved
@hmaarrfk

hmaarrfk commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

seems like something is off in the windows ffmpeg build?

@FirefoxMetzger

FirefoxMetzger commented Oct 6, 2025 •

Copy link
Copy Markdown
Contributor

@hmaarrfk Yes, there exists currently a flakey test for the old ffmpeg plugin (not the pyav one) where something in the shutdown of the subprocess is racy which may causes a test to fail. I'm not super motivated to fix it though, because the "damage" it does is that I need to spend 2 seconds to click on "re-run failed tests" 🤷 and the entire plugin will get deprecated in favour of the pyAV one anyway.

That issue doesn't affect us here though. This CI run is failing because FFMPEG appears to now support a new hardware accelerator codec called ohcodec which we are not expecting. A quick google says that this is a new addition from the last few weeks, so I need to block out some time to add it to the list of other virtual or hardware codecs that we should not use during testing.

@FirefoxMetzger

FirefoxMetzger commented Oct 6, 2025 •

Copy link
Copy Markdown
Contributor

I'll review the PR later this week. I remember that there is a reason why the file opening is written so awkwardly ... something about not leaking a file reference in an edge-case ... but it's been a few years since I touched that code so I need to dig through it again to refresh my memory 😅

@hmaarrfk

hmaarrfk commented Oct 6, 2025

Copy link
Copy Markdown
Contributor Author

something about not leaking a file reference in an edge-case ...

yeah, it seems that this was the reason, but i can't follow the kind of edge case it helps with.

Thx tbh, this isn't really on my critical path, it just looked really weird....

@FirefoxMetzger

Copy link
Copy Markdown
Contributor

Okay, I got the logic now. Pillow's Image.open actually doesn't close the file if you pass a file handle and, as a result, we don't get two OS calls to open the file twice. We simply pass around file handles.

It's safe to merge the two calls into one, but we won't get a big performance boost out of it I fear.

@FirefoxMetzger
FirefoxMetzger merged commit 6a45d8a into imageio:master Oct 22, 2025
@hmaarrfk

Copy link
Copy Markdown
Contributor Author

ah got it.

@hmaarrfk

Copy link
Copy Markdown
Contributor Author

not sure i agree.

THe old way

  1. Open call when entering the context manager (read metadata and whatnot)
  2. CLose call when exiting the context manager
  3. Open call again (read metadata again and whatnot)

Alot of time is "wasted' reading metadata from files. as that metadata grows, it becomes an increased cost.

Its just a tiny optimization, but also improves readability IMO.

Thx for merging.

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.

2 participants