0x00
想把 ins 小姐姐的图片生成照片墙?
0x01
爬虫?或者手动一张张下载,或者 chrome 插件批量下载。
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
|
import os from PIL import Image import logging import math from uuid import uuid4
logger = logging.getLogger() logger.setLevel(logging.DEBUG) handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) logger.addHandler(handler)
def main(path): img_list = os.listdir(path) count = len(img_list) logger.info(F'共有 {count} 张图片') eachSize = 64 eachLine = round(math.sqrt(count)) logger.info(F'单个图像边长 {eachSize} 像素,一行 {eachLine} 个照片,最终图像边长 {eachSize * eachLine}') toImage = Image.new('RGBA', (eachSize * eachLine, eachSize * eachLine)) y = 0 for x, i in enumerate(img_list): try: img = Image.open(os.path.join(path, i)) except IOError: logger.info(F"Error: 没有找到文件或读取文件失败 {os.path.join(path,i)}") else: img = img.resize((eachSize, eachSize), Image.ANTIALIAS) toImage.paste(img, ((x % eachLine) * eachSize, y * eachSize)) if (x + 1) % eachLine == 0: y += 1 logger.info("照片墙生成成功,正在保存..") filename = os.path.join(os.path.dirname(path), F'{uuid4().hex}.png') logger.debug(F'照片墙文件名 {filename}') toImage.save(filename)
if __name__ == "__main__": img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'photos') logger.info(F'原始照片文件夹路径 {img_path}') main(img_path)
|
1 2
| toImage = Image.new('RGBA', (eachSize*eachLine+10*eachLine,eachSize*eachLine+10*eachLine)) toImage.paste(img, (x * (eachSize+10), y * (eachSize+10)))
|
效果图就不贴了。
0x02
参考资料:
itchat+pillow实现微信好友头像爬取和拼接
Pillow v2.4.0 (PIL fork)
photos by la_lavenda (哎,😩发现小姐姐的照片都删了) 侵删
封面出处:http://simpledesktops.com/browse/desktops/2017/feb/27/arrow/