Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pictures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import math
from fractions import Fraction
from pathlib import Path
from types import NotImplementedType

from django.core import checks
from django.core.files.base import ContentFile
Expand Down Expand Up @@ -105,7 +106,7 @@ def name(self) -> str:
def path(self) -> Path:
return Path(self.storage.path(self.name))

def process(self, image) -> Image:
def process(self, image) -> Image.Image:
image = ImageOps.exif_transpose(image) # crates a copy
height = self.height or self.width / Fraction(*image.size)
size = math.floor(self.width), math.floor(height)
Expand All @@ -130,7 +131,7 @@ def delete(self):


class PictureFieldFile(ImageFieldFile):
def __xor__(self, other) -> tuple[set[Picture], set[Picture]]:
def __xor__(self, other) -> tuple[set[Picture], set[Picture]] | NotImplementedType:
"""Return the new and obsolete :class:`Picture` instances."""
if not isinstance(other, PictureFieldFile):
return NotImplemented
Expand Down Expand Up @@ -192,7 +193,7 @@ def height(self):
return self._get_image_dimensions()[1]

@property
def aspect_ratios(self) -> {Fraction | None: {str: {int: Picture}}}:
def aspect_ratios(self) -> dict[Fraction | None, dict[str, dict[int, Picture]]]:
self._require_file()
return self.get_picture_files(
file_name=self.name,
Expand All @@ -210,7 +211,7 @@ def get_picture_files(
img_height: int,
storage: Storage,
field: PictureField,
) -> {Fraction | None: {str: {int: Picture}}}:
) -> dict[Fraction | None, dict[str, dict[int, Picture]]]:
PictureClass = import_string(conf.get_settings().PICTURE_CLASS)
return {
ratio: {
Expand Down
6 changes: 3 additions & 3 deletions pictures/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def sizes(


def source_set(
size: (int, int), *, ratio: str | Fraction | None, max_width: int, cols: int
size: tuple[int, int], *, ratio: str | Fraction | None, max_width: int, cols: int
) -> set:
ratio = Fraction(ratio) if ratio else None
img_width, img_height = size
Expand All @@ -82,8 +82,8 @@ def placeholder(width: int, height: int, alt):
hue = random.randint(0, 360) # NoQA S311
img = Image.new("RGB", (width, height), color=f"hsl({hue}, 40%, 80%)")
draw = ImageDraw.Draw(img)
draw.line(((0, 0, width, height)), width=3, fill=f"hsl({hue}, 60%, 20%)")
draw.line(((0, height, width, 0)), width=3, fill=f"hsl({hue}, 60%, 20%)")
draw.line((0, 0, width, height), width=3, fill=f"hsl({hue}, 60%, 20%)")
draw.line((0, height, width, 0), width=3, fill=f"hsl({hue}, 60%, 20%)")
draw.rectangle(
(width / 4, height / 4, width * 3 / 4, height * 3 / 4),
fill=f"hsl({hue}, 40%, 80%)",
Expand Down
8 changes: 4 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ def test_placeholder():
assert img.height == 1200


class TestPicture(Picture):
class SamplePicture(Picture):
@property
def url(self):
return f"/media/{self.parent_name}"


def test_reconstruct(image_upload_file):
picture = TestPicture(
picture = SamplePicture(
image_upload_file.name,
"WEBP",
"16/9",
Expand All @@ -164,7 +164,7 @@ def test_reconstruct(image_upload_file):
assert isinstance(reconstructed, Storage)

assert utils.reconstruct(
"tests.test_utils.TestPicture",
"tests.test_utils.SamplePicture",
[],
{
"parent_name": "test.jpg",
Expand All @@ -173,7 +173,7 @@ def test_reconstruct(image_upload_file):
"storage": default_storage,
"width": 100,
},
) == TestPicture(
) == SamplePicture(
"test.jpg",
"JPEG",
"16/9",
Expand Down