先上代码:本文主要工给自己参考,在需要的时候直接搜索查找就行了,不想看没有实际运行例子的文档,当参考完这部分还哦未能解决问题在参考PIL的相关文档!

 Skip to content
This repository
Search
Pull requests
Issues
Gist
@mm1994uestc
Unwatch 1
Star 0
Fork 0 mm1994uestc/PythonImageProcessing
Code Issues 0 Pull requests 0 Projects 0 Wiki Pulse Graphs Settings
Branch: master Find file Copy pathPythonImageProcessing/ImagePython.py
61e7cd2 35 minutes ago
@mm1994uestc mm1994uestc -a
1 contributor
RawBlameHistory
129 lines (127 sloc) 4.88 KB
import Image as Im
import ImageFilter as ImFilter
import ImageChops as ImChops
import ImageFont as ImFont
import ImageDraw as ImDraw
import math
import sys
import os print 'This File is for you to Transform the Original Pic into another Form!'
im = Im.open('/home/ubuntu-mm/Python/ImageProcess/ImageData/IEEEXtreme.png') #Read the Image
w, h = im.size
def RGB2Gray(image):
w, h = image.size
print 'The Wide is :',w
print 'The Length is :',h
RGB2Gray(im)
def ImRoll(image, Theta):
"Roll a image sideways"
w, h = image.size
Theta = Theta % w
if Theta == 0:
return image
Part1 = image.crop((0, 0, Theta, h))
Part2 = image.crop((Theta, 0, w, h))
image.paste(Part2, (0, 0, w-Theta, h))
image.paste(Part1, (w-Theta, 0, w, h))
return image
Param = 0.5
NewSize = ((int)(w*Param), (int)(h*Param)) #Notice the size must be a integer!
Scaler = Im.ANTIALIAS
im_roll = ImRoll(im, 90) #roll the pic
im_resize = im.resize(NewSize, Scaler) #resize the pic to be 25% Origin'area
im_rotate = im.rotate(45) #rotate the pic for degree at 45
print 'Now,The geomgraphic transform!@ImRoll() @resize() @rotate()'
#im.show() #show():To output the Picture to the windows
#im_roll.show()
#im_resize.show()
#im_rotate.show()
print 'Now,The Filter Transform!@filter()'
im_filter = im.filter(ImFilter.DETAIL) #filter is to change the values of pixel
#im_filter.show()
im_new = Im.new('L', (100,100), [0,255])
#im_new.show()
im2 = Im.open('/home/ubuntu-mm/Python/ImageProcess/ImageData/GitHub.png')
im3 = im2.rotate(45) #Rotate the im2 with the degree at 45
im_blend = Im.blend(im2, im3, 0.2) #The im and im1 must be the same size(im_blend=im2*0.2+im3*(1-0.2))
#im_blend.show()
mask = Im.new('L', im2.size, [0,255])
im4 = Im.composite(im2, im3 ,mask) #Composite two pic into one and filter with the mask windows.
#im4.show()
def ImageTransform(In):
Res = pow(In,2)/255
return Res
print 'Attributes of the Image object!'
print im2.format,im2.mode,im2.size,im2.palette,im2.info
im4.save('/home/ubuntu-mm/Python/ImageProcess/ImageData/login.png','png')
im6 = im2.convert("L") #Change the pic into different Mode:@"L"@"l"@"RGB"@"CMYK"
#im6.show()
im7 = im2.copy() #Copy the image file to a new buffer
im8 = im2.crop((0,0,80,80))
#im8.show()
Bands = im2.getbands() #Get The bands of the Pic
print Bands
for i in Bands:
print i
Extreme = im2.getextrema() #Get Max_values and Min_values of pic(GrayScale)
for i in Extreme:
print i
PixelValues = im2.getpixel((0,0)) #Get to values of the coordinate you input(0,0)
print 'The pixelValues of point(0,0) is:',PixelValues
w, h = im2.size #Get the size of the pic
mask = Im.new('L',(w,h),[0,255]) #To create a new mask
list1 = im2.convert("L").histogram(mask) #To show the histogram with the statical number of the GrayScale Values where the mask is nonzero replect
print list1
im9 = ImChops.offset(im2 , -10, -10) #To move the pic with the offset produce a new pic
#im9.show()
im10 = im2.point(ImageTransform) #To transform every Pixel's values with func ImageTranform which defined before
#im10.show() #show out the image
im2.putpixel((10,10),(0,0,0)) #Location:(10,10) pixel's color value into (0,0,0)
im2.show()
im11 = im2.resize((80,80)) #To resize The picture into newsize (80,80)
#im11.show()
R,G,B = im11.split() #To split the im11's three channel in to RGB linear bands
print R
im12 = im2.copy()
im12.thumbnail((80,60)) #To resize the Pic as the rate->Height:Width
#For eg:im.size()=(400,150) It's after im.thumbnail((40,40)) will be (40,15)
#im12.show()
Method = Im.ROTATE_90 #@Im.FLIP_RIGHT_LEFT@Im.FLIP_TOP_BOTTOM@Im.ROTATE_90@Im.ROTATE_180
im13 = im2.copy()
im13.transpose(Method) #To transform the pic as the Method show
#im13.show()
print 'Now,Let draw what we want!'
Cavon1 = Im.new('RGB',(300,300),(255,255,255))
draw = ImDraw.Draw(Cavon1)
draw.arc((0,0,202,202), 0, 135, (0,255,0))
draw.arc((0,0,205,205), 0, 135, (255,0,0))
draw.arc((0,0,208,208), 0, 135, (0,0,255))
draw.arc((0,0,211,211), 0, 135, (255,255,0))
draw.arc((0,0,212,212), 0, 135, (255,0,255))
#Cavon2 = Im.new('RGB',(200,300),(255,255,255))
draw.ellipse((0,0,30,40),(0,255,0))
draw.ellipse((20,20,40,30),(255,125,30))
draw.line(((60,60),(90,60),(90,90),(60,90),(60,60)),(255,0,0))
draw.point((100,100),(255,0,255))
draw.polygon([(60,60),(90,60),(90,90),(60,90)],fill="red",outline="green")
#fontPath = "/usr/share/fonts/dejavu-lgc/DejaVuLGCSansCondensed-Bold.ttf"
#sans16 = ImFont.truetype(fontPath,16)
draw.text((130,80),"Hello PIL!",fill="red")
Cavon1.show()
print 'Image Filter!'
im_filter1 = im2.filter(ImFilter.BLUR)
im_filter2 = im2.filter(ImFilter.CONTOUR)
im_filter3 = im2.filter(ImFilter.DETAIL)
im_filter4 = im2.filter(ImFilter.EDGE_ENHANCE)
im_filter5 = im2.filter(ImFilter.EDGE_ENHANCE_MORE)
im_filter6 = im2.filter(ImFilter.FIND_EDGES)
im_filter7 = im2.filter(ImFilter.SMOOTH)
im_filter8 = im2.filter(ImFilter.SHARPEN)
im_filter1.show()
im_filter2.show()
im_filter3.show()
im_filter4.show()
im_filter5.show()
im_filter6.show()
im_filter7.show()
im_filter8.show()
Contact GitHub API Training Shop Blog About
© 2016 GitHub, Inc. Terms Privacy Security Status Help

