ci: Add image dimension check (#51)

This commit is contained in:
Jonah Lawrence 2022-06-01 12:58:26 -06:00 committed by GitHub
parent a3a49ae8e3
commit 730e6bf508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 133 additions and 0 deletions

88
.github/scripts/check_wallpapers.py vendored Normal file
View File

@ -0,0 +1,88 @@
import os
import sys
from argparse import ArgumentParser
from PIL import Image
class ImageFile:
def __init__(self, filename: str, size: tuple[int, int]):
self.filename = filename
self.width = size[0]
self.height = size[1]
def list_images(images: list[ImageFile]):
"""Displays a list of images with sizes and filenames"""
print(*[f"{f.width:<4} x {f.height:<4} : {f.filename}" for f in images], sep="\n")
def list_small_images(
images: list[ImageFile], *, min_width: int, min_height: int
) -> int:
"""Lists images that are smaller than min_width x min_height and returns the number of images"""
# sort by number of pixels
images.sort(key=lambda x: x.width * x.height)
# list images if the width or height is less than min_width or min_height
filtered = [f for f in images if f.width < min_width or f.height < min_height]
if len(filtered) > 0:
print(f"\n{len(filtered)} images are smaller than {min_width} x {min_height}")
list_images(filtered)
return len(filtered)
def list_narrow_images(images: list[ImageFile], *, max_ratio: float) -> int:
"""Lists images having width/height less than max_ratio and returns the number of images"""
# sort by width to height ratio
images.sort(key=lambda x: x.width / x.height)
# list images where the width/height ratio is less than max_ratio
filtered = [f for f in images if f.width / f.height < max_ratio]
if len(filtered) > 0:
print(f"\n{len(filtered)} images have width/height less than {max_ratio}")
list_images(filtered)
return len(filtered)
def filter_wallpapers(dir: str, min_width: int, min_height: int, max_ratio: float):
"""Filters wallpapers in the current directory."""
dirpath = os.path.abspath(dir)
images = [
ImageFile(f, Image.open(os.path.join(dirpath, f)).size)
for f in os.listdir(dirpath)
]
small = list_small_images(images, min_width=min_width, min_height=min_height)
narrow = list_narrow_images(images, max_ratio=max_ratio)
if small + narrow > 0:
sys.exit(f"Found {small} small images and {narrow} narrow images.")
if __name__ == "__main__":
# read image directory path, min_width, min_height, max_ratio from passed arguments
parser = ArgumentParser(description="Filter wallpapers in the current directory.")
parser.add_argument(
"--dir",
"-d",
type=str,
default=".",
help="Directory to filter wallpapers in. Defaults to current directory.",
)
parser.add_argument(
"--min_width",
type=int,
default=1920,
help="minimum width of images to keep (default: 1920)",
)
parser.add_argument(
"--min_height",
type=int,
default=1080,
help="minimum height of images to keep (default: 1080)",
)
parser.add_argument(
"--max_ratio",
type=float,
default=1.5,
help="maximum width/height ratio of images to keep (default: 1.5)",
)
args = parser.parse_args()
filter_wallpapers(args.dir, args.min_width, args.min_height, args.max_ratio)

45
.github/workflows/check-wallpapers.yml vendored Normal file
View File

@ -0,0 +1,45 @@
name: "Check Wallpaper Dimensions"
on:
push:
branches:
- main
paths:
- "**.jpg"
- "**.jpeg"
- "**.png"
- "**.webp"
pull_request:
branches:
- main
paths:
- "**.jpg"
- "**.jpeg"
- "**.png"
- "**.webp"
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install Pillow
- name: Check Wallpaper Dimensions
run: |
# run check_wallpapers.py and set the exit code
python .github/scripts/check_wallpapers.py -d images
if [ $? -ne 0 ]; then
exit 1
fi