Skip to content

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
@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

2 participants