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

二、转为二色
 import Image
img=Image.open('o.jpg')
w,h=img.size
img1=Image.new('RGB',(w,h))
for x in range(w):
for y in range(h):
(r,g,b)=img.getpixel((x,y))
if r+g+b>255:
nc=(255,255,255)
else:
nc=(0,0,0)
img1.putpixel((x,y),nc)
img1.save('n.jpg')

转黑白
img.convert('1')

缩放

img.resize(...)

img.thumbnail((new_w, new_h))

旋转

img.rotate(angle)
滤镜

img1 = img.filter(ImageFilter.BLUR)

画图

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

增强

 import ImageEnhance
brightness = ImageEnhance.Brightness(img)
bright_img = brightness.enhance(2.0) ##亮度增强
bright_img.save(img1)
sharpness = ImageEnhance.Sharpness(img)
sharp_img = sharpness.enhance(7.0) #锐度增强
sharp_img.save(img1)
contrast = ImageEnhance.Contrast(img) #对比度增强
contrast_img = contrast.enhance(2.0)
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()

pil的更多相关文章

  1. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  2. Mac osx 安装PIL出现Some externally hosted files were ignored (use --allow-external PIL to allow).

    出现这个问题Some externally hosted files were ignored (use --allow-external PIL to allow)的主要原因是PIL的一些依赖库还没 ...

  3. 使用Python中PIL图形库进行截屏

    目的:通过使用Python的一个图形库PIL(Python Image Library)对屏幕进行截图 步骤: 1.下载PIL(路径)并安装 2.新建文件“截屏.py”,右键Edit with IDL ...

  4. python PIL比较图片像素

    # -*- coding: utf-8 -*- from PIL import Image from pylab import * def compare_pic_L(pic1,pic2): #打开第 ...

  5. 支付宝AR红包引出Python中的PIL小试

    这两天支付宝AR红包火了,周围的同学全在玩.可是我一直在想这个原理是什么?通过请教大神和思考,知道了它有两个限定条件:GPS地理位置和图片的识别.所以,只要我们有了这两个限定条件,就不难进行该红包的破 ...

  6. 解决win7下PIL无法打开图片的问题

    找到PIL安装文件里的ImageShow.py 把第99行的 return "start /wait %s && del /f %s" % (file, file) ...

  7. python2.7安装PIL.Image模块

    这是大家常用的两种安装方法 sudo pip install PIL pip install PIL --allow-external PIL --allow-unverified PIL 如果安装成 ...

  8. Mac下python初学之Image库(PIL)

    Mac下python 使用Image库 安装PIL,下载http://www.pythonware.com/products/pil/ 解压PIL源码包,阅读README知道需要使用python se ...

  9. PIL中的Image和numpy中的数组array相互转换

    1. PIL image转换成array img = np.asarray(image) 需要注意的是,如果出现read-only错误,并不是转换的错误,一般是你读取的图片的时候,默认选择的是&quo ...

  10. windows下python Tkinner环境布置(包含PIL环境安装)

    布置步骤:1.安装python 2.7.11 安装步骤:由于网上存在有相关经验,所以在此引用一下 http://jingyan.baidu.com/article/0bc808fc42dfab1bd4 ...

随机推荐

  1. uva11292 Dragon of Loowater

    水题,排序遍历即可 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  2. 接口与继承ppt作业

    问:为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来? 答:子类拥有父的成员变量和成员方法,如果不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化.不 ...

  3. python读取excel数据,并可视化展现

    #-*- coding: utf-8 -*- import pandas as pda import matplotlib.pyplot as pyl import matplotlib.font_m ...

  4. Host基本概念

    TSO            TSO是Time Sharing Option的缩写,是MVS的命令输入处理器.        TSO是在操作系统的管理下,用来支持ISPF菜单式会话系统和资源管理设备( ...

  5. OpenVZ VPS加速方案–Final Speed

    body,td { font-family: 微软雅黑; font-size: 10pt }   OpenVZ VPS加速方案–Final Speed OpenVZ VPS加速方案–Final Spe ...

  6. CSS 文本和表格中文字溢出显示省略号

    CSS控制文本超出指定宽度后用省略号代替,CSS控制文本不换行. 一般的文字截断(适用于内联与块): .text-overflow {     display:block;/*内联对象需加*/     ...

  7. scala-尾递归

    ------------------------- by chenkh ----------------------------- 随笔记录什么是尾递归,为什么需要尾递归,尾递归show by exa ...

  8. 丰富自己的代码库-SqlServerHelper(Ado)

    设计思路很简单,就是把数据封装为DataTable类,封装了类型转换,使用者可以不必考虑任何类型转换,而使用VC提供的CString即可.封装了按行号查找功能,先看代码 #pragma once #i ...

  9. love easily fade

      Ubuntu addict and loving it , the answerer 's intro attracted me.When I first read a book on linux ...

  10. 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject

    先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...