#encoding:cp936
import random
import os
import Image
def getDelta():
#获取随机的四边缩小像素数
delta = [0,0,0,0]
while(1):
for i in range(0,4):
random.seed()
delta[i] = random.randint(1,15)
if(delta[0] > 7 or delta[1] > 7 or delta[2] > 7 or delta[3] > 7):
break
else:
continue
return delta
def getSmallPic(filename,delta):
im = Image.open(filename)
size = im.size
box = (delta[0],delta[1],size[0]-delta[2],size[1]-delta[3])
region = im.crop(box)
region.save(filename,'JPEG')
print filename + ' 保存成功!'
print '========================'
print '请确保待处理文件夹在桌面!'
path = 'C:\\Users\\Administrator\\Desktop\\'
name = raw_input('请输入文件夹名:')
documentName = path + name + '\\'
picList = os.listdir(documentName)
#迭代操作各图片
for pic in picList:
filename = documentName + pic
delta = getDelta()
getSmallPic(filename,delta)
x = input()