Skip to content

Commit a955ab0

Browse files
committed
Rename zimscraperlib.image.convertion to zimscraperlib.image.conversion to fix typo and fix type hints
1 parent 4c90482 commit a955ab0

File tree

6 files changed

+25
-20
lines changed

6 files changed

+25
-20
lines changed

Diff for: CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Automatically index PDF documents content #167
1616
- Automatically set proper title on PDF documents #168
1717

18+
## Changed
19+
- **BREAKING** Renamed `zimscraperlib.image.convertion` to `zimscraperlib.image.conversion` to fix typo
20+
- **BREAKING** Many changes in type hints to match the real underlying code
21+
- **BREAKING** Force all boolean arguments to be keyword-only in function calls for clarity / disambiguation (see ruff rule FBT002)
22+
- Prefer to use `IO[bytes]` to `io.BytesIO` when possible since it is more generic
23+
1824
### Fixed
1925

2026
- Metadata length validation is buggy for unicode strings #158

Diff for: src/zimscraperlib/image/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# vim: ai ts=4 sts=4 et sw=4 nu
44

55
# flake8: noqa
6-
from .convertion import convert_image
6+
from .conversion import convert_image
77
from .optimization import optimize_image
88
from .probing import is_valid_image
99
from .transformation import resize_image

Diff for: src/zimscraperlib/image/convertion.py renamed to src/zimscraperlib/image/conversion.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
from __future__ import annotations
55

6-
import io
76
import pathlib
7+
from typing import IO
88

9-
import PIL
9+
from PIL.Image import open as pilopen
1010

1111
from zimscraperlib.constants import ALPHA_NOT_SUPPORTED
1212
from zimscraperlib.image.probing import format_for
@@ -15,8 +15,8 @@
1515

1616

1717
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],
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")

Diff for: src/zimscraperlib/image/optimization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from optimize_images.img_dynamic_quality import jpeg_dynamic_quality
3636
from PIL import Image
3737

38-
from zimscraperlib.image.convertion import convert_image
38+
from zimscraperlib.image.conversion import convert_image
3939
from zimscraperlib.image.probing import format_for
4040
from zimscraperlib.image.utils import save_image
4141

Diff for: src/zimscraperlib/image/utils.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
# vim: ai ts=4 sts=4 et sw=4 nu
33
from __future__ import annotations
44

5-
import io
65
import pathlib
6+
from typing import IO
77

8-
from PIL import Image
8+
from PIL.Image import Image
9+
from PIL.ImageFile import ImageFile
910

1011

1112
def save_image(
12-
src: Image, # pyright: ignore
13-
dst: pathlib.Path | io.BytesIO,
14-
fmt: str | None = None,
13+
src: Image | ImageFile,
14+
dst: pathlib.Path | IO[bytes],
15+
fmt: str,
1516
**params: str,
1617
) -> None:
1718
"""PIL.Image.save() wrapper setting default parameters"""
18-
args = {"JPEG": {"quality": 100}, "PNG": {}}.get(fmt, {}) # pyright: ignore
19+
args = {"JPEG": {"quality": 100}, "PNG": {}}.get(fmt, {})
1920
args.update(params or {})
20-
src.save(dst, fmt, **args) # pyright: ignore
21+
src.save(dst, fmt, **args)

Diff for: tests/image/test_image.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from resizeimage.imageexceptions import ImageSizeError
1616

1717
from zimscraperlib.image import presets
18-
from zimscraperlib.image.convertion import convert_image, create_favicon
18+
from zimscraperlib.image.conversion import convert_image, create_favicon
1919
from zimscraperlib.image.optimization import (
2020
ensure_matches,
2121
optimize_gif,

0 commit comments

Comments
 (0)