Skip to content

Exclude formats with ! when opening#9564

Open
radarhere wants to merge 5 commits intopython-pillow:mainfrom
radarhere:open
Open

Exclude formats with ! when opening#9564
radarhere wants to merge 5 commits intopython-pillow:mainfrom
radarhere:open

Conversation

@radarhere
Copy link
Copy Markdown
Member

Currently, to prevent opening a file in a certain format, without limiting your access to any other format, you can

from PIL import Image
Image.init()
Image.open(path, formats=[f for f in Image.ID if f != "EPS"])

or

from PIL import Image, EpsImagePlugin
Image.ID.remove("EPS")
Image.open(path)

It would be nice if there was a simpler way. This PR suggests

from PIL import Image
Image.open(path, formats=["!EPS"])

And yes, I suggest that Image.open(path, formats=["EPS", "!EPS"]) still does not allow an EPS image to be opened.

Copy link
Copy Markdown
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Right, so:

Image.open(path)  # tries all, default formats=None
Image.open(path, formats=None)  # tries all
Image.open(path, formats=["JPEG"])  # only tries JPEG
Image.open(path, formats=["!EPS"])  # tries all but EPS
Image.open(path, formats=["EPS", "!EPS"])  # tries all but EPS
Image.open(path, formats=["JPEG", "!EPS"])  # only tries JPEG, !EPS has no effect

That last one could be surprising, but it's probably okay.

Comment thread src/PIL/Image.py Outdated
Comment thread src/PIL/Image.py Outdated
Comment thread src/PIL/Image.py Outdated
Comment thread src/PIL/Image.py Outdated
radarhere and others added 3 commits April 16, 2026 00:12
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@radarhere
Copy link
Copy Markdown
Member Author

Image.open(path, formats=["EPS", "!EPS"]) # tries all but EPS

I originally thought this should try none, but it's an edge case that could be debated. I've pushed your suggestions, and gone with trying all except EPS.

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