造数据
图片旋转
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#PROJECT_NAME: PROJECT_NAME
#E_MAIL: renoyuan@foxmail.com
#AUTHOR: reno
import os
from io import BytesIO
from typing import Union
from PIL import Image
def img_rotate(f_b: bytes, angle:Union[int,float], endstuff="PNG") ->bytes:
"""
图片旋转
"""
f=BytesIO()
f.write(f_b)
f2 = BytesIO()
img = Image.open(f)
if angle >= 0 and angle<360:
angle = 360-angle
elif angle <0 and angle>-360:
angle = abs(angle)
dst1 = img.rotate(angle, resample=Image.BICUBIC, expand=True, fillcolor='white')
dst1.save(f2,format=endstuff)
img_res = f2.getvalue()
f.close()
f2.close()
return img_res
def main():
"""
读取数据 造旋转数据
"""
out_p = "/home/ocrproject/forecast_tianjing/test/output_rotate"
input_p = "/home/ocrproject/forecast_tianjing/test/input_rotate"
p_l = os.listdir("/home/ocrproject/forecast_tianjing/test/input_rotate")
for i in p_l:
angle = 0
path = os.path.join(input_p,i)
while angle < 370:
ff = open(path,"rb")
f_byte = ff.read()
ff.close
img_res = img_rotate(f_byte,angle)
with open(os.path.join(out_p,f"{angle}度{i}"),"wb") as f:
f.write(img_res)
angle += 15
main()