Skip to content

Commit

Permalink
Merge pull request #7999 from radarhere/accept
Browse files Browse the repository at this point in the history
Added MPEG accept function
  • Loading branch information
hugovk committed May 11, 2024
2 parents 5e48d54 + c6a3f0f commit 0c8be38
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
39 changes: 39 additions & 0 deletions Tests/test_file_mpeg.py
@@ -0,0 +1,39 @@
from __future__ import annotations

from io import BytesIO

import pytest

from PIL import Image, MpegImagePlugin


def test_identify() -> None:
# Arrange
b = BytesIO(b"\x00\x00\x01\xb3\x01\x00\x01")

# Act
with Image.open(b) as im:
# Assert
assert im.format == "MPEG"

assert im.mode == "RGB"
assert im.size == (16, 1)


def test_invalid_file() -> None:
# Arrange
invalid_file = "Tests/images/flower.jpg"

# Act / Assert
with pytest.raises(SyntaxError):
MpegImagePlugin.MpegImageFile(invalid_file)


def test_load() -> None:
# Arrange
b = BytesIO(b"\x00\x00\x01\xb3\x01\x00\x01")

with Image.open(b) as im:
# Act / Assert: cannot load
with pytest.raises(OSError):
im.load()
6 changes: 5 additions & 1 deletion src/PIL/MpegImagePlugin.py
Expand Up @@ -53,6 +53,10 @@ def read(self, bits: int) -> int:
return v


def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"\x00\x00\x01\xb3"


##
# Image plugin for MPEG streams. This plugin can identify a stream,
# but it cannot read it.
Expand All @@ -77,7 +81,7 @@ def _open(self) -> None:
# --------------------------------------------------------------------
# Registry stuff

Image.register_open(MpegImageFile.format, MpegImageFile)
Image.register_open(MpegImageFile.format, MpegImageFile, _accept)

Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"])

Expand Down

0 comments on commit 0c8be38

Please sign in to comment.