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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| from PIL import Image import os import sys import json from datetime import datetime from ImageProcess import Graphics
SIZE_normal = 1.0 SIZE_small = 1.5 SIZE_more_small = 2.0 SIZE_more_small_small = 3.0
def make_directory(directory): """创建目录""" os.makedirs(directory)
def directory_exists(directory): """判断目录是否存在""" if os.path.exists(directory): return True else: return False
def list_img_file(directory): """列出目录下所有文件,并筛选出图片文件列表返回""" old_list = sorted(os.listdir(directory), reverse=True) print(old_list) new_list = [] for filename in old_list: name, fileformat = filename.split(".") if fileformat.lower() == "jpg" or fileformat.lower() == "png" or fileformat.lower() == "gif" or fileformat.lower() == "jpeg": new_list.append(filename) return new_list
def print_help(): print(""" This program helps compress many image files you can choose which scale you want to compress your img(jpg/png/etc) 1) normal compress(4M to 1M around) 2) small compress(4M to 500K around) 3) smaller compress(4M to 300K around) """)
def compress(choose, des_dir, src_dir, file_list): """压缩算法,img.thumbnail对图片进行压缩, 参数 ----------- choose: str 选择压缩的比例,有4个选项,越大压缩后的图片越小 """ if choose == '1': scale = SIZE_normal if choose == '2': scale = SIZE_small if choose == '3': scale = SIZE_more_small if choose == '4': scale = SIZE_more_small_small for infile in file_list: img = Image.open(src_dir+infile) w, h = img.size img.thumbnail((int(w/scale), int(h/scale))) img.save(des_dir + infile) def compress_photo(): '''调用压缩图片的函数 ''' src_dir, des_dir = "photos/", "min_photos/" if directory_exists(src_dir): if not directory_exists(src_dir): make_directory(src_dir) file_list_src = list_img_file(src_dir) if directory_exists(des_dir): if not directory_exists(des_dir): make_directory(des_dir) file_list_des = list_img_file(des_dir) '''如果已经压缩了,就不再压缩''' for i in range(len(file_list_des)): if file_list_des[i] in file_list_src: file_list_src.remove(file_list_des[i]) compress('4', des_dir, src_dir, file_list_src)
def handle_photo(): '''根据图片的文件名处理成需要的json格式的数据 ----------- 最后将data.json文件存到博客的source/photos文件夹下 ''' src_dir, des_dir = "photos/", "min_photos/" file_list = list_img_file(src_dir) print(file_list) list_info = [] for i in range(len(file_list)): filename = file_list[i] date_str, info = filename.split("_") info, _ = info.split(".") date = datetime.strptime(date_str, "%Y-%m-%d") year_month = date_str[0:7] if i == 0: new_dict = {"date": year_month, "arr":{'year': date.year, 'month': date.month, 'link': [filename], 'text': [info], 'type': ['image'] } } list_info.append(new_dict) elif year_month != list_info[-1]['date']: new_dict = {"date": year_month, "arr":{'year': date.year, 'month': date.month, 'link': [filename], 'text': [info], 'type': ['image'] } } list_info.append(new_dict) else: list_info[-1]['arr']['link'].append(filename) list_info[-1]['arr']['text'].append(info) list_info[-1]['arr']['type'].append('image') list_info.reverse() tmp = bubbleYear(list_info) bubble(tmp) final_dict = {"list": list_info} with open("../hexoBlog/source/photos/data.json","w") as fp: json.dump(final_dict, fp)
def cut_photo(): """裁剪算法 ---------- 调用Graphics类中的裁剪算法,将src_dir目录下的文件进行裁剪(裁剪成正方形) """ src_dir = "photos/" if directory_exists(src_dir): if not directory_exists(src_dir): make_directory(src_dir) file_list = list_img_file(src_dir) if file_list: print_help() for infile in file_list: img = Image.open(src_dir+infile) img = img.convert('RGB') Graphics(infile=src_dir+infile, outfile=src_dir + infile).cut_by_ratio() else: pass else: print("source directory not exist!")
def git_operation(): ''' git 命令行函数,将仓库提交 ---------- 需要安装git命令行工具,并且添加到环境变量中 ''' os.system('git add --all') os.system('git commit -m "add photos"') os.system('git push origin master')
def bubble(bubbleList): listLength = len(bubbleList) while listLength > 0: for i in range(listLength - 1): for j in range(listLength-i-1): if(bubbleList[j].get('arr').get('year') == bubbleList[j+1].get('arr').get('year')): if bubbleList[j].get('arr').get('month') < bubbleList[j+1].get('arr').get('month'): bubbleList[j], bubbleList[j+1] = bubbleList[j+1], bubbleList[j] return bubbleList
def bubbleYear(bubbleList): listLength = len(bubbleList) while listLength > 0: for i in range(listLength - 1): for j in range(listLength-i-1): if bubbleList[j].get('arr').get('year') < bubbleList[j+1].get('arr').get('year'): bubbleList[j], bubbleList[j+1] = bubbleList[j+1], bubbleList[j] return bubbleList
if __name__ == "__main__": cut_photo() compress_photo() git_operation() handle_photo()
|