From 730e6bf5083bfa166d545a90fd6cabf72fd25939 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Wed, 1 Jun 2022 12:58:26 -0600 Subject: [PATCH] ci: Add image dimension check (#51) --- .github/scripts/check_wallpapers.py | 88 ++++++++++++++++++++++++++ .github/workflows/check-wallpapers.yml | 45 +++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 .github/scripts/check_wallpapers.py create mode 100644 .github/workflows/check-wallpapers.yml diff --git a/.github/scripts/check_wallpapers.py b/.github/scripts/check_wallpapers.py new file mode 100644 index 0000000..a9ea7b1 --- /dev/null +++ b/.github/scripts/check_wallpapers.py @@ -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) diff --git a/.github/workflows/check-wallpapers.yml b/.github/workflows/check-wallpapers.yml new file mode 100644 index 0000000..1839ac2 --- /dev/null +++ b/.github/workflows/check-wallpapers.yml @@ -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