generating images from local images

#coding:utf-8
import os,sys

def showImageInHTML(imageTypes,savedir):
    files=getAllFiles(savedir)
    images=[f for f in files if f[f.rfind('.')+1:] in imageTypes]
    images=[item for item in images if os.path.getsize(item)>5*1024]
    images=[item[item.rfind('/'):] for item in images]
    newfile='%s/%s'%(savedir,'index.html')
    with open(newfile,'w') as f:
        f.write('
') for image in images: f.write("%s"%image) f.write("
") f.write("\n"%image) f.write("
") f.write('
') print('success,images are wrapped up in %s'%newfile) def getAllFiles(directory): files=[] for dirpath, dirnames,filenames in os.walk(directory): if filenames!=[]: for file in filenames: files.append(dirpath+'/'+file) files.sort(key=len) return files def cur_file_dir(): path = sys.path[0] if os.path.isdir(path): return path elif os.path.isfile(path): return os.path.dirname(path) if __name__ == '__main__': savedir=cur_file_dir() showImageInHTML(('jpg','png','gif'), savedir)
#coding:utf-8
import os,sys

def showImageInHTML(imageTypes,savedir, images_dir):
    files=getAllFiles(images_dir)
    images=[f for f in files if f[f.rfind('.')+1:] in imageTypes]
    newfile='%s/%s'%(savedir,'index.html')
    with open(newfile,'w') as f:
        f.write('
') for image in images: f.write("%s"%image) f.write("
") f.write("\n"%image) f.write("
") f.write('
') print('success,images are wrapped up in %s'%newfile) def getAllFiles(directory): print(directory) files=[] for dirpath, dirnames,filenames in os.walk(directory): if filenames!=[]: for file in filenames: print(dirpath, dirnames,file) files.append(dirpath+'/'+file) files.sort(key=len) return files def cur_file_dir(): path = sys.path[0] images_dir = sys.argv[1] if os.path.isdir(path): return path, images_dir elif os.path.isfile(path): return os.path.dirname(path), images_dir if __name__ == '__main__': savedir, images_dir=cur_file_dir() showImageInHTML(('jpg','png','gif'), savedir, images_dir)

你可能感兴趣的:(generating images from local images)