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

Added MPEG accept function #7999

Merged
merged 2 commits into from
May 11, 2024
Merged

Added MPEG accept function #7999

merged 2 commits into from
May 11, 2024

Conversation

radarhere
Copy link
Member

Investigating MpegImagePlugin's _open function,

def _open(self) -> None:
assert self.fp is not None
s = BitStream(self.fp)
if s.read(32) != 0x1B3:
msg = "not an MPEG file"
raise SyntaxError(msg)

this is saying that MPEG images must start with b"\x00\x00\x01\xb3".

This is what https://en.wikipedia.org/wiki/List_of_file_signatures describes as the magic number for "MPEG-1 video and MPEG-2 video (MPEG-1 Part 2 and MPEG-2 Part 2)"

So I have added an _accept function to check the magic number. This is a minor performance improvement, allowing non-MPEG images to be rejected without further file reading.

I have also added tests for the MPEG format, as those were not present. These are based on the first few tests of test_file_bufrstub.py.

@hugovk hugovk merged commit 0c8be38 into python-pillow:main May 11, 2024
55 of 56 checks passed
@radarhere radarhere deleted the accept branch May 11, 2024 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants