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 | |
Michael Bestas | cf31e1b | 2022-04-10 18:59:55 +0300 | [diff] [blame^] | 9 | resources = ["res_1080p/drawable-nodpi", |
| 10 | "res_1440p/drawable-nodpi"] |
Arian | e1e2981 | 2020-03-04 19:26:20 +0100 | [diff] [blame] | 11 | |
| 12 | def generate_smallvariants(resource): |
| 13 | global path |
| 14 | wallpapers_path = os.path.join(path, resource) |
| 15 | clean(wallpapers_path) |
| 16 | wallpapers = os.listdir(wallpapers_path) |
| 17 | |
| 18 | for wallpaper in wallpapers: |
| 19 | # Test if the wallpaper is a valid jpeg file |
| 20 | if imghdr.what(os.path.join(wallpapers_path, wallpaper)) != "jpeg": |
| 21 | print(os.path.join(resource, wallpaper) + " is not a valid jpeg file") |
| 22 | continue |
| 23 | |
| 24 | # Append _small.jpg to the wallpaper |
| 25 | wallpaper_small = os.path.splitext(wallpaper)[0] + "_small.jpg" |
| 26 | wallpaper_small_path = os.path.join(wallpapers_path, wallpaper_small) |
| 27 | |
| 28 | # Save the wallpaper with 1/4 size to wallpaper_small_path |
| 29 | with Image.open(os.path.join(wallpapers_path, wallpaper)) as img: |
| 30 | size = int(img.width / 4), int(img.height / 4) |
| 31 | |
| 32 | img_small = img.resize(size, Image.ANTIALIAS) |
| 33 | img_small.save(wallpaper_small_path, "JPEG") |
| 34 | |
| 35 | def clean(wallpapers_path): |
| 36 | wallpapers = os.listdir(wallpapers_path) |
| 37 | |
| 38 | for wallpaper in wallpapers: |
| 39 | # Get rid of existing small variants |
| 40 | if wallpaper.endswith("_small.jpg"): |
| 41 | os.remove(os.path.join(wallpapers_path, wallpaper)) |
| 42 | |
| 43 | for resource in resources: |
| 44 | generate_smallvariants(resource) |