【python 保存生成的图片 (plt;opencv;PIL)】

python 保存生成的图片 (plt;opencv;PIL)

1. 使用 plt 保存生成的图片

import matplotlib.pyplot as plt
import cv2
import os

images_path = "./test_file/test_1.jpg"

for i,img_name in enumerate(os.listdir(images_path)):
    img_path = os.path.join(images_path,img_name)
    img = cv2.imread(img_path)  # numpy的数组形式,色彩空间为BGR
    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #
    plt.subplot(2,2,i+1),plt.imshow(img)
plt.savefig("./test_new/new_result.jpg") ## 保存图片
plt.show()

2. 使用 opencv 保存生成的图片

images_path = "./test_file/test_1.jpg"
#img = cv.imdecode(np.fromfile("动漫人物_0.jpg",np.uint8))#含有中文路径的图片打开
img = cv2.imread(images_path)  #读取图片
cv2.imwrite("./test_new/new_result.jpg",img)  #将图片保存为1.jpg
  • 若文件路径中存在中文,则可使用下列脚本来进行保存生成图片

    images_path = "./test_file/test_1.jpg"
    img = cv.imdecode(np.fromfile("皇家马德里_1.jpg",np.uint8))#含有中文路径的图片
    cv2.imwrite("./test_new/new_result.jpg",img)  #将图片保存为new_result.jpg
    

3. 使用 PIL 保存生成的图片

images_path = "./test_file/test_1.jpg"
img = Image.open(images_path)  # 打开图片
img.save("./test_new/new_result.jpg")  # 将图片保存为. new_result.jpg

猜你喜欢

转载自blog.csdn.net/crist_meng/article/details/125639877