Skip to content

Commit

Permalink
Check for matching aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
laggykiller committed Nov 30, 2024
1 parent 83c53fe commit ca2082c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/sticker_convert/job_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class CompOption(BaseOption):
no_compress: Optional[bool] = None
processes: int = ceil(cpu_count() / 2)
animated: Optional[bool] = None
square: Optional[bool] = None

def to_dict(self) -> Dict[Any, Any]:
return {
Expand Down Expand Up @@ -115,7 +114,6 @@ def to_dict(self) -> Dict[Any, Any]:
"no_compress": self.no_compress,
"processes": self.processes,
"animated": self.animated,
"square": self.square,
}

def get_size_max(self) -> Tuple[Optional[int], Optional[int]]:
Expand Down
1 change: 0 additions & 1 deletion src/sticker_convert/uploaders/compress_wastickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.base_spec.size_max_vid = 500000
self.base_spec.duration_min = 8
self.base_spec.duration_max = 10000
self.base_spec.square = True
self.base_spec.set_res(512)
self.base_spec.set_format((".webp",))

Expand Down
1 change: 0 additions & 1 deletion src/sticker_convert/uploaders/upload_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.base_spec.set_size_max(300000)
self.base_spec.set_res_max(512)
self.base_spec.duration_max = 3000
self.base_spec.square = True

self.png_spec = copy.deepcopy(self.base_spec)
self.png_spec.set_format((".apng",))
Expand Down
3 changes: 1 addition & 2 deletions src/sticker_convert/uploaders/upload_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:

self.base_spec.size_max_img = 512000
self.base_spec.size_max_vid = 256000
self.base_spec.square = True
self.base_spec.duration_max = 3000
self.base_spec.set_res(512)

Expand All @@ -48,7 +47,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.opt_comp_merged.merge(self.base_spec)

base_cover_spec = CompOption(
size_max_img=128000, size_max_vid=32000, square=True, duration_max=3000
size_max_img=128000, size_max_vid=32000, duration_max=3000
)
base_cover_spec.set_res(100)

Expand Down
1 change: 0 additions & 1 deletion src/sticker_convert/uploaders/upload_viber.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

self.base_spec.set_size_max(0)
self.base_spec.square = True

self.png_spec = copy.deepcopy(self.base_spec)
self.png_spec.set_res_max(490)
Expand Down
1 change: 0 additions & 1 deletion src/sticker_convert/uploaders/xcode_imessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.base_spec.set_size_max(500000)
self.base_spec.set_res(300)
self.base_spec.set_format(("png", ".apng", ".gif", ".jpeg", "jpg"))
self.base_spec.square = True

self.small_spec = copy.deepcopy(self.base_spec)

Expand Down
29 changes: 23 additions & 6 deletions src/sticker_convert/utils/media/format_verify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import os
from fractions import Fraction
from pathlib import Path
from typing import Optional, Tuple, Union

Expand All @@ -16,9 +17,7 @@ def check_file(file: Union[Path, bytes], spec: CompOption) -> bool:
file_info = CodecInfo(file)

return (
FormatVerify.check_file_res(
file, res=spec.get_res(), square=spec.square, file_info=file_info
)
FormatVerify.check_file_res(file, res=spec.get_res(), file_info=file_info)
and FormatVerify.check_file_fps(
file, fps=spec.get_fps(), file_info=file_info
)
Expand Down Expand Up @@ -48,7 +47,6 @@ def check_file_res(
res: Tuple[
Tuple[Optional[int], Optional[int]], Tuple[Optional[int], Optional[int]]
],
square: Optional[bool] = None,
file_info: Optional[CodecInfo] = None,
) -> bool:
if file_info:
Expand All @@ -65,8 +63,27 @@ def check_file_res(
return False
if res[1][1] and file_height > res[1][1]:
return False
if square and file_height != file_width:
return False

min_ratio = None
if res[0][0] and res[1][0]:
min_ratio = Fraction(res[0][0], res[1][0])

max_ratio = None
if res[0][1] and res[1][1]:
max_ratio = Fraction(res[0][1], res[1][1])

file_ratio = Fraction(file_width, file_height)

if min_ratio is not None and max_ratio is not None:
if min_ratio == max_ratio:
return True if file_ratio == min_ratio else False
else:
return True

if min_ratio is not None:
return True if file_ratio == min_ratio else False
if max_ratio is not None:
return True if file_ratio == max_ratio else False

return True

Expand Down

0 comments on commit ca2082c

Please sign in to comment.