程序里面使用的数据和图片都在我的github源码中,请参照mm1994uestc--》https://github.com/mm1994uestc/PythonImageProcessing/blob/master/ImagePython.py

PIL库的下载Python Imaging Library (PIL)--》http://www.pythonware.com/products/pil/index.htm

详细的PIL教程参照Python PIL hand--》http://effbot.org/imagingbook/pil-index.htm

也可以参考这个网站--》http://effbot.org/imagingbook/

如有错误,还请多多指教!

Python中的库使用之一 PIL的更多相关文章

  1. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...

  2. Python中第三方库Requests库的高级用法详解

    Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...

  3. Python中cv2库和matplotlib库色彩空间排布不一致

    今天在python中读如图片时发现以下问题: 1.在from matplotlib import pyplot as plt之后,再import cv2 cv2.imshow()不能正常使用,还不知道 ...

  4. Python 中拼音库 PyPinyin 的用法【华为云技术分享】

    [摘要] 最近碰到了一个问题,项目中很多文件都是接手过来的中文命名的一些素材,结果在部署的时候文件名全都乱码了,导致项目无法正常运行. 后来请教了一位大佬怎么解决文件名乱码的问题,他说这个需要正面解决 ...

  5. python中pyperclip库的功能

    python3中pyperclip库的功能 作用就是复制.粘贴 例子 import pyperclip pyperclip.copy('Hello world!') pyperclip.paste() ...

  6. Python中msgpack库的使用

    msgpack用起来像json,但是却比json快,并且序列化以后的数据长度更小,言外之意,使用msgpack不仅序列化和反序列化的速度快,数据传输量也比json格式小,msgpack同样支持多种语言 ...

  7. Python中datetime库的用法

    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...

  8. Python 中 selenium 库

    目录 selenium 基础语法 一. 环境配置 1. 安装环境 2. 配置参数 3. 常用参数搭配 4. 分浏览器启动 二. 基本语法 1. 元素定位 2. 控制浏览器操作 3. 操作元素的方法 3 ...

  9. Python中第三方库的安装

    网上的帖子挺多的,教你如何安装,安装第三方工具库的方法总共分为三类:Dos系统下pip命令:安装包下载安装:IDE集成环境下安装(Pycharm,Spyder……) http://www.jiansh ...

