Python的Pillow库进行图像文件处理(图文详解)

目录

本文详解的讲解了使用Pillow库进行图片的简单处理,使用PyCharm开发Python的详细过程和各种第三方库的安装与使用。

目标

1.熟悉Python的开发环境;

2.掌握Pillow库的安装方法;

3.熟悉Pillow库的使用方法。

开始吧!

1、打开PyCharm,创建一个新的.py文件

Python的Pillow库进行图像文件处理(图文详解)

2、配置环境

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

本文中使用Python3.6版本开发 点击ok 2、库的安装使用

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

在搜索栏中输入pillow,选中第一个pillow,然后安装。

Python的Pillow库进行图像文件处理(图文详解)

安装完成!

Python的Pillow库进行图像文件处理(图文详解)

此图中我们看到pillow库已配置成功。

3、PIL库概述

PIL库可以完成图像归档和图像处理两方面功能需求:

(1)图像归档:对图像进行批处理、生成图像预览、图像格式转换等;

(2)图像处理:图像基本处理、像素处理、颜色处理等。

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

Python的Pillow库进行图像文件处理(图文详解)

4、代码段

本次使用的图片为:

Python的Pillow库进行图像文件处理(图文详解)

绝对路径为D:\\python作业\\dog.jpg

#图片处理:
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
img = Image.open(\"D:\\python\\dog.jpg\")
print(img.format)		 # 输出图片基本信息
print(img.mode)
print(img.size)
img_resize = img.resize((256,256)) # 调整尺寸
img_resize.save(\"dogresize.jpg\")
img_rotate = img.rotate(45)         # 旋转
img_rotate.save(\"dogrotate.jpg\")
om=img.convert(\'L\')				# 灰度处理
om.save(\'doggray.jpg\')
om = img.filter(ImageFilter.CONTOUR)		# 图片的轮廓
om.save(\'dogcontour.jpg\')
om = ImageEnhance.Contrast(img).enhance(20)		# 对比度为初始的10倍
om.save(\'dogencontrast.jpg\')
#更改图片格式:
from PIL import Image
import os
 
filelist =[\"dog.jpg\",
           \"dogcontour.jpg\",
           \"dogencontrast.jpg\",
           \"doggray.jpg\",
           \"dogresize.jpg\",
           \"dogrotate.jpg\",
           ]
for infile in filelist:
  outfile = os.path.splitext(infile)[0] + \".png\"
  if infile != outfile:
    try:
      Image.open(infile).save(outfile)
    except IOError:
      print (\"cannot convert\", infile)

鼠标右键点击运行

Python的Pillow库进行图像文件处理(图文详解)

运行结果

Python的Pillow库进行图像文件处理(图文详解)

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容