Skip to content

Instantly share code, notes, and snippets.

@kenjiSpecial
Last active March 12, 2019 05:35
Show Gist options
  • Save kenjiSpecial/03dd66a7d0507fd0d33c0520403393d7 to your computer and use it in GitHub Desktop.
Save kenjiSpecial/03dd66a7d0507fd0d33c0520403393d7 to your computer and use it in GitHub Desktop.
画像リサイズ用のpython 記事URL: https://qiita.com/kenjiSpecial@github/items/275fe416c1973f6d6931
from pathlib import Path
from PIL import Image
import json
import unicodedata
Path("./output").mkdir(parents=True, exist_ok=True)
p = Path("./original/")
t = list(p.glob("*"))
# {id: dateStr, imgName: str, tinyImageName: str, title; str, caption: str }
data = []
for i in range(len(t)):
data.append({})
cnt = 0
image_size = 1400
for imgUrl in t:
img = Image.open(imgUrl)
imgWid, imgHig = img.size
print(imgUrl, imgWid, imgHig)
editWid, editHig = 0, 0
if imgHig > imgWid:
editHig = image_size
editWid = int(imgWid/imgHig * editHig)
else:
editWid = image_size
editHig = int(imgHig/imgWid * editWid)
imgName = str(imgUrl.absolute()) # .split('\')
imgNameArr = imgName.split("\\")
imgUrl = imgNameArr[len(imgNameArr) - 1]
nameArr = imgUrl.split('・')
imgIndex = int(nameArr[0]) - 1
date = nameArr[len(nameArr) - 1].split('.')[0]
year = int(date[0:4])
month = int(date[4:6])
day = int(date[6:8])
img_resize = img.resize((editWid, editHig))
img_url = "./output/" + nameArr[1]
img_resize.save(img_url)
img_tiny_resize = img.resize((16, 16))
img_tiny_url = "./output/" + date + "-tiny.jpg"
img_tiny_resize.save(img_tiny_url)
data[imgIndex] = {"id": date, "imgName": nameArr[1],
"tinyImgName": date + "-tiny.jpg", "title": unicodedata.normalize("NFC", "タイトル"), "caption": unicodedata.normalize("NFC", "キャプション")}
url = "output\\data.json"
outputData = json.dumps(data, ensure_ascii=False, indent=4)
with open(url, 'w', encoding='utf-8') as outfile:
outfile.write(outputData)
# import glob
# import os
# print(glob.glob("C:\\Users\\kenji\\personal-project\\python\\image-resize\\original\\1968メキシコ\\*"))
from pathlib import Path
from PIL import Image
import json
import unicodedata
image_size = 1400
folderNames = ["1964_tyo", "1968_mex", "1972_muc",
"1976_ymq", "1980_mow", "1984_lax", "1988_sel", "1992_bcn", "1996_atl", "2000_syd", "2004_ath", "2008_pek", "2012_lon", "2016_rio"]
for folderName in folderNames:
data = {}
data['album'] = []
folderParts = folderName.split('_')
p = Path("./original/" + folderName + "/")
t = list(p.glob("*"))
cnt = 0
for imgUrl in t:
img = Image.open(imgUrl)
imgWid, imgHeight = img.size
editWid, editHig = 0, 0
if imgHeight > imgWid:
editHig = image_size
editWid = int(imgWid/imgHeight * editHig)
else:
editWid = image_size
editHig = int(imgHeight/imgWid * editWid)
imgName = folderParts[0] + "_" + folderParts[1] + "_" + ("%02d" % cnt)
outImgUrl = "./output/" + folderParts[0] + "/" + imgName + ".jpg"
Path(
"./output/" + folderParts[0]).mkdir(parents=True, exist_ok=True)
print(img)
img_resize = img.resize((editWid, editHig))
img_resize.save(outImgUrl)
imgFiles = str(imgUrl.absolute()).split('\\')
baseImageName = imgFiles[len(imgFiles) - 1].split("(")[0]
data['album'].append({
"key": imgName,
"caption": unicodedata.normalize("NFC", baseImageName)
})
cnt = cnt + 1
url = "output\\" + str(folderParts[0]) + "\\data.json"
# url = "output\\data.js
# n"
outputData = json.dumps(data, ensure_ascii=False, indent=4)
with open(url, 'w', encoding='utf-8') as outfile:
# json.dump(data, outfile, ensure_ascii=False, encoding='utf8', indent=2)
outfile.write(outputData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment