File: android-resize-images.py

package info (click to toggle)
wand 0.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • sloc: python: 7,455; makefile: 124
file content (38 lines) | stat: -rw-r--r-- 1,042 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python

# Python MagickWand bindings can be obtained
# from https://github.com/dahlia/wand
from wand.image import Image

import os
import re

# Android - Supporting multiple screens
# http://developer.android.com/guide/practices/screens_support.html

MANIFEST = (
    # dpi, ratio
    ('h', .75),  # high-dpi
    ('m', .5),  # medium-dpi
    ('l', .375)  # low-dpi
)

if __name__ == "__main__":
    # We start from here (2.0x size images)
    files = os.listdir("res/drawable-xhdpi/")

    # Filter out non-desired files
    files = filter(lambda x: re.match(r"^\w+(.jpg|.png)$", x) is not None,
                   files)

    for filename in files:
        with Image(filename="res/drawable-xhdpi/%s" % filename) as img:

            print("Resizing %s..." % filename)

            width, height = img.size

            for dpi, ratio in MANIFEST:
                cimg = img.clone()
                cimg.resize(int(width*ratio), int(height*ratio))
                cimg.save(filename="res/drawable-%sdpi/%s" % (dpi, filename))