[Python saves the generated image (plt; opencv; PIL)]

Python saves the generated image (plt; opencv; PIL)

1. Use plt to save the generated image

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. Use opencv to save the generated image

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
  • If there is Chinese in the file path, you can use the following script to save and generate pictures

    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. Use PIL to save the generated image

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

Guess you like

Origin blog.csdn.net/crist_meng/article/details/125639877