def flip_function(img_path,save_path):
img = tf.io.read_file(img_path)
img_original = tf.image.decode_jpeg(img, channels=3)
img1 = tf.image.random_flip_left_right(img_original)
img2 = tf.image.random_flip_up_down(img_original)
img1 = tf.cast(img1, dtype=tf.uint8)
img2 = tf.cast(img2, dtype=tf.uint8)
img1 = tf.image.encode_jpeg(img1)
img2 = tf.image.encode_jpeg(img2)
img1_save_path = save_path + '_flip1' + '.jpg'
with tf.io.gfile.GFile(img1_save_path, 'wb') as file:
file.write(img1.numpy())
img2_save_path = save_path + '_flip2' + '.jpg'
with tf.io.gfile.GFile(img2_save_path, 'wb') as file:
file.write(img2.numpy())
imgs_path = 'C:/Users/a/Downloads/test_images/' # 文件所在文件夹
save_path = 'C:/Users/a/Downloads/test_save/' # 保存文件文件夹
filenames = os.listdir(imgs_path)
for filename in filenames:
# 要处理的图片路径
img_path = imgs_path+filename
# 要保存的图片
save_path = save_path+filename[:-4]
flip_function(img_path,save_path)
原始图片:
运行结果: