批量处理图片脚本赏析
批量处理图片脚本赏析
http://www.315ok.org/blogfolder/65
http://www.315ok.org/logo.png
批量处理图片脚本赏析
批量处理图片脚本赏析
该脚本是本人为了应对经常的图片处理而写,主要功能如下:
# updated by Adam on 2005-10-31
#version 1.0
# updated by Adam on 2005-9-29
import Image
import os
#import glob
import math,string
from os.path import *
def AdjustPics(maxsize,Picname):
global imtype
try:
im=Image.open(Picname)
imtype=im.format
xsize,ysize=im.size
if xsize > ysize:
tempsize=xsize
bito=math.floor(maxsize)/tempsize
im2=im.resize((maxsize,ysize * bito))
else:
tempsize=ysize
bito=math.floor(maxsize)/tempsize
im2=im.resize((xsize * bito,maxsize))
return im2
except:
print Picname
def processFiles(SFilepath,DFilepath):
global root,dirs,files
import re
s1=len(SourcePath)
for root, dirs, files in os.walk(SFilepath):
if len(files)!=0:
for name in files:
if name=="Thumbs.db" :
continue
#fetch the file path info
suffix=name.split('.')[-1]
pic=os.path.join(root, name)
temp=pic[s1+1:]
ts=string.split(temp,'.')
temp1=ts[0]
# replace more than one spaces using '_' in filename
temp2=re.sub('\s+|-+','_',temp1)
# add filename suffix
temp3=temp2 + r'.' + suffix
temp_s=temp2 + r'_s.' + suffix
#group the full destination path of file
Dpic=os.path.join(DestPath, temp3)
Dpic_s=os.path.join(DestPath, temp_s)
if os.path.isdir(DFilepath):
pass
else:
os.makedirs(DFilepath)
try:
AdjustPics(maxsize,pic).save(Dpic,imtype) # store the big photo
AdjustPics(maxsize/2,pic).save(Dpic_s,imtype) #store the small photo
except:
continue
if len(dirs)!=0:
#obey the source directory tree to build destination directory tree
for dr in dirs:
SFilepath=os.path.join(root, dr)
temp=SFilepath[s1+1:]
DFilepath=os.path.join(DestPath, temp)
if os.path.isdir(DFilepath):
pass
else:
os.makedirs(DFilepath)
if __name__ == '__main__':
SourcePath=r'D:\pconline\prog\test'
DestPath=r'D:\pconline\prog\test5'
maxsize=180
os.chdir(SourcePath)
SFilepath=SourcePath
DFilepath=DestPath
processFiles(SFilepath,DFilepath)
- 文件夹嵌套,批量处理所有图片类型
- 制定图片规格,并自动生产大小两种图片
- 可以制定生成的图片存放的目标文件夹
# updated by Adam on 2005-10-31
#version 1.0
# updated by Adam on 2005-9-29
import Image
import os
#import glob
import math,string
from os.path import *
def AdjustPics(maxsize,Picname):
global imtype
try:
im=Image.open(Picname)
imtype=im.format
xsize,ysize=im.size
if xsize > ysize:
tempsize=xsize
bito=math.floor(maxsize)/tempsize
im2=im.resize((maxsize,ysize * bito))
else:
tempsize=ysize
bito=math.floor(maxsize)/tempsize
im2=im.resize((xsize * bito,maxsize))
return im2
except:
print Picname
def processFiles(SFilepath,DFilepath):
global root,dirs,files
import re
s1=len(SourcePath)
for root, dirs, files in os.walk(SFilepath):
if len(files)!=0:
for name in files:
if name=="Thumbs.db" :
continue
#fetch the file path info
suffix=name.split('.')[-1]
pic=os.path.join(root, name)
temp=pic[s1+1:]
ts=string.split(temp,'.')
temp1=ts[0]
# replace more than one spaces using '_' in filename
temp2=re.sub('\s+|-+','_',temp1)
# add filename suffix
temp3=temp2 + r'.' + suffix
temp_s=temp2 + r'_s.' + suffix
#group the full destination path of file
Dpic=os.path.join(DestPath, temp3)
Dpic_s=os.path.join(DestPath, temp_s)
if os.path.isdir(DFilepath):
pass
else:
os.makedirs(DFilepath)
try:
AdjustPics(maxsize,pic).save(Dpic,imtype) # store the big photo
AdjustPics(maxsize/2,pic).save(Dpic_s,imtype) #store the small photo
except:
continue
if len(dirs)!=0:
#obey the source directory tree to build destination directory tree
for dr in dirs:
SFilepath=os.path.join(root, dr)
temp=SFilepath[s1+1:]
DFilepath=os.path.join(DestPath, temp)
if os.path.isdir(DFilepath):
pass
else:
os.makedirs(DFilepath)
if __name__ == '__main__':
SourcePath=r'D:\pconline\prog\test'
DestPath=r'D:\pconline\prog\test5'
maxsize=180
os.chdir(SourcePath)
SFilepath=SourcePath
DFilepath=DestPath
processFiles(SFilepath,DFilepath)