def online_rotate_picture(image):
"""
?????, ??????
????????????????
:param image:
:return:
"""
# ??????????????????????????
box = image.getbbox()
width = box[2] - box[0]
height = box[3] - box[1]
# print('?????{}??{}??{}'.format(box, width, height))
# ???????????????????
region = image.crop((box[0], box[1], box[2], box[1] + 1))
box2 = region.getbbox()
mid = width / 2
if box2[0] < mid < box2[2]:
# ???????? ?????
return image
adjacent = max(box2[0], width - box2[2]) # ??
# print('?????{}???{}'.format(box2, adjacent))
# ???????????????????
region = image.crop((box[0], box[1], box[0] + 1, box[3]))
box3 = region.getbbox()
opposite = max(box3[1], height - box3[3]) # ??
# print('?????{}???{}'.format(box3, opposite))
import math
angle = math.atan(opposite / adjacent) / math.pi * 180
if box2[0] > mid:
# ?????????????????????????
angle = - angle
# print('??????{}'.format(int(angle)))
# ??
size = image.size
ret = image.resize((size[0] * 2, size[1] * 2), Image.ANTIALIAS)
# ??
ret = ret.rotate(angle)
# ?????????????????
from PIL import ImageEnhance
ret = ImageEnhance.Contrast(ret)
ret = ret.enhance(2)
# ??
ret = ret.crop(ret.getbbox())
# box = image.getbbox()
# width = box[2] - box[0]
# height = box[3] - box[1]
# print('?????{}??{}??{}'.format(box, width, height))
return ret
评论列表
文章目录