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
| from PIL import Image import os
source_dirs = './max'
target_dirs = './mid'
target_dirs2 = './min'
def getListFiles(path): assert os.path.isdir(path), '%s not exist.' % path ret = [] has_dirs = [] for root, dirs, files in os.walk(path): s = ''.join(root) + '/' + ''.join(dirs) if s not in has_dirs: has_dirs.append(s) if path == source_dirs: create_dirs(has_dirs) for filesPath in files: ret.append(os.path.join(root, filesPath)) return ret
def create_dirs(has_dirs): for i in has_dirs: i = i.replace(source_dirs, target_dirs) if not os.path.exists(i): os.makedirs(i) for i in has_dirs: i = i.replace(source_dirs, target_dirs2) if not os.path.exists(i): os.makedirs(i)
def scale(i): img = Image.open('' + i) bb = i.replace(source_dirs, target_dirs2) bb = bb.replace('\\', '/') width, height = img.size w_s = int(width / (width / 800)) h_s = int(height / (height / 400)) img = img.resize((w_s, h_s), Image.ANTIALIAS) blank = (w_s - h_s) / 2 region = img.crop((0, 0, w_s, h_s)) region.save(bb)
def resize(i): img = Image.open('' + i) bb = i.replace(source_dirs, target_dirs) bb = bb.replace('\\', '/') width, height = img.size he = (width * 400) / 1920 wi = (height - he) / 2 box = (0, wi, width, he + wi) region = img.crop(box) region.save(bb)
def join_url(utl_txt, lst): file = utl_txt for i in lst: i = i.replace('./', '/') i = i.replace('\\', '/') i1 = i.replace('max', 'mid') i2 = i.replace('max', 'min') with open(file, 'a+') as f: with open(file, 'r') as f1: url = 'https://cdn.jsdelivr.net/gh/runrab/cdn2@master/img' + i + '\n' if url not in f1.readlines(): f.write(url) with open(r"mid.txt", 'a+') as f: with open(r"mid.txt", 'r') as f1: url = 'https://cdn.jsdelivr.net/gh/runrab/cdn2@master/img' + i2 + '\n' if url not in f1.readlines(): f.write(url) with open(r"min.txt", 'a+') as f: with open(r"min.txt", 'r') as f1: url = 'https://cdn.jsdelivr.net/gh/runrab/cdn2@master/img' + i2 + '\n' if url not in f1.readlines(): f.write(url)
def declImg(): a = getListFiles(source_dirs) b = getListFiles(target_dirs) c = getListFiles(target_dirs2) for i in a: if i not in b: b.append(i) resize(i) if i not in c: c.append(i) scale(i) join_url(r"max.txt", a)
declImg()
''' #导出requirements.txt '''
|