File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44from .fileutils import FileStream , Tempfile
55from .polyfile import Match , register_parser , Submatch
66
7- from PIL import Image
7+
8+ def _get_pil_image ():
9+ """Lazy import PIL.Image only when needed (for JPEG2000 parsing)."""
10+ from PIL import Image
11+ return Image
812
913
1014@register_parser ("image/jp2" )
1115def parse_jpeg2000 (file_stream : FileStream , parent : Match ):
16+ Image = _get_pil_image ()
1217 with Tempfile (file_stream .read (parent .length )) as input_bytes :
1318 img = Image .open (input_bytes )
1419 with BytesIO () as img_data :
Original file line number Diff line number Diff line change 11import base64
22from io import BytesIO
3-
4- from PIL import Image , ImageDraw
3+ from typing import TYPE_CHECKING
54
65from .polyfile import register_parser , InvalidMatch , Submatch
76
7+ if TYPE_CHECKING :
8+ from PIL import Image as PILImage
9+
10+
11+ def _get_pil ():
12+ """Lazy import PIL only when needed (for NES ROM CHR bank rendering)."""
13+ from PIL import Image , ImageDraw
14+ return Image , ImageDraw
15+
816
917def parse_ines_header (header , parent = None ):
1018 magic = header [:4 ]
@@ -130,7 +138,8 @@ def chr_values(chr_bytes: bytes):
130138 ((((chr_bytes [offset + y + 8 ] >> shift ) & 0b1 )) << 1 ) | ((chr_bytes [offset + y ] >> shift ) & 0b1 )
131139
132140
133- def render_chr (chr_bytes : bytes ) -> Image :
141+ def render_chr (chr_bytes : bytes ) -> "PILImage" :
142+ Image , ImageDraw = _get_pil ()
134143 img = Image .new (mode = 'L' , size = (8 * 16 , 8 * 32 ))
135144 d = ImageDraw .Draw (img )
136145 for x , y , pixel in chr_values (chr_bytes ):
You can’t perform that action at this time.
0 commit comments