File tree Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Expand file tree Collapse file tree 2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change
1
+ from __future__ import annotations
2
+
3
+ from io import BytesIO
4
+
5
+ import pytest
6
+
7
+ from PIL import Image , MpegImagePlugin
8
+
9
+
10
+ def test_identify () -> None :
11
+ # Arrange
12
+ b = BytesIO (b"\x00 \x00 \x01 \xb3 \x01 \x00 \x01 " )
13
+
14
+ # Act
15
+ with Image .open (b ) as im :
16
+ # Assert
17
+ assert im .format == "MPEG"
18
+
19
+ assert im .mode == "RGB"
20
+ assert im .size == (16 , 1 )
21
+
22
+
23
+ def test_invalid_file () -> None :
24
+ # Arrange
25
+ invalid_file = "Tests/images/flower.jpg"
26
+
27
+ # Act / Assert
28
+ with pytest .raises (SyntaxError ):
29
+ MpegImagePlugin .MpegImageFile (invalid_file )
30
+
31
+
32
+ def test_load () -> None :
33
+ # Arrange
34
+ b = BytesIO (b"\x00 \x00 \x01 \xb3 \x01 \x00 \x01 " )
35
+
36
+ with Image .open (b ) as im :
37
+ # Act / Assert: cannot load
38
+ with pytest .raises (OSError ):
39
+ im .load ()
Original file line number Diff line number Diff line change @@ -53,6 +53,10 @@ def read(self, bits: int) -> int:
53
53
return v
54
54
55
55
56
+ def _accept (prefix : bytes ) -> bool :
57
+ return prefix [:4 ] == b"\x00 \x00 \x01 \xb3 "
58
+
59
+
56
60
##
57
61
# Image plugin for MPEG streams. This plugin can identify a stream,
58
62
# but it cannot read it.
@@ -77,7 +81,7 @@ def _open(self) -> None:
77
81
# --------------------------------------------------------------------
78
82
# Registry stuff
79
83
80
- Image .register_open (MpegImageFile .format , MpegImageFile )
84
+ Image .register_open (MpegImageFile .format , MpegImageFile , _accept )
81
85
82
86
Image .register_extensions (MpegImageFile .format , [".mpg" , ".mpeg" ])
83
87
You can’t perform that action at this time.
0 commit comments