Python之图像的基本操作和处理

*****************************

Time2017.5.31

AuthorMichael Chan

AddressChongqing University of Technology

Email[email protected] 

FunctionPython图像的基本操作和处理

****************************

Python之图像的基本操作和处理

一.PIL

PILPython Imaging Library,图像处理类库)提供了通用的图像处理功能,以及大量有用的基本图像操作,比如图像缩放、裁剪、旋转、颜色转换等。利用 PIL 中的函数,我们可以从大多数图像格式的文件中读取数据,然后写入最常见的图像格式文件中。PIL 中最重要的模块为 Image

扫描二维码关注公众号,回复: 2745167 查看本文章

To load an image from a file, use the open function in the Image module.

>>>from PIL import Image

>>>im = Image.open(‘11.jpg’).convert(‘L’)

>>>print im.format, im.size, im.mode

>>>im.show()

 The mode attribute defines the number and names of the bands in the image, and also the pixel type and depth. Common modes are “L” (luminance) for greyscale images, “RGB” for true colour images, and “CMYK” for pre-press images.

图像的颜色转换可以使用 convert() 方法来实现。要读取一幅图像,并将其转换成

灰度图像,只需要加上 convert('L')。

二.os模块

python编程时,经常和文件、目录打交道,这是就离不了os模块os模块包含普遍的操作系统功能,与具体的平台无关。以下列举常用的命令

1. os.name()——判断现在正在实用的平台,Windows 返回 ‘nt'; Linux 返回’posix'

2. os.getcwd()——得到当前工作的目录。

3. os.listdir()——指定所有目录下所有的文件和目录名。

4. os.remove()——删除指定文件

5. os.rmdir()——删除指定目录

6. os.mkdir()——创建目录

  注意:这样只能建立一层,要想递归建立可用:os.makedirs()

7. os.path.isfile()——判断指定对象是否为文件。是返回True,否则False

8. os.path.isdir()——判断指定对象是否为目录。是True,否则False

9. os.path.exists()——检验指定的对象是否存在。是True,否则False.

10. os.path.split()——返回路径的目录和文件名。

11. os.getcwd()——获得当前工作的目录(get current work dir)

12. os.system()——执行shell命令。

注意:此处运行shell命令时,如果要调用python之前的变量,可以用如下方式:

var=123

os.environ['var']=str(var) //注意此处[]内得是 “字符串”

os.system('echo $var')

13. os.chdir()——改变目录到指定目录

14. os.path.getsize()——获得文件的大小,如果为目录,返回0

15. os.path.abspath()——获得绝对路径。

16. os.path.join(path, name)——连接目录和文件名。

17.os.path.basename(path)——返回文件名

18. os.path.dirname(path)——返回文件路径

19. 获得程序所在的实际目录

import os

import sys

if __name__ == "__main__":

    print os.path.realpath(sys.argv[0])

    print os.path.split(os.path.realpath(sys.argv[0]))

    print os.path.split(os.path.realpath(sys.argv[0]))[0]

运行结果:

E:\python 2.7.13

('E:\\', 'python 2.7.13')

E:\

三.python os.path模块

os.path.abspath(path) #返回绝对路径

os.path.basename(path) #返回文件名

os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。

os.path.dirname(path) #返回文件路径

os.path.exists(path)  #路径存在则返回True,路径损坏返回False

os.path.lexists  #路径存在则返回True,路径损坏也返回True

os.path.expanduser(path)  #path中包含的"~""~user"转换成用户目录

os.path.expandvars(path)  #根据环境变量的值替换path中包含的”$name””${name}”

os.path.getatime(path)  #返回最后一次进入此path的时间。

os.path.getmtime(path)  #返回在此path下最后一次修改的时间。

os.path.getctime(path)  #返回path的大小

os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误

os.path.isabs(path)  #判断是否为绝对路径

os.path.isfile(path)  #判断路径是否为文件

os.path.isdir(path)  #判断路径是否为目录

os.path.islink(path)  #判断路径是否为链接

os.path.ismount(path)  #判断路径是否为挂载点()

os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径

os.path.normcase(path)  #转换path的大小写和斜杠

os.path.normpath(path)  #规范path字符串形式

os.path.realpath(path)  #返回path的真实路径

os.path.relpath(path[, start])  #start开始计算相对路径

os.path.samefile(path1, path2)  #判断目录或文件是否相同

os.path.sameopenfile(fp1, fp2)  #判断fp1fp2是否指向同一文件

os.path.samestat(stat1, stat2)  #判断stat tuple stat1stat2是否指向同一个文件

os.path.split(path)  #把路径分割成dirnamebasename,返回一个元组

os.path.splitdrive(path)   #一般用在windows下,返回驱动器名和路径组成的元组

os.path.splitext(path)  #分割路径,返回路径名和文件扩展名的元组

os.path.splitunc(path)  #把路径分割为加载点与文件

os.path.walk(path, visit, arg)  #遍历path,进入每个目录都调用visit函数,visit函数必须有

3个参数(arg, dirname, names)dirname表示当前目录的目录名,names代表当前目录下的所有

文件名,args则为walk的第三个参数

os.path.supports_unicode_filenames  #设置是否支持unicode路径名


猜你喜欢

转载自blog.csdn.net/u011344545/article/details/72820595