好玩图像PIL处理

                 好玩图像PIL处理

一、PIL库学习总结

1、PIL中的模块

  Image模块、ImageChops模块、ImageCrackCode模块、ImageDraw模块、ImageEnhance模块、ImageFile模块、ImageFileIO模块、ImageFilter模块、ImageFont模块、ImageGrab模块、ImageOps模块、ImagePath模块、ImageSequence模块、ImageStat模块、ImageTk模块、ImageWin模块、PSDraw模块。最常用模块为Image模块、ImageFilter模块、ImageEnhance模块。

 

2、Image类的解释

         Image类的图像读取和创建方法   

 方法描述
Image.open(filename)        根据参数加载图像文件      
Image.new(mode,size,color)根据给定参数创建一个新的图像
Image.open(StringIO.StringIO(buffer))从字符串中获取图像
Image.frombytes(mode,size,data)根据像素点data创建图像
Image.veify()对图像文件完整性进行检查,返回异常

                                                                                                                                                                                                                          

 

 

 

 

       

           

           Image类的常用属性      

属性描述
Image.format标识图像格式或来源,如果图形不是从文件读取,值为None
Image.mode图形的色彩模式,"L"为灰度图像,"RGB"为真彩色图像,"CMYK"为出版图像
Image.size图像高度与宽度,单位是像素(px),返回值是二元元组(tuple)
Image.palette调色板属性,返回一个ImagePalette类型

                                                                                                                                                                                                                                                                                                                           

   

 

 

 

          

          Image类的序列图像操作方法

方法描述
Image.seek(frame)跳转并返回图像中的指定帧
Image.tell()返回当前帧的序号

 

 

     

 

 

 

         

          Image类的图像转换和保存方法

方法描述
Image.save(filename,format)将图像保存为filename文件名,format是图片格式
Image.convert(mode)使用不同的参数,转换图像为新的模式
Image.thumbnail(size)创建图像的缩略图,size是缩略图尺寸的二元元组

                                                                                                                                                               

 

 

 

 

         

         Image类的图像旋转与缩放方法

方法描述
Image.resize(size)按size大小调整图像,生成副本
Image.rotate(angle)按angle角度旋转图像,生成副本

 

      

 

 

 

 

           Image类的图像像素和通道处理方法

方法描述
Image.piont(func)根据函数func的功能对每个元素进行运算,返回图像副本
Image.split()提取RGB图像的每个图形通道,返回图像副本
Image.merge(mode,bands)合并通道,其中mode表示色彩,bands表示新的色彩通道
Image.blend(im1,im2,alpha)

将两幅图片im1和im2按照如下公式插值后生成新的图像:

im1*(1.0-alpha)+im2*alpha

 

 

 

 

 

 

 

 

 

 

  引用方法: 

1 from PIL import Image
2 im=Image.open("D:\\我的文件\\Python\\壁纸3.jpg")
3 im.thumbnail((158,158))
4 om.save("D:\\我的文件\\Python\\壁纸5.jpg")

 

3、ImageFilter类的解释

                 

             ImageFilter类的预定义过滤方法

方法表示描述
ImageFilter.BLUK图像的模糊效果
ImageFilter.CONTOUR图像的轮廓效果
ImageFilter.DETAIL图像的细节效果
ImageFilter.EDGE_ENHANCE图像的边界加强效果
ImageFilter.EDGE_ENHANCE_MORE图像的阔值边界加强效果
ImageFilter.EMBOSS图像的浮雕效果
ImageFilter.FIND_EDGES图像的边界效果
ImageFilter.SMOOTH图像的平滑效果
ImageFilter.SMOOTH_MORE图像的阔值平滑效果
ImageFilter.SHARPEN图像的锐化效果

   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  引用方法:

1 from PIL import ImageFilter
2 im=Image.open("D:\\我的文件\\Python\\壁纸5.jpg")
3 om=im.filter(ImageFilter.CONTOUR)
4 om.save("D:\\我的文件\\Python\\壁纸2.jpg")
5 om=im.filter(ImageFilter.EMBOSS)
6 om.save("D:\\我的文件\\Python\\壁纸1.jpg")

 

4、ImageEnance类的解释

 

     ImageEnhance类的图像增强和滤镜方法                                                                

             方法          描述
ImageEnhance.enhance(factor)对选择的属性增强factor倍
ImageEnhance.Color(im)调整图像的颜色平衡
ImageEnhance.Contrast(im)调整图像的对比度
ImageEnhance.Brightness(im)调整图像的亮度
ImageEnhance.Sharpness(im)调整图像的锐度

 

 

 

 

 

 

 

 

 

  

  引用如下: 

