先上代码:本文主要工给自己参考,在需要的时候直接搜索查找就行了,不想看没有实际运行例子的文档,当参考完这部分还哦未能解决问题在参考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. c# 正则表达式 匹配中括号&颜色过滤

    现在需要匹配 [color=#000000],以"[color"开头,以"[/color]"结束,中间字符数量不限制,最后返回所有匹配的下标. 代码如下: // ...

  2. 解决打印机报错:操作无法完成(错误0x00000709)。

    解决:操作无法完成(错误0x00000709).再次检查打印机名称,并确保打印机已连接到... 上午同时说,网络打印机打印不了,于是首先看一下打印服务器IP是不是给换了,结果没换. 接着尝试重新添加一 ...

  3. java内部类的使用

    内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...

  4. js不间断滚动

    CSS ul, li { margin: 0; padding: 0; } #scrollDiv { width: 300px; height: 25px; line-height: 25px; bo ...

  5. ActiveReports 报表应用教程 (4)---分栏报表

    在 ActiveReports 中可以实现分栏报表布局样式,可以设置横向分栏.纵向分栏,同时进行分栏和分组设置,统计分栏分组的小计.合计等.在商业报表系统中常见的分栏报表有商品标签.员工工卡.条码打印 ...

  6. LeetCode129:Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  7. ECMall如何支持SSL连接邮件服务器的配置

    首先,主要是ecmall使用的phpmailer版本太低,不支持加密连接. 然后,得对相应代码做一定调整. 1. 覆盖phpmailer 请从附件进行下载: http://files.cnblogs. ...

  8. (旧)子数涵数·DW——网页制作的流程

    PS:这是我很早以前的一个废掉的项目. 当时用的还是table排版,现在基本都是div了吧. 这个项目前段时间,我还抢救过一次,后来还是放弃了. 先行.网页制作的流程分为哪些呢? 一.网站策划(当时, ...

  9. CSS 去掉inline-block元素间隙的几种方法

    最近做移动端页面时,经常会用到inline-block元素来布局,但无可避免都会遇到一个问题,就是inline-block元素之间的间隙.这些间隙会导致一些布局上的问题,需要把间隙去掉.对于inlin ...

  10. 结合微软开放api,使用MSN,Hotmail等登陆Sharepoint网站

    成功使用Windows Live账号登陆SharePoint系统. 附上创建SPTrustedIdentityTokenIssuer的PS脚本====================RegSTS.ps ...