自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便。需要安装 PIL
- #!/usr/bin/env python
- import Image
- import os
- import os.path
- import sys
- path = sys.argv[1]
- small_path = (path[:-1] if path[-1]=='/' else path) +'_small'
- if not os.path.exists(small_path):
- os.mkdir(small_path)
- for root, dirs, files in os.walk(path):
- for f in files:
- fp = os.path.join(root, f)
- img = Image.open(fp)
- w, h = img.size
- img.resize((w/2, h/2)).save(os.path.join(small_path, f), "JPEG")
- print fp
python 中 base64 压缩图片,用post传送
Including binaries in your sources
Sometime it's handy to include small files in your sources (icons, test files, etc.)
Let's take a file (myimage.gif) and convert it in base64 (optionnaly compressing it with zlib):
import base64,zlib
print base64.encodestring(zlib.compress(data))
Get the text created by this program and use it in your source:
myFile = zlib.decompress(base64.decodestring("""
eJxz93SzsExUZlBn2MzA8P///zNnzvz79+/IgUMTJ05cu2aNaBmDzhIGHj7u58+fO11ksLO3Kyou
ikqIEvLkcYyxV/zJwsgABDogAmQGA8t/gROejlpLMuau+j+1QdQxk20xwzqhslmHH5/xC94Q58ST
72nRllBw7cUDHZYbL8VtLOYbP/b6LhXB7tAcfPCpHA/fSvcJb1jZWB9c2/3XLmQ+03mZBBP+GOak
/AAZGXPL1BJe39jqjoqEAhFr1fBi1dao9g4Ovjo+lh6GFDVWJqbisLKoCq5p1X5s/Jw9IenrFvUz
+mRXTeviY+4p2sKUflA1cjkX37TKWYwFzRpFYeqTs2fOqEuwXsfgOeGCfmZ57MP4WSpaZ0vSJy97
WPeY5ca8F1sYI5f5r2bjec+67nmaTcarm7+Z0hgY2Z7++fpCzHmBQCrPF94dAi/jj1oZt8R4qxsy
6liJX/UVyLjwoHFxFK/VMWbN90rNrLKMGQ7iQSc7mXgTkpwPXVp0mlWz/JVC4NK0s0zcDWkcFxxF
mrvdlBdOnBySvtNvq8SBFZo8rF2MvAIMoZoPmZrZPj2buEDr2isXi0V8egpelyUvbXNc7yVQkKgS
sM7g0KOr7kq3WRIkitSuRj1VXbSk8v4zh8fljqtOhyobP91izvh0c2hwqKz3jPaHhvMMXVQspYq8
aiV9ivkmHri5u2NH8fvPpVWuK65I3OMUX+f4Lee+3Hmfux96Vq5RVqxTN38YeK3wRbVz5v06FSYG
awWFgMzkktKiVIXkotTEktQUhaRKheDUpMTikszUPIVgx9AwR3dXBZvi1KTixNKyxPRUhcQSBSRe
Sn6JQl5qiZ2CrkJGSUmBlb4+QlIPKKGgAADBbgMp"""))
print "I have a file of %d bytes." % len(myFile)
For example, if you use PIL (Python Imaging Library), you can directly open this image:
import Image,StringIO
myimage.show()
由于在建立我的摄影网站的时候需要使用一些压缩技术,来加快网站的访问速度。
所以,学习了一下如何在python的环境下压缩图片。
1. 下载安装PIL
下载安装按成以后,django报错了,说PIL包出现了错误
然后,我google了错误,找到了这个页面django,说不能使用PIL,必须使用pillow。
所以我又得卸载PIL。
当我使用pip,uninstall pil的时候,发现我的pip版本太低,需要升级,最后我使用easy_install 升级了pip。
重新安装pillow,最好先卸载了旧版本的pillow,然后再安装一次pillow。
完成了恢复。。。。太坑了。。。
2. 在使用PIL的使用,出现了IOError: decoder jpeg not available的错误
解决方法是:
install libpng and libjpeg package (combo installer) from this link: http://ethan.tira-thompson.com/Mac_OS_X_Ports.html
sudo pip install -I pillow
3. 使用PIL 压缩图片, 引用自 http://fc-lamp.blog.163.com/blog/static/174566687201282424018946/
#coding:utf-8''' python图片处理 @author:fc_lamp @blog:http://fc-lamp.blog.163.com/'''from PIL import Image as image#等比例压缩图片def resizeImg(**args): args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size widthRatio = heightRatio = None ratio = 1 if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']): if arg['dst_w'] and ori_w > arg['dst_w']: widthRatio = float(arg['dst_w']) / ori_w #正确获取小数的方式 if arg['dst_h'] and ori_h > arg['dst_h']: heightRatio = float(arg['dst_h']) / ori_h if widthRatio and heightRatio: if widthRatio < heightRatio: ratio = widthRatio else: ratio = heightRatio if widthRatio and not heightRatio: ratio = widthRatio if heightRatio and not widthRatio: ratio = heightRatio newWidth = int(ori_w * ratio) newHeight = int(ori_h * ratio) else: newWidth = ori_w newHeight = ori_h im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q']) ''' image.ANTIALIAS还有如下值: NEAREST: use nearest neighbour BILINEAR: linear interpolation in a 2x2 environment BICUBIC:cubic spline interpolation in a 4x4 environment ANTIALIAS:best down-sizing filter '''#裁剪压缩图片def clipResizeImg(**args): args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size dst_scale = float(arg['dst_h']) / arg['dst_w'] #目标高宽比 ori_scale = float(ori_h) / ori_w #原高宽比 if ori_scale >= dst_scale: #过高 width = ori_w height = int(width*dst_scale) x = 0 y = (ori_h - height) / 3 else: #过宽 height = ori_h width = int(height*dst_scale) x = (ori_w - width) / 2 y = 0 #裁剪 box = (x,y,width+x,height+y) #这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标 #所包围的图像,crop方法与php中的imagecopy方法大为不一样 newIm = im.crop(box) im = None #压缩 ratio = float(arg['dst_w']) / width newWidth = int(width * ratio) newHeight = int(height * ratio) newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q']) #水印(这里仅为图片水印)def waterMark(**args): args_key = {'ori_img':'','dst_img':'','mark_img':'','water_opt':''} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size mark_im = image.open(arg['mark_img']) mark_w,mark_h = mark_im.size option ={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h), 'rightlow':(ori_w-mark_w,ori_h-mark_h) } im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA')) im.save(arg['dst_img']) ''' #Demon#源图片ori_img = './1.jpg'#水印标mark_img = 'D:/mark.png'#水印位置(右下)water_opt = 'rightlow'#目标图片dst_img = './QQQ20140106-1.jpg'#目标图片大小dst_w = 600dst_h = 600#保存的图片质量save_q = 35#裁剪压缩#clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q = save_q)#等比例压缩resizeImg(ori_img=ori_img,dst_img=ori_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)#水印#waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)'''
python(PIL)图像处理(等比例压缩、裁剪压缩) 缩略(水印)图
在开始前,你可以先了解一下PIL基础知识:http://tech.seety.org/python/python_imaging.html#coding:utf-8 ''' python图片处理 @author:fc_lamp @blog:http://fc-lamp.blog.163.com/ ''' import Image as image #等比例压缩图片 def resizeImg(**args): args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size widthRatio = heightRatio = None ratio = 1 if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']): if arg['dst_w'] and ori_w > arg['dst_w']: widthRatio = float(arg['dst_w']) / ori_w #正确获取小数的方式 if arg['dst_h'] and ori_h > arg['dst_h']: heightRatio = float(arg['dst_h']) / ori_h if widthRatio and heightRatio: if widthRatio < heightRatio: ratio = widthRatio else: ratio = heightRatio if widthRatio and not heightRatio: ratio = widthRatio if heightRatio and not widthRatio: ratio = heightRatio newWidth = int(ori_w * ratio) newHeight = int(ori_h * ratio) else: newWidth = ori_w newHeight = ori_h im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q']) ''' image.ANTIALIAS还有如下值: NEAREST: use nearest neighbour BILINEAR: linear interpolation in a 2x2 environment BICUBIC:cubic spline interpolation in a 4x4 environment ANTIALIAS:best down-sizing filter ''' #裁剪压缩图片 def clipResizeImg(**args): args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size dst_scale = float(arg['dst_h']) / arg['dst_w'] #目标高宽比 ori_scale = float(ori_h) / ori_w #原高宽比 if ori_scale >= dst_scale: #过高 width = ori_w height = int(width*dst_scale) x = 0 y = (ori_h - height) / 3 else: #过宽 height = ori_h width = int(height*dst_scale) x = (ori_w - width) / 2 y = 0 #裁剪 box = (x,y,width+x,height+y) #这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标 #所包围的图像,crop方法与php中的imagecopy方法大为不一样 newIm = im.crop(box) im = None #压缩 ratio = float(arg['dst_w']) / width newWidth = int(width * ratio) newHeight = int(height * ratio) newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q']) #水印(这里仅为图片水印) def waterMark(**args): args_key = {'ori_img':'','dst_img':'','mark_img':'','water_opt':''} arg = {} for key in args_key: if key in args: arg[key] = args[key] im = image.open(arg['ori_img']) ori_w,ori_h = im.size mark_im = image.open(arg['mark_img']) mark_w,mark_h = mark_im.size option ={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h), 'rightlow':(ori_w-mark_w,ori_h-mark_h) } im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA')) im.save(arg['dst_img']) #Demon #源图片 ori_img = 'D:/tt.jpg' #水印标 mark_img = 'D:/mark.png' #水印位置(右下) water_opt = 'rightlow' #目标图片 dst_img = 'D:/python_2.jpg' #目标图片大小 dst_w = 94 dst_h = 94 #保存的图片质量 save_q = 35 #裁剪压缩 clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q = save_q) #等比例压缩 #resizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q) #水印 #waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)