随机推荐

  1. sql server索引功能资料

    无论何时对基础数据执行插入.更新或删除操作,SQL Server 数据库引擎都会自动维护索引.随着时间的推移,这些修改可能会导致索引中的信息分散在数据库中(含有碎片).当索引包含的页中的逻辑排序(基于 ...

  2. css中position与z-index

    position属性 在css中,position属性用来控制元素的位置信息,其取值共有4种,即static.relative.absolute.fixed. 静态定位(static) 若没有指定po ...

  3. 根据Expander的IsExpanded属性值的变化动态设计Control的size

    简要说明: 当Expander 的IsExpanded属性为“True” 时给控件设个尺寸(此处为高度),当为“False”时给控件设另外一个值. 知识点:数据绑定.Style和Trigger < ...

  4. 【C#进阶系列】05 基元类型、引用类型和值类型

     基元类型和FCL类型 FCL类型就是指Int32这种类型,这是CLR支持的类型. 而基元类型就是指int这种类型,这是C#编译器支持的,实际上在编译后,还是会被转为Int32类型. 而且学过C的朋友 ...

  5. 【JS复习笔记】00 序

    作为一个前端苦手,说是复习,你就当我是重学好了. 好吧,我当然不可能抱着一个砖头去复习,所以捡了本薄的来读——<JavaScript语言精粹>. 当初带我的人说这本书挺好,就看这本书好了. ...

  6. C#中ListView的简单使用方法

    ListView是用于显示数据的,先在窗体中拉一个lisview控件,还有一些新增.修改.删除.查询按钮和文本框,控件名称为listview,按钮为btnInsert,btnUpate,btnDele ...

  7. 邻接矩阵c源码(构造邻接矩阵,深度优先遍历,广度优先遍历,最小生成树prim,kruskal算法)

    matrix.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include < ...

  8. AC自动机---Keywords Search

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...

  9. 容器--ArrayList

    一.前言 作为List的重要实现之一,ArrayList已经成了我们编写程序时不可或缺的重要容器之一,面试的时候也经常会被问到,所以,深入理解其实现机制,无论是对于我们正确使用这个类来说,还是准备面试 ...

  10. Chrome使用技巧(几个月的心得)

    转用Chrome,不仅仅因为它的插件之丰富,更因为它的响应速度其他浏览器都望尘莫及.接着我就要写写一些心得. 如何最简易地用上谷歌搜索? 1,下载hosts文件:https://pan.baidu.c ...