resizing Images after downloaded with PIL #5

Merged
acastro merged 1 commits from resizeimgs into master 5 years ago

@ -14,6 +14,8 @@
* [jinja2](https://jinja.palletsprojects.com/en/2.11.x/) Python library * [jinja2](https://jinja.palletsprojects.com/en/2.11.x/) Python library
* Install: * Install:
* `pip3 install jinja2` * `pip3 install jinja2`
* [Pillow](https://pillow.readthedocs.io/en/stable/) Python library for image processing
* `pip3 install Pillow`
* [pandoc](https://pandoc.org/) * [pandoc](https://pandoc.org/)
* Install: * Install:
* Debian/Ubuntu: `sudo apt install pandoc` * Debian/Ubuntu: `sudo apt install pandoc`

@ -2,6 +2,7 @@ import os
from mwclient import Site from mwclient import Site
from pprint import pprint from pprint import pprint
from functions import update_json from functions import update_json
from PIL import Image
site = Site(host='hub.xpub.nl/sandbox', path='/itchwiki/') site = Site(host='hub.xpub.nl/sandbox', path='/itchwiki/')
wd = os.path.dirname(os.path.abspath(__file__)) # working directory wd = os.path.dirname(os.path.abspath(__file__)) # working directory
@ -11,6 +12,7 @@ os.makedirs(imgdir, exist_ok=True) # create images/ dir
imgsjson_fn = os.path.join(wd, 'images.json') imgsjson_fn = os.path.join(wd, 'images.json')
thumbnail_size = 1200 # largest px dimension of img thumbnails
with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd
@ -43,4 +45,18 @@ for img in site.allimages():
print('DOWNLOADING:', img_fn) print('DOWNLOADING:', img_fn)
with open(img_fn, 'wb') as img_file: with open(img_fn, 'wb') as img_file:
img.download(destination=img_file) img.download(destination=img_file)
# resize image
pilimg = Image.open(img_fn)
pilimg_dim = list(pilimg._size)
pilimg_dim_sort = sorted(pilimg_dim) # smallest dimension 1st
img_ratio = pilimg_dim_sort[0] / pilimg_dim_sort[1]
if pilimg_dim == pilimg_dim_sort:
# if height was largest
new_dim = [(thumbnail_size * img_ratio), thumbnail_size]
else:
# if with was largest
new_dim = [thumbnail_size,(thumbnail_size * img_ratio)]
pilimg.thumbnail(new_dim)
pilimg.save(img_fn)
print('\n') print('\n')

Loading…
Cancel
Save