import tensorflow as tf
import os
import tensorflow_addons as tfa
import numpy as np
def rotate_function(img_path,save_path):
img = tf.io.read_file(img_path)
img_original = tf.image.decode_jpeg(img, channels=3)
# img = tf.image.rot90(img,1)
for radient in np.arange(0.1,3,0.4): # 这个可以自己设置,步长什么的
img = tfa.image.transform_ops.rotate(img_original,radient)
img = tf.cast(img, dtype=tf.uint8)
img = tf.image.encode_jpeg(img)
img_save_path = save_path + '_rotate' + str(int(radient*10)) + '.jpg'
with tf.io.gfile.GFile(img_save_path,'wb') as file:
file.write(img.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]
rotate_function(img_path, save_path)
效果: