blob: e096ce25625d7be833250a43f909a449494427b3 [file] [log] [blame]
Ariane1e29812020-03-04 19:26:20 +01001#!/usr/bin/env python3
2import imghdr
3import os
4
5from PIL import Image
6
7path = os.path.dirname(os.path.realpath(__file__))
8
Michael Bestascf31e1b2022-04-10 18:59:55 +03009resources = ["res_1080p/drawable-nodpi",
10 "res_1440p/drawable-nodpi"]
Ariane1e29812020-03-04 19:26:20 +010011
12def 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
35def 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
43for resource in resources:
44 generate_smallvariants(resource)