Alexander Martinz | ef99634 | 2023-05-05 10:52:43 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | import os |
| 3 | |
| 4 | from PIL import Image |
| 5 | |
| 6 | path = os.path.dirname(os.path.realpath(__file__)) |
| 7 | |
| 8 | resources = [ |
| 9 | "res_1080p/drawable-nodpi", |
| 10 | # "res_1440p/drawable-nodpi", |
| 11 | ] |
| 12 | |
| 13 | def generate_smallvariants(resource): |
| 14 | global path |
| 15 | wallpapers_path = os.path.join(path, resource) |
| 16 | clean(wallpapers_path) |
| 17 | wallpapers = os.listdir(wallpapers_path) |
| 18 | |
| 19 | for wallpaper in wallpapers: |
| 20 | # Append _small.jpg to the wallpaper |
| 21 | wallpaper_small = os.path.splitext(wallpaper)[0] + "_small.jpg" |
| 22 | wallpaper_small_path = os.path.join(wallpapers_path, wallpaper_small) |
| 23 | |
| 24 | # Save the wallpaper with 1/4 size to wallpaper_small_path |
| 25 | with Image.open(os.path.join(wallpapers_path, wallpaper)) as img: |
| 26 | size = int(img.width / 4), int(img.height / 4) |
| 27 | |
| 28 | img_small = img.resize(size, Image.ANTIALIAS) |
| 29 | img_small.save(wallpaper_small_path, "JPEG") |
| 30 | |
| 31 | def clean(wallpapers_path): |
| 32 | wallpapers = os.listdir(wallpapers_path) |
| 33 | |
| 34 | for wallpaper in wallpapers: |
| 35 | # Get rid of existing small variants |
| 36 | if wallpaper.endswith("_small.jpg"): |
| 37 | os.remove(os.path.join(wallpapers_path, wallpaper)) |
| 38 | |
| 39 | for resource in resources: |
| 40 | generate_smallvariants(resource) |