solve.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:qlcoder 作者: L1nwatch 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号