生成照片库的缩略图图像

以下是为网站生成缩略图图像的程序. 有用,如果您想这样做. 它用于为我的横幅,少女和 Mores -Project Gallery. (http://xahlee.org/periodic_dosage_dir/lanci/lanci.html) 其他Lang欢迎的评论和版本. xah xa*@xahlee.org http://xahlee.org/ # - * - 编码:UTF-8 - * - # Python #©2006-04撰写的Xah Lee,http://xahlee.org/,2006-04 #给出了带有数百张照片的照片的网站库,我想 生成缩略图页面,以便观众可以看到鸟的视线 图片. #从技术上讲: #给定一个dir:例如 /users/xah/web/odecic_dosage_dir/lanci/ #此DIR内部有许多HTML文件,也许在Sub Dir中. #任何HTML文件都是照片库,带有照片的直列图像. #所有图像文件都位于给定的DIR下方或subdir中. #•目标是创建所有内联图像的缩略图图像 在该DIR下的所有HTML文件中. #•这些缩略图图像目的地可以指定,与 给定的dir. #•缩略图图像必须保留其所处的DIR结构. 示例,如果Inline图像的完整路径为/a/b/c/d/1.img,则A 根称为 /a /b,然后缩略图图像的路径必须保留 C/D,作为指定缩略图目的地下的SUD BIR. #•如果内联图像的大小小于特定给定尺寸 (指定为区域),然后跳过. #注意:将考虑HTML文件中的在线内线图像 缩略图. 给定的DIR或链接图像中的任何其他图像都应 被忽略. ############################## 导入RE,子过程,OS.Path #HTML文件和图像所在的路径. 例如 /A B C D inpath ='/user/xah/3d-xplormath/curves'#无尾斜线 #inpath的子字符串. 缩略图将保留DIR结构. 如果图像是在/a/b/c/d/e/e/f/1.png上,而rootdir是/a/b/c,则 缩略图将在/x/y/d/e/f/1.png上 rootdir ='/user/xah/3d-xplormath/curves'#无尾斜线 #Thumbanil图像的目的地路径. 它将被创建. 现有的事物将被过多写. 例如 /x/y thumbnaildir ='/user/xah/3d-xplormath/curves/tn'#无尾斜线 #缩略图大小 缩略图= 150 * 150 #如果图像小于该区域,请不要为其缩略图. minarea = 200*200 #成像"识别"程序路径 识别= r'/sw/bin/识别' convert = r'/sw/bin/convert' #嵌套的深度深入潜水. minlevel = 1; #MyDir的文件和DIR是级别1. maxlevel = 9; # 包括的 ################################# ## 功能 def scalefactor(a,(w,h)): '''getInlineimg(a,(w,h))返回一个数字s,使得w*s*h*s == a. 这用于用给定的图像生成图像的缩放系数 所需的缩略图A. W和H是矩形的宽度和高度 (图片). A给出了照片的缩略图(作为区域). 什么时候 图像在两个维度上都按S缩放,它将具有所需的大小 由区域A指定 thumbnail.''' 返回(float(a)/float(w*h))** 0.5 def getinlineimg(file_full_path): '''getinlineimg(html_file_fulel_path)返回一个列表的数组 内联图像. 例如,它可能返回 ['xx.jpg','../image.png']'''' ff = open(file_full_path,'rb') txt_segs = re.split(r'src',unicode(ff.read(),'utf-8')) txt_segs.pop(0) ff.close() linx = [] 对于txt_segs中的linkblock: matchResult = re.search(ur'\ s*= \ s*\"([^\ \"]+)\"', LinkBlock,re.u) 如果matchResult:linx.append( matchResult.group(1).encode('utf-8')) 返回linx def linkfullpath(dir,locallink): '''linkfullpath(dir,locallink)返回一个完整的字符串 通往本地链接的路径. 例如, linkfullpath('/user/t/t/public_html/a/a/b',','../image/t.png')返回 '用户/t/public_html/a/image/t.png'. 返回的结果不会 包含双重斜线或'../'string.'''' 结果= dir +'/' + locallink 结果= re.sub(r'//+',r'/',结果) while re.search(r'/[^\/]+\/\.\.',结果):结果= re.sub(r'/[^\/]+\/\.\.','',结果) 返回结果 def buildthumbnails(dpath,fname,tbpath,rpath,aketa): u''生成缩略图图像. DPATH是目录的完整路径,并且 FNAME是其下面存在的HTML文件名. tbpath是 缩略图图像目的地DIR. 区域是缩略图图像大小 就其区域而言. 此功能将在 tbpath. rpath是dpath的根部dir子集,用于构建dir 每个缩略图的TBPATH结构. 例如,如果 dpath ='/users/mary/public/pictures' fname ='trip.html'(在DPath下退出) tbpath ='/users/mary/public/thumbs' rpath ='/users/mary/public'(必须是dpath的子字符串或等于 它.) and trip.html包含 然后将在 '/users/mary/public/thumbs/pictures/beijin/day1/img1.jpg' 该函数使用ImageMagick的shell命令 - 转换和 识别,并假设磁盘上的两种路径都设置在 全球vars convert convert'''''' # 大纲: #•在文件中阅读. #•从内联图像标签中获取IMG路径,积累它们 进入列表. #对于每个图像,找到其尺寸w和h. #•在磁盘上生成缩略图图像. #生成图像路径列表. imgpaths = [] 对于filter(lambda x :(不是x.startswith('http')))和(不是 X.Endswith('icon_sum.gif')),getinlineimg(dpath +'/' + fname):): imgpaths.append(linkfullpath(dpath,im)) #如果存在,将图像路径更改为完整尺寸的图像. #也就是说,如果图像以-s.jpg结尾,请找到一个没有'-s'的一个. imgpaths2 = [] 对于我的imgpath中的我: p =我的路 (dirname,filename)= os.path.split(mypath) (fileBasEname,fileextension)= os.path.splitext(文件名) if(re.search(r'-s $',filebasename,re.u)): p2 = os.path.join(dirname,filebasename [0:-2]) + fileextension 如果OS.Path.Exists(P2):P = P2 imgpaths2.append(p) #找出每个图像的宽度和高度 #imgdata中的每个元素都具有[图像完整路径,[width, 高度]] imgdata = [] 对于ImgPaths中的Imp2: #DSCN26 99m-S.JPG JPEG 307x230+0+0 DirectClass 8位51.7K 0.0U 0:01 打印"识别:",imp imginfo = subprocess.popen([识别,imp], stdout = subprocess.pipe).communicate()[0] (宽度,高度)=(imginfo.split()[2]).split('x') 高度=高度split('+')[0] 如果int(width)*int(高度)> minarea:imgdata.append([imp, [int(width),int(高度)]]) ##打印' ' #在缩略图dir中创建缩放图像文件. DIR结构 复制. 对于Imgdata中的Imp: 打印"缩略图:",imp oriimgfullpath = imp [0] thumbnailerativepath = oriimgfullpath [len(rpath) + 1:] thumbnailfullpath = tbpath +'/' +缩略图 打印'r',缩略图 打印" F",Thumbnailfullpath sf = scalefactor(aketa,(imp [1] [0],imp [1] [1])) #print'' #向缩略图DIR制作DIR (dirname,filename)= os.path.split(thumbnailfullpath) (fileBasEname,fileextension)= os.path.splitext(文件名) 打印"创建缩略图:",Thumbnailfullpath 尝试: OS.Makedirs(Dirname,0775) 除(Oserror): 经过 #创建缩略图 subprocess.popen([[convert,'-scale',str(round(sf*100,2)) + '%',oriimgfullpath,thumbnailfullpath])))) #print' ' ################### # 主要的 def dirhandler(虚拟,Curdir,Filess): curdirlevel = len(re.split('/',curdir)) - len(re.split('/',intath)) FILESSLEVEL = Curdirlevel+1 如果minlevel <= filesslevel <= maxlevel: 对于Filess的孩子: 异 如果'.html'== os.path.splitext(child)[1]和 OS.Path.Iffile(Curdir+'/'+儿童): 打印"处理:",curdir+'/'+孩子 BuildThumbNails(Curdir,Child,Thumbnaildir,rootdir,Thumbnailsizearea) 当intath [-1] =='/':inpath = inpath [0:-1]#摆脱尾声 削减 os.path.walk(inpath,dirhandler,"虚拟")

# 回答1

Python代码存档:http://xahlee.org/perl-python/tn_gen.html xah xa*@xahlee.org http://xahlee.org/ XAH Lee写道:( _@_)

标签: python

添加新评论