티스토리 뷰

import math
from PIL import Image


def rotate(x, y, theta, pi=3.14):
    radian = theta * pi / 180
    sin = math.sin(radian)
    cos = math.cos(radian)
    new_x = cos*x - sin*y
    new_y = sin*x + cos*y
    return new_x, new_y


def rotate_img(ori, theta):
    dst = Image.new("RGB", ori.size, (0, 0, 0))
    width, height = ori.size
    pixels_dst = dst.load()
    pixels_ori = ori.load()
    for y in range(height):
        for x in range(width):
            rotate_x, rotate_y = rotate(x, y, theta)
            if 0 <= rotate_x < width and 0 <= rotate_y < height:
                pixels_dst[rotate_x, rotate_y] = pixels_ori[x, y]
    return dst


img = Image.open("./img/rirak.jpg")
dst = rotate_img(img, 5)
img.show()
dst.show()

이차원 좌표에서 이미지 회전 공식

 

파이썬에서 각세타가 아니라 라디안으로 math 함수에 인자를 넣어야함

 

라디안 = 세타 * pi / 180

 

 

 

 

 

'python lecture > project' 카테고리의 다른 글

[python] 호텔 평점 프로젝트  (0) 2020.11.08
[python] 은행프로그램  (0) 2020.07.02
[python] 지하철 승하차 기록 조회  (0) 2020.06.07
[python] n 진수 바꾸기  (0) 2020.06.03
[python] 시간변환  (0) 2020.05.27
댓글