-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_image_processor.py
96 lines (81 loc) · 3.58 KB
/
web_image_processor.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Here are a few functions that may come handy when preparing imagery for a new website
# You will have to install PIL using sudo pip3 install Pillow
from xml.dom.minidom import parseString
from PIL import Image
from pathlib import Path
import os, sys
path = "src/images/partners/"
dirs = os.listdir(path)
pic = "vidifx-icon.png"
print(dirs)
def resize_all_square_imgs():
for item in dirs:
if os.path.isfile(path + item):
if item.endswith(".png"):
im = Image.open(path + item)
# f, e = os.path.splitext(path + item)
# imResize = im.resize((24, 24), Image.ANTIALIAS)
# imResize.save(f + "/24resized.png", "PNG", quality=100)
# imResize = im.resize((36, 36), Image.ANTIALIAS)
# imResize.save(f + "/36resized.png", "PNG", quality=100)
im.thumbnail((180, 180), Image.ANTIALIAS)
# im = im.resize((180, 180), Image.ANTIALIAS)
# im.save(target_path + item + ".png", "PNG", quality=100)
im.save(path + item, "PNG", quality=100)
print(path + item)
def resize_all_based_on_width():
basewidth = 200
for item in dirs:
if os.path.isfile(path + item):
if item.endswith(".png"):
im = Image.open(path + item)
wpercent = basewidth / float(im.size[0])
hsize = int((float(im.size[1]) * float(wpercent)))
im = im.resize((basewidth, hsize), Image.ANTIALIAS)
im.save(path + item, "PNG", quality=100)
print(path + item)
def resize_all_based_on_height():
baseheight = 50
for item in dirs:
if os.path.isfile(path + item):
if item.endswith(".png"):
im = Image.open(path + item)
hpercent = baseheight / float(im.size[1])
width = int((float(im.size[0]) * float(hpercent)))
im = im.resize((width, baseheight), Image.ANTIALIAS)
im.save(path + item, "PNG", quality=100)
print(path + item)
def convert_to_webp(path):
# Convert all images in a folder to webp
print("starting...")
paths = Path(path).glob("*.png")
for path in paths:
webp_path = path.with_suffix(".webp")
image = Image.open(path)
image.save(webp_path, format="webp")
print(path)
def generate_icons(pic):
im = Image.open(pic)
# Just create 2 new lines per image size required
imResize = im.resize((16, 16), Image.ANTIALIAS)
imResize.save("16x16favicon.ico", "ICO", quality=100)
imResize = im.resize((32, 32), Image.ANTIALIAS)
imResize.save("favicon.ico", "ICO", quality=100)
imResize = im.resize((48, 48), Image.ANTIALIAS)
imResize.save("icon-48x48.png", "PNG", quality=100)
imResize = im.resize((72, 72), Image.ANTIALIAS)
imResize.save("icon-72x72.png", "PNG", quality=100)
imResize = im.resize((96, 96), Image.ANTIALIAS)
imResize.save("icon-96x96.png", "PNG", quality=100)
imResize = im.resize((144, 144), Image.ANTIALIAS)
imResize.save("icon-144x144.png", "PNG", quality=100)
imResize = im.resize((192, 192), Image.ANTIALIAS)
imResize.save("logo192.png", "PNG", quality=100)
imResize = im.resize((256, 256), Image.ANTIALIAS)
imResize.save("icon-256x256.png", "PNG", quality=100)
imResize = im.resize((384, 384), Image.ANTIALIAS)
imResize.save("icon-384x384.png", "PNG", quality=100)
imResize = im.resize((512, 512), Image.ANTIALIAS)
imResize.save("logo512.png", "PNG", quality=100)
# generate_icons(pic)
resize_all_based_on_height()