图片批量旋转与翻转工具
使用python实现的图片批量旋转与翻转工具,能够多选图片然后统一旋转或翻转。
支持五种操作:1.向左旋转90度 2.向右旋转90度 3.旋转180度 4.左右翻转 5.上下翻转
## -*- coding: utf-8 -*-
# written by starvapour, 23/5/2021
from cv2 import transpose, flip, imwrite, imdecode, imencode
from tkinter import Tk, filedialog
from time import sleep
from numpy import fromfile, uint8
from os.path import splitext
def process_1(img):
return flip(transpose(img), 0)
def process_2(img):
return flip(transpose(img), 1)
def process_3(img):
return flip(img, -1)
def process_4(img):
return flip(img, 1)
def process_5(img):
return flip(img, 0)
operations = {1:process_1, 2:process_2, 3:process_3, 4:process_4, 5:process_5}
operation = None
# 用于批量旋转图片
print("请选择需要旋转/翻转的图片:")
root = Tk()
root.withdraw()
img_paths = filedialog.askopenfilenames(filetypes=[('图片文件', ('.jpg', '.jpeg', '.png', '.bmp')), ('所有文件', '*')])
type_correct = False
while not type_correct:
try:
print("1.向左旋转90度 2.向右旋转90度 3.旋转180度 4.左右翻转 5.上下翻转")
operation = operations[int(input("请输入数字选择你想要的操作:"))]
type_correct = True
except:
print("输入值错误,请重新输入。\n")
print()
for img_path in img_paths:
try:
img = imdecode(fromfile(img_path, dtype=uint8), -1)
imencode(splitext(img_path)[1], operation(img))[1].tofile(img_path)
except:
print("警告:你选择的文件"+img_path+"在运行时出现错误!")
print("所有正确图片已完成缩放,可以关闭窗口。")
sleep(60)