Board logo

标题: 【Python工具包/库推荐系列】- Python 图片处理包 [打印本页]

作者: 龙听    时间: 2024-1-8 11:05     标题: 【Python工具包/库推荐系列】- Python 图片处理包

如果您的Python应用程序以任何方式与图像进行交互,则Python映像库(也称为PIL或Pillow)是Python必需的。它使编写以各种格式打开,修改和保存图像的代码变得容易。
  1. from PIL import Image
  2. #Open image using Image module
  3. im = Image.open("images/cuba.jpg")
  4. #Show actual Image
  5. im.show()
  6. #Show rotated Image
  7. im = im.rotate(45)
  8. im.show()
复制代码
如果您要对图像进行更高级的处理(例如图像识别,在这种情况下,OpenCV将是一个不错的选择),Pillow不会自行裁切的。但是对于基本的图像导入,处理和导出,Pillow是您的首选解决方案。
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt

  4. image = cv2.imread("C://gfg//tomatoes.jpg", 1)
  5. # Loading the image

  6. half = cv2.resize(image, (0, 0), fx = 0.1, fy = 0.1)
  7. bigger = cv2.resize(image, (1050, 1610))

  8. stretch_near = cv2.resize(image, (780, 540),
  9.                interpolation = cv2.INTER_NEAREST)


  10. Titles =["Original", "Half", "Bigger", "Interpolation Nearest"]
  11. images =[image, half, bigger, stretch_near]
  12. count = 4

  13. for i in range(count):
  14.     plt.subplot(2, 2, i + 1)
  15.     plt.title(Titles[i])
  16.     plt.imshow(images[i])

  17. plt.show()
复制代码





欢迎光临 龙听期货论坛 (http://www.qhlt.cn/) Powered by Discuz! 7.2