本文实例讲述了python图像处理之图像的读取、显示与保存操作。分享给大家供大家参考,具体如下:
python作为机器学习和图像处理的利器,收到越来越多的推崇,特别是在图像处理领域,越来越多的研究和开发开始转向使用python语言,下面就介绍python图像处理中最基本的操作,即图像的读取显示与保存。
1、使用pil模块
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# -*- coding:utf-8 -*-
from pil import image
import numpy as np
def test_pil():
#读取图像
im = image. open ( "lena.jpg" )
#显示图像
im.show()
#转换成灰度图像
im_gray = im.convert( "l" )
im_gray.show()
#保存图像
im_gray.save( "image_gray.jpg" )
return
test_pil()
|
显示结果如下:
2、使用scipy和matplotlib模块
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# -*- coding:utf-8 -*-
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
def test_misc():
#读取图像
im = misc.imread( "lena.jpg" )
#显示图像
plt.figure( 0 )
plt.imshow(im)
#旋转图像
im_rotate = misc.imrotate(im, 90 )
plt.figure( 1 )
plt.imshow(im_rotate)
#保存图像
misc.imsave( "lena_rotate.jpg" , im_rotate)
plt.show()
return
test_misc()
|
显示结果如下:
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/guduruyu/article/details/70738654