Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Added MPEG accept function#7999

Merged
hugovk merged 2 commits into
python-pillow:mainpython-pillow/Pillow:mainfrom
radarhere:acceptradarhere/Pillow:acceptCopy head branch name to clipboard
May 11, 2024
Merged

Added MPEG accept function#7999
hugovk merged 2 commits into
python-pillow:mainpython-pillow/Pillow:mainfrom
radarhere:acceptradarhere/Pillow:acceptCopy head branch name to clipboard

Conversation

@radarhere

Copy link
Copy Markdown
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.