blob: 3e09ed234af71670737d5e29daa265bf26815871 [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
9resources = ["res_1080p/common/drawable-nodpi",
10 "res_1080p/full/drawable-nodpi",
11 "res_1440p/common/drawable-nodpi",
12 "res_1440p/full/drawable-nodpi"]
13
14def 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
37def 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
45for resource in resources:
46 generate_smallvariants(resource)