-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy paththumbnail_converter.py
39 lines (29 loc) · 1.15 KB
/
thumbnail_converter.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
# Code Listing #1
"""
Thumbnail converter using URLs
"""
import threading
import urllib.request
# Install PILL via Pillow
from PIL import Image
def thumbnail_image(url, size=(64, 64), format='.png'):
""" Convert image to a specific format """
im = Image.open(urllib.request.urlopen(url))
# filename is last part of URL minus extension + '.format'
pieces = url.split('/')
filename = ''.join((pieces[-2],'_',pieces[-1].split('.')[0],'_thumb',format))
im.thumbnail(size, Image.ANTIALIAS)
im.save(filename)
print('Saved',filename)
if __name__ == "__main__":
img_urls = ['https://dummyimage.com/256x256/000/fff.jpg',
'https://dummyimage.com/320x240/fff/00.jpg',
'https://dummyimage.com/640x480/ccc/aaa.jpg',
'https://dummyimage.com/128x128/ddd/eee.jpg',
'https://dummyimage.com/720x720/111/222.jpg']
for url in img_urls:
# For making the program serial, comment out the next two
# lines and uncomment the last line.
t = threading.Thread(target=thumbnail_image,args=(url,))
t.start()
# thumbnail_image(url)