Arian | e1e2981 | 2020-03-04 19:26:20 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | import imghdr |
| 3 | import os |
| 4 | |
| 5 | from PIL import Image |
| 6 | |
| 7 | path = os.path.dirname(os.path.realpath(__file__)) |
| 8 | |
| 9 | resources = ["res_1080p/common/drawable-nodpi", |
| 10 | "res_1080p/full/drawable-nodpi", |
| 11 | "res_1440p/common/drawable-nodpi", |
| 12 | "res_1440p/full/drawable-nodpi"] |
| 13 | |
| 14 | def generate_smallvariants(resource): |
| 15 | global path |
| 16 | wallpapers_path = os.path.join(path, resource) |
| 17 | clean(wallpapers_path) |
| 18 | wallpapers = os.listdir(wallpapers_path) |
| 19 | |
| 20 | for wallpaper in wallpapers: |
| 21 | # Test if the wallpaper is a valid jpeg file |
| 22 | if imghdr.what(os.path.join(wallpapers_path, wallpaper)) != "jpeg": |
| 23 | print(os.path.join(resource, wallpaper) + " is not a valid jpeg file") |
| 24 | continue |
| 25 | |
| 26 | # Append _small.jpg to the wallpaper |
| 27 | wallpaper_small = os.path.splitext(wallpaper)[0] + "_small.jpg" |
| 28 | wallpaper_small_path = os.path.join(wallpapers_path, wallpaper_small) |
| 29 | |
| 30 | # Save the wallpaper with 1/4 size to wallpaper_small_path |
| 31 | with Image.open(os.path.join(wallpapers_path, wallpaper)) as img: |
| 32 | size = int(img.width / 4), int(img.height / 4) |
| 33 | |
| 34 | img_small = img.resize(size, Image.ANTIALIAS) |
| 35 | img_small.save(wallpaper_small_path, "JPEG") |
| 36 | |
| 37 | def clean(wallpapers_path): |
| 38 | wallpapers = os.listdir(wallpapers_path) |
| 39 | |
| 40 | for wallpaper in wallpapers: |
| 41 | # Get rid of existing small variants |
| 42 | if wallpaper.endswith("_small.jpg"): |
| 43 | os.remove(os.path.join(wallpapers_path, wallpaper)) |
| 44 | |
| 45 | for resource in resources: |
| 46 | generate_smallvariants(resource) |