33
44from __future__ import annotations
55
6- import io
76import pathlib
7+ from typing import IO
88
9- import PIL
9+ from PIL . Image import open as pilopen
1010
1111from zimscraperlib .constants import ALPHA_NOT_SUPPORTED
1212from zimscraperlib .image .probing import format_for
1515
1616
1717def 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 ] ,
2020 ** params : str ,
2121) -> None :
2222 """convert an image file from one format to another
@@ -29,12 +29,10 @@ def convert_image(
2929 to RGB. ex: RGB, ARGB, CMYK (and other PIL colorspaces)"""
3030
3131 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
3533 if not fmt :
3634 fmt = format_for (dst )
37- with PIL . Image . open (src ) as image : # pyright: ignore
35+ with pilopen (src ) as image :
3836 if image .mode == "RGBA" and fmt in ALPHA_NOT_SUPPORTED or colorspace :
3937 image = image .convert (colorspace or "RGB" ) # noqa: PLW2901
4038 save_image (image , dst , fmt , ** params )
@@ -45,13 +43,13 @@ def create_favicon(src: pathlib.Path, dst: pathlib.Path) -> None:
4543 if dst .suffix != ".ico" :
4644 raise ValueError ("favicon extension must be ICO" )
4745
48- img = PIL . Image . open (src ) # pyright: ignore
46+ img = pilopen (src )
4947 w , h = img .size
5048 # resize image to square first
5149 if w != h :
5250 size = min ([w , h ])
5351 resized = dst .parent .joinpath (f"{ src .stem } .tmp.{ src .suffix } " )
5452 resize_image (src , size , size , resized , "contain" )
55- img = PIL . Image . open (resized ) # pyright: ignore
53+ img = pilopen (resized )
5654 # now convert to ICO
5755 save_image (img , dst , "ICO" )
0 commit comments