先上代码:本文主要工给自己参考,在需要的时候直接搜索查找就行了,不想看没有实际运行例子的文档,当参考完这部分还哦未能解决问题在参考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. Oracle级联查询

    在ORACLE 数据库中有一种方法可以实现级联查询   select  *                //要查询的字段 from table              //具有子接点ID与父接点I ...

  2. Python基础:开篇

    一.概述 Python基础 是对 Python(2.7版本)基本语法的要点总结,主要参考了以下资料: Python核心编程(第二版) Learning Python (3rd Edition) Pyt ...

  3. Windows 64位操作系统和32位操作系统在注册表上的有一点不一样

    Windows 64位操作系统为提供对32位应用程序的兼容,在“C:\Windows\SysWOW64”目录下保留了很多32位的工具(如CMD.exe是32位的).在Windows 64位操作系统上跑 ...

  4. Python轻量Web框架Flask使用

    http://blog.csdn.net/jacman/article/details/49098819 目录(?)[+] Flask安装 Python开发工具EclipsePyDev准备 Flask ...

  5. Web基础开发最核心要解决的问题

    Web基础开发要解决的问题,往往也就是那些框架出现的目的 - 要解决问题. 1. 便捷的Db操作: 2. 高效的表单处理: 3. 灵活的Url路由: 4. 合理的代码组织结构: 5. 架构延伸 缓存. ...

  6. strtr对用户输入的敏感词汇进行过滤

    /** * 过滤用户输入的基本数据,防止script攻击 * * @access public * @return string */ function compile_str($str) { $ar ...

  7. Ubuntu安装图形桌面

    apt-get直接更新即可 apt-get install ubuntu-desktop

  8. [转载]hashmap hashtable 的区别

    Hashtable 和 HashMap 做为 Map 的基本特性 两者都实现了Map接口,基本特性相同 -          对同一个Key,只会有一个对应的value值存在 -          如 ...

  9. spring boot学习笔记

    spring boot 是什么 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. spring boot采用了“约定优于配置” ...

  10. css百宝箱

    关于css百宝箱? 在前端学习中,总会遇到零星的知识点,小技巧,这些知识点小到不至于用一片博客写出来,遇到时网上查询一下或许也能搞定,但不一定能记住,所以这篇博客就用来记录那些散落的知识点,积少成多, ...