downloading images with clean filenames and resizing:jpg,jpeg,png

populat_orgs
Castro0o 5 years ago
parent 9411671332
commit b441df8ba0

@ -1,8 +1,9 @@
import os import os
from mwclient import Site from mwclient import Site
from pprint import pprint from pprint import pprint
from functions import update_json
from PIL import Image from PIL import Image
from functions import update_json, remove_nonwords
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
@ -28,7 +29,7 @@ for img in site.allimages():
# important img info to dictionary # important img info to dictionary
img_dict = { img_dict = {
'name': img.name, 'name': img.name,
'filename': img.page_title, 'filename': remove_nonwords(img.page_title),
'timestamp': img.imageinfo['timestamp'], 'timestamp': img.imageinfo['timestamp'],
'url': img.imageinfo['url'], 'url': img.imageinfo['url'],
'urldesc': img.imageinfo['descriptionshorturl'], 'urldesc': img.imageinfo['descriptionshorturl'],
@ -37,6 +38,7 @@ for img in site.allimages():
# location of image storage # location of image storage
img_fn = os.path.join(imgdir, img_dict['filename']) img_fn = os.path.join(imgdir, img_dict['filename'])
print(img_fn)
# function updates images.json and returns whether the img needs to be downloaded or not # function updates images.json and returns whether the img needs to be downloaded or not
download = update_json(imgsjson_fn, img_dict, img_fn) download = update_json(imgsjson_fn, img_dict, img_fn)
@ -47,16 +49,18 @@ for img in site.allimages():
img.download(destination=img_file) img.download(destination=img_file)
# resize image # resize image
pilimg = Image.open(img_fn) fn, ext = os.path.splitext(img_fn)
pilimg_dim = list(pilimg._size) if ext.lower() in ['.jpg', '.jpeg', '.gif', '.png']: # only img format
pilimg_dim_sort = sorted(pilimg_dim) # smallest dimension 1st pilimg = Image.open(img_fn)
img_ratio = pilimg_dim_sort[0] / pilimg_dim_sort[1] pilimg_dim = list(pilimg._size)
if pilimg_dim == pilimg_dim_sort: pilimg_dim_sort = sorted(pilimg_dim) # smallest dimension 1st
# if height was largest img_ratio = pilimg_dim_sort[0] / pilimg_dim_sort[1]
new_dim = [(thumbnail_size * img_ratio), thumbnail_size] if pilimg_dim == pilimg_dim_sort:
else: # if height was largest
# if with was largest new_dim = [(thumbnail_size * img_ratio), thumbnail_size]
new_dim = [thumbnail_size,(thumbnail_size * img_ratio)] else:
pilimg.thumbnail(new_dim) # if with was largest
pilimg.save(img_fn) new_dim = [thumbnail_size,(thumbnail_size * img_ratio)]
pilimg.thumbnail(new_dim)
pilimg.save(img_fn)
print('\n') print('\n')

Loading…
Cancel
Save