Python中的库使用之一 PIL

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

  1 Skip to content
  2 This repository
  3 Search
  4 Pull requests
  5 Issues
  6 Gist
  7  @mm1994uestc
  8  Unwatch 1
  9   Star 0
 10  Fork 0 mm1994uestc/PythonImageProcessing
 11  Code  Issues 0  Pull requests 0  Projects 0  Wiki  Pulse  Graphs  Settings
 12 Branch: master Find file Copy pathPythonImageProcessing/ImagePython.py
 13 61e7cd2  35 minutes ago
 14 @mm1994uestc mm1994uestc -a
 15 1 contributor
 16 RawBlameHistory     
 17 129 lines (127 sloc)  4.88 KB
 18 import Image as Im
 19 import ImageFilter as ImFilter
 20 import ImageChops as ImChops
 21 import ImageFont as ImFont
 22 import ImageDraw as ImDraw
 23 import math
 24 import sys
 25 import os
 26 
 27 print 'This File is for you to Transform the Original Pic into another Form!'
 28 im = Im.open('/home/ubuntu-mm/Python/ImageProcess/ImageData/IEEEXtreme.png') #Read the Image
 29 w, h = im.size
 30 def RGB2Gray(image):
 31     w, h = image.size
 32     print 'The Wide is :',w
 33     print 'The Length is :',h
 34 RGB2Gray(im)
 35 def ImRoll(image, Theta):
 36     "Roll a image sideways"
 37     w, h = image.size
 38     Theta = Theta % w
 39     if Theta == 0:
 40         return image
 41     Part1 = image.crop((0, 0, Theta, h))
 42     Part2 = image.crop((Theta, 0, w, h))
 43     image.paste(Part2, (0, 0, w-Theta, h))
 44     image.paste(Part1, (w-Theta, 0, w, h))
 45     return image
 46 Param = 0.5
 47 NewSize = ((int)(w*Param), (int)(h*Param))  #Notice the size must be a integer!
 48 Scaler = Im.ANTIALIAS
 49 im_roll = ImRoll(im, 90)  #roll the pic
 50 im_resize = im.resize(NewSize, Scaler)  #resize the pic to be 25% Origin'area
 51 im_rotate = im.rotate(45)  #rotate the pic for degree at 45
 52 print 'Now,The geomgraphic transform!@ImRoll() @resize() @rotate()'
 53 #im.show()  #show():To output the Picture to the windows
 54 #im_roll.show()
 55 #im_resize.show()
 56 #im_rotate.show()
 57 print 'Now,The Filter Transform!@filter()'
 58 im_filter = im.filter(ImFilter.DETAIL)  #filter is to change the values of pixel
 59 #im_filter.show()
 60 im_new = Im.new('L', (100,100), [0,255])
 61 #im_new.show()
 62 im2 = Im.open('/home/ubuntu-mm/Python/ImageProcess/ImageData/GitHub.png')
 63 im3 = im2.rotate(45) #Rotate the im2 with the degree at 45
 64 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))
 65 #im_blend.show()
 66 mask = Im.new('L', im2.size, [0,255])
 67 im4 = Im.composite(im2, im3 ,mask) #Composite two pic into one and filter with the mask windows.
 68 #im4.show()
 69 def ImageTransform(In):
 70     Res = pow(In,2)/255
 71     return Res
 72 print 'Attributes of the Image object!'
 73 print im2.format,im2.mode,im2.size,im2.palette,im2.info
 74 im4.save('/home/ubuntu-mm/Python/ImageProcess/ImageData/login.png','png')
 75 im6 = im2.convert("L") #Change the pic into different Mode:@"L"@"l"@"RGB"@"CMYK"
 76 #im6.show()
 77 im7 = im2.copy() #Copy the image file to a new buffer
 78 im8 = im2.crop((0,0,80,80))
 79 #im8.show()
 80 Bands = im2.getbands() #Get The bands of the Pic
 81 print Bands
 82 for i in Bands:
 83     print i
 84 Extreme = im2.getextrema() #Get Max_values and Min_values of pic(GrayScale) 
 85 for i in Extreme:
 86     print i
 87 PixelValues = im2.getpixel((0,0)) #Get to values of the coordinate you input(0,0) 
 88 print 'The pixelValues of point(0,0) is:',PixelValues
 89 w, h = im2.size #Get the size of the pic
 90 mask = Im.new('L',(w,h),[0,255]) #To create a new mask
 91 list1 = im2.convert("L").histogram(mask) #To show the histogram with the statical number of the GrayScale Values where the mask is nonzero replect
 92 print list1
 93 im9 = ImChops.offset(im2 , -10, -10) #To move the pic with the offset produce a new pic
 94 #im9.show()
 95 im10 = im2.point(ImageTransform) #To transform every Pixel's values with func ImageTranform which defined before
 96 #im10.show() #show out the image
 97 im2.putpixel((10,10),(0,0,0)) #Location:(10,10) pixel's color value into (0,0,0)
 98 im2.show()
 99 im11 = im2.resize((80,80))  #To resize The picture into newsize (80,80)