1 from PIL import Image
2 from PIL import ImageEnhance
3 im=Image.open("D:\\我的文件\\Python\\壁纸4.jpg")
4 om=ImageEnhance.Contrast(im)        #调整图像的对比度
5 om.enhance(20).save("D:\\我的文件\\Python\\壁纸7.jpg")
6 om=ImageEnhance.Sharpness(im)       #调整图像的锐度
7 om.enhance(20).save("D:\\我的文件\\Python\\壁纸6.jpg")

 

 

二、图像处理的实例

1、生成缩略图 

1 from PIL import Image
2 im=Image.open("D:\\我的文件\\Python\\壁纸5.jpg")
3 im.thumbnail((58,58))

 

 显示如下:                 

        

  

1 from PIL import Image
2 im=Image.open("D:\\我的文件\\Python\\壁纸3.jpg")
3 im.thumbnail((158,158))
4 om.save("D:\\我的文件\\Python\\壁纸5.jpg")

 

 

显示如下:

            

 

 

2、图像处理

代码如下:

  

 1 from PIL import Image
 2 from PIL import ImageFilter
 3 im=Image.open("D:\\我的文件\\Python\\壁纸5.jpg")
 4 im.thumbnail((158,158))
 5 om.save("D:\\我的文件\\Python\\壁纸5.jpg")
 6 r,g,b=im.split()
 7 om=Image.merge("RGB",(b,g,r))
 8 om.save("D:\\我的文件\\Python\\壁纸3.jpg")
 9 om=im.filter(ImageFilter.CONTOUR)
10 om.save("D:\\我的文件\\Python\\壁纸2.jpg")
11 om=im.filter(ImageFilter.EMBOSS)
12 om.save("D:\\我的文件\\Python\\壁纸1.jpg")

 

 

显示如下:

  

 

 3、提取图像每一帧

  

 1 from PIL import Image
 2 #from PIL import ImageFilter
 3 im=Image.open("D:\\我的文件\\Python\\动图.gif")
 4 try:
 5     im.save('picframe{:02d}.png'.format(im.tell()))
 6     while True:
 7         im.seek(im.tell()+1)
 8         im.save('picframe{:02d}.png'.format(im.tell()))
 9 except:
10     print("处理结束")

 

 

 

       

 

 

4、美图秀秀  

1 from PIL import Image
2 from PIL import ImageEnhance
3 im=Image.open("D:\\我的文件\\Python\\壁纸4.jpg")
4 om=ImageEnhance.Contrast(im)        #调整图像的对比度
5 om.enhance(20).save("D:\\我的文件\\Python\\壁纸7.jpg")
6 om=ImageEnhance.Sharpness(im)       #调整图像的锐度
7 om.enhance(20).save("D:\\我的文件\\Python\\壁纸6.jpg")
8 om=ImageEnhance.Brightness(im)      #调整图像的亮度
9 om.enhance(1).save("D:\\我的文件\\Python\\壁纸8.jpg")

  

结果如下:

   

  

 

 

 5、好玩的gif图片

  

 1 from PIL import Image
 2 import os #第一步 获得所有图像文件列表,过滤不需要扩展名
 3 filelist = []  
 4 path = os.getcwd()
 5 files = os.listdir("D:\\我的文件\\Python")
 6 for f in files:  
 7     if(os.path.isfile(path + '/' + f)):
 8         if (os.path.splitext(f)[1] == ".BMP"):
 9             filelist.append(f)
10         if (os.path.splitext(f)[1] == ".JPG"):
11             filelist.append(f)
12         if (os.path.splitext(f)[1] == ".PNG"):
13             filelist.append(f)
14         if (os.path.splitext(f)[1] == ".TIF"):
15             filelist.append(f) #第二步 当判断文件不是GIF格式的时候转换为GIF格式
16 for infile in filelist:
17   outfile = os.path.splitext(infile)[0] + ".gif"
18   if infile != outfile:
19     try:
20       Image.open(infile).save(outfile)
21       print("Covert to GIF successfully!")
22     except IOError:
23       print("This format can not support!")
24       infile

  

 之后的成果我不知道怎么展示~~~~

所以就到这里啦

感觉自己好酷哦(哈哈哈哈哈哈哈哈哈哈)

      

 

转载于:https://www.cnblogs.com/sun0618-/p/10687213.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值