【python图像处理】图像的读取、显示与保存


python作为机器学习和图像处理的利器,收到越来越多的推崇,特别是在图像处理领域,越来越多的研究和开发开始转向使用python语言,下面就介绍python图像处理中最基本的操作,即图像的读取显示与保存。 原文地址:https://blog.csdn.net/guduruyu/article/details/70738654

原文地址

1、使用PIL模块

代码如下:

[python] view plain copy
print ?
  1. from PIL import Image  
  2. import numpy as np  
  3.   
  4.   
  5. def test_pil():  
  6.   
  7.     #读取图像  
  8.     im = Image.open(”lena.jpg”)  
  9.     #显示图像  
  10.     im.show()  
  11.   
  12.     #转换成灰度图像  
  13.     im_gray = im.convert(”L”)  
  14.     im_gray.show()  
  15.   
  16.     #保存图像  
  17.     im_gray.save(”image_gray.jpg”)  
  18.   
  19.     return  
 
 

显示结果如下:



2、使用scipy和matplotlib模块

代码如下:

[python] view plain copy
print ?
  1. import numpy as np  
  2. from scipy import misc  
  3. import matplotlib.pyplot as plt  
  4.   
  5.   
  6. def test_misc():  
  7.     #读取图像  
  8.     im = misc.imread(”lena.jpg”)  
  9.     #显示图像  
  10.     plt.figure(0)  
  11.     plt.imshow(im)  
  12.   
  13.     #旋转图像  
  14.     im_rotate = misc.imrotate(im, 90)  
  15.     plt.figure(1)  
  16.     plt.imshow(im_rotate)  
  17.   
  18.     #保存图像  
  19.     misc.imsave(”lena_rotate.jpg”, im_rotate)  
  20.   
  21.     plt.show()  
  22.   
  23.     return  
 
 

显示结果如下:





猜你喜欢

转载自blog.csdn.net/tanlangqie/article/details/79672412