2022年 11月 5日

python打开图片的几种方法

1. opencv方法

  1. import cv2
  2. img_path =r"C:/Users/gf879/Pictures/Saved Pictures/cat1.jpg"
  3. img = cv2.imread(img_path)
  4. cv2.namedWindow('myPicture',0)
  5. cv2.resizeWindow('myPicture', 500, 500)
  6. cv2.imshow('myPicture',img)
  7. cv2.waitKey()
  8. cv2.destroyWindow('myPicture')

结果:

 

2. matplotlib方法

  1. import matplotlib.pyplot as plt
  2. img_path =r"C:/Users/gf879/Pictures/Saved Pictures/cat1.jpg"
  3. img = plt.imread(img_path)
  4. fig = plt.figure('show picture')
  5. # ax = fig.add_subplot(111)
  6. plt.imshow(img)
  7. plt.show()

结果:

3. PIL

  1. from PIL import Image
  2. img=Image.open('C:/Users/gf879/Pictures/Saved Pictures/cat1.jpg')
  3. img.show()

结果: