3
3
4
4
from __future__ import annotations
5
5
6
- import io
7
6
import pathlib
7
+ from typing import IO
8
8
9
- import PIL
9
+ from PIL . Image import open as pilopen
10
10
11
11
from zimscraperlib .constants import ALPHA_NOT_SUPPORTED
12
12
from zimscraperlib .image .probing import format_for
15
15
16
16
17
17
def convert_image (
18
- src : pathlib .Path | io . BytesIO ,
19
- dst : pathlib .Path | io . BytesIO ,
18
+ src : pathlib .Path | IO [ bytes ] ,
19
+ dst : pathlib .Path | IO [ bytes ] ,
20
20
** params : str ,
21
21
) -> None :
22
22
"""convert an image file from one format to another
@@ -29,12 +29,10 @@ def convert_image(
29
29
to RGB. ex: RGB, ARGB, CMYK (and other PIL colorspaces)"""
30
30
31
31
colorspace = params .get ("colorspace" ) # requested colorspace
32
- fmt = (
33
- params .pop ("fmt" ).upper () if "fmt" in params else None # pyright: ignore
34
- ) # requested format
32
+ fmt = params .pop ("fmt" ).upper () if "fmt" in params else None # requested format
35
33
if not fmt :
36
34
fmt = format_for (dst )
37
- with PIL . Image . open (src ) as image : # pyright: ignore
35
+ with pilopen (src ) as image :
38
36
if image .mode == "RGBA" and fmt in ALPHA_NOT_SUPPORTED or colorspace :
39
37
image = image .convert (colorspace or "RGB" ) # noqa: PLW2901
40
38
save_image (image , dst , fmt , ** params )
@@ -45,13 +43,13 @@ def create_favicon(src: pathlib.Path, dst: pathlib.Path) -> None:
45
43
if dst .suffix != ".ico" :
46
44
raise ValueError ("favicon extension must be ICO" )
47
45
48
- img = PIL . Image . open (src ) # pyright: ignore
46
+ img = pilopen (src )
49
47
w , h = img .size
50
48
# resize image to square first
51
49
if w != h :
52
50
size = min ([w , h ])
53
51
resized = dst .parent .joinpath (f"{ src .stem } .tmp.{ src .suffix } " )
54
52
resize_image (src , size , size , resized , "contain" )
55
- img = PIL . Image . open (resized ) # pyright: ignore
53
+ img = pilopen (resized )
56
54
# now convert to ICO
57
55
save_image (img , dst , "ICO" )
0 commit comments