PIL&opencv

tech2024-03-24  17

(1) PIL

import os import numpy as np from tqdm import tqdm from PIL import Image for file in tqdm(os.listdir(path)): # 读取图像 img = np.array(Image.open(os.path.join(path, file))) (ids, ext) = os.path.splitext(file) img1 = np.rot90(img, 1) img2 = np.rot90(img, 2) img3 = np.rot90(img, 3) img4 = np.rot90(img, 4) # array转为Image img1 = Image.fromarray(img1) img2 = Image.fromarray(img2) img3 = Image.fromarray(img3) img4 = Image.fromarray(img4) # 保存Image img1.save(os.path.join(augpath, ids + '_1.png')) img2.save(os.path.join(augpath, ids + '_2.png')) img3.save(os.path.join(augpath, ids + '_3.png')) img4.save(os.path.join(augpath, ids + '_4.png'))

(2) opencv-python

import os import cv2 import numpy as np from tqdm import tqdm for file in tqdm(os.listdir(path): # 读取图像 img = cv2.imread(os.path.join(path, name)) (ids, ext) = os.path.splitext(name) img1 = np.rot90(img, 1) img2 = np.rot90(img, 2) img3 = np.rot90(img, 3) img4 = np.rot90(img, 4) # 保存图像 cv2.imwrite(os.path.join(augpath, ids + '_1.png'), img1) cv2.imwrite(os.path.join(augpath, ids + '_2.png'), img2) cv2.imwrite(os.path.join(augpath, ids + '_3.png'), img3) cv2.imwrite(os.path.join(augpath, ids + '_4.png'), img4)

对于jpg图像,存储和读取时图像像素不一致,保存为png时正常 因为jpg是有损压缩格式,保存时会压缩失真(某些像素值会有少许变化),而.png 是无损压缩格式

最新回复(0)