100 #im11.show()
101 R,G,B = im11.split()  #To split the im11's three channel in to RGB linear bands
102 print R
103 im12 = im2.copy()
104 im12.thumbnail((80,60)) #To resize the Pic as the rate->Height:Width
105 #For eg:im.size()=(400,150) It's after im.thumbnail((40,40)) will be (40,15)
106 #im12.show()
107 Method = Im.ROTATE_90 #@Im.FLIP_RIGHT_LEFT@Im.FLIP_TOP_BOTTOM@Im.ROTATE_90@Im.ROTATE_180
108 im13 = im2.copy()
109 im13.transpose(Method) #To transform the pic as the Method show
110 #im13.show()
111 print 'Now,Let draw what we want!'
112 Cavon1 = Im.new('RGB',(300,300),(255,255,255))
113 draw = ImDraw.Draw(Cavon1)
114 draw.arc((0,0,202,202), 0, 135, (0,255,0))
115 draw.arc((0,0,205,205), 0, 135, (255,0,0))
116 draw.arc((0,0,208,208), 0, 135, (0,0,255))
117 draw.arc((0,0,211,211), 0, 135, (255,255,0))
118 draw.arc((0,0,212,212), 0, 135, (255,0,255))
119 #Cavon2 = Im.new('RGB',(200,300),(255,255,255))
120 draw.ellipse((0,0,30,40),(0,255,0))
121 draw.ellipse((20,20,40,30),(255,125,30))
122 draw.line(((60,60),(90,60),(90,90),(60,90),(60,60)),(255,0,0))
123 draw.point((100,100),(255,0,255))
124 draw.polygon([(60,60),(90,60),(90,90),(60,90)],fill="red",outline="green")
125 #fontPath = "/usr/share/fonts/dejavu-lgc/DejaVuLGCSansCondensed-Bold.ttf"
126 #sans16 = ImFont.truetype(fontPath,16)
127 draw.text((130,80),"Hello PIL!",fill="red")
128 Cavon1.show()
129 print 'Image Filter!'
130 im_filter1 = im2.filter(ImFilter.BLUR)
131 im_filter2 = im2.filter(ImFilter.CONTOUR)
132 im_filter3 = im2.filter(ImFilter.DETAIL)
133 im_filter4 = im2.filter(ImFilter.EDGE_ENHANCE)
134 im_filter5 = im2.filter(ImFilter.EDGE_ENHANCE_MORE)
135 im_filter6 = im2.filter(ImFilter.FIND_EDGES)
136 im_filter7 = im2.filter(ImFilter.SMOOTH)
137 im_filter8 = im2.filter(ImFilter.SHARPEN)
138 im_filter1.show()
139 im_filter2.show()
140 im_filter3.show()
141 im_filter4.show()
142 im_filter5.show()
143 im_filter6.show()
144 im_filter7.show()
145 im_filter8.show()
146 Contact GitHub API Training Shop Blog About
147 © 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/

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

原文地址:https://www.cnblogs.com/uestc-mm/p/6025123.html