http://effbot.org/imagingbook/

一、安装

pip install PIL --allow-external PIL --allow-unverified PIL
注:新版pip不支持--allow-external了,但可直接安装
pip install pillow
如报错找不到Python.h,安装python-dev,并将Python.h所在路径加入C_INCLUDE_PATH
 
 
出现“The _imagingft C module is not installed”错误
删除pil后安装字体包再重装pil
centos:
yum install libjpeg-devel
yum install freetype-devel
yum install libpng-devel

ubuntu:
apt-get install libfreetype6-dev
os:
brew install freetype

在ubuntu的virtualenv下用pip不成功,但apt-get python-pil不会出错。

或在vitrualenv下安装pillow代替,可能需要先sudo apt-get install libjpeg8-dev

使用pillow可能出现keyerror:jpeg错误,使用from PIL import Image代替import Image

二、转为二色
 1 import Image
 2 img=Image.open('o.jpg')
 3 w,h=img.size
 4 img1=Image.new('RGB',(w,h))
 5 for x in range(w):
 6     for y in range(h):
 7         (r,g,b)=img.getpixel((x,y))
 8         if r+g+b>255:
 9             nc=(255,255,255)
10         else:
11             nc=(0,0,0)
12         img1.putpixel((x,y),nc)
13 img1.save('n.jpg')

转黑白
img.convert('1')

缩放

img.resize(...)

img.thumbnail((new_w, new_h))

旋转

img.rotate(angle)
滤镜

img1 = img.filter(ImageFilter.BLUR)

画图

1 import ImageDraw 
2 draw = ImageDraw.Draw(img) 
3 width,height = img.size 
4 draw.line(((0,0),(width-1,height-1)),fill=255) #画直线 
5 draw.line(((0,height-1),(width-1,0)),fill=255) 
6 draw.arc((0,0,width-1,height-1),0,360,fill=255) #画椭圆 
7 img.save(self.save_file) 

增强

 1 import ImageEnhance 
 2 brightness = ImageEnhance.Brightness(img) 
 3 bright_img = brightness.enhance(2.0) ##亮度增强 
 4 bright_img.save(img1) 
 5 sharpness = ImageEnhance.Sharpness(img) 
 6 sharp_img = sharpness.enhance(7.0) #锐度增强 
 7 sharp_img.save(img1) 
 8 contrast = ImageEnhance.Contrast(img) #对比度增强 
 9 contrast_img = contrast.enhance(2.0)  
10 contrast_img.save(img1) 

三、显示

pic是jpg格式的二进制字符串

pic=file.read('1.jpg')

 

im=Image.open(io.BytesIO(pic)) #StringIO.StringIO(pic)

im.show()

 

#wx
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# increase the size of the frame for larger images
frame1 = wx.Frame(None, -1, "An image on a panel", size = (400, 300))
bmp=wx.BitmapFromImage(wx.ImageFromStream(StringIO.StringIO(pic)))
wx.StaticBitmap(frame1, -1, bmp, (10, pos), (bmp.GetWidth(), bmp.GetHeight()))
frame1.Show(1)
app.MainLoop()

 

posted on 2015-12-05 23:15  perel  阅读(311)  评论(0编辑  收藏  举报