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
| import os from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True
def resize_by_size(infile): """按照生成图片文件大小进行处理(单位KB)""" file_name = infile.split('/')[-1] if file_name.split('.')[-1] not in ['jpg','JPG','PNG','png']: pass else: outfilename = 'thumbnail' + infile.split('/')[-1] outfile = '/'.join(infile.split('/')[:-1]) + '/' + outfilename im = Image.open(infile) size_tmp = os.path.getsize(infile) q = 100 while size_tmp > 10240 and q > 0: out = im.resize(im.size, Image.ANTIALIAS) out.save(outfile, quality=q) size_tmp = os.path.getsize(outfile) q -= 10 if q == 100: shutil.copy(infile,outfile)
def equal_img(infile): """ 修改大小 :param cls: :return: """
file_name = infile.split('/')[-1] if file_name.startswith('thumbnail') or file_name.startswith('.'): pass else: if file_name.split('.')[-1] not in ['jpg','JPG','PNG','png','jpeg','JPEG']: pass else: outfilename = 'thumbnail' + infile.split('/')[-1] outfile = '/'.join(infile.split('/')[:-1]) + '/' + outfilename im = Image.open(infile) width = im.size[0] height = im.size[1] print(u'width:height:',width,height) if width <= 200: shutil.copy(infile,outfile) else: coe = float('%.2f'%(200/width)) print(u'coe:',coe) if coe <= 0: coe = 0.15 if im.mode == "P": im = im.convert('RGB') out = im.resize((int(width*coe), int(height*coe)), Image.ANTIALIAS)
out.save(outfile)
def gci(filepath): files = os.listdir(filepath) for fi in files: fi_d = os.path.join(filepath,fi) if os.path.isdir(fi_d): gci(fi_d) else: equal_img(os.path.join(filepath,fi_d))
gci('/root/www/edu_online1/media/works/13265647342/udict/1552525240000/䤋')
|