컴퓨터/Python

python opencv 이미지 그림 회전 미세조정

풍경소리^^ 2022. 11. 30. 16:04

https://ansan-survivor.tistory.com/954

 

[Python OpenCV] 파이썬 이미지 회전하기 각도 조절하기

원본영상을 90도 회전시키는 함수 회전각에 원하는 각을 넣어 회전 가능하다. (for문으면 계속 빙빙돌릴 수 있다) ''' made by 안산드레아스 이미지 회전하기 필요 패키지 : python-opencv ''' import cv2 '''

ansan-survivor.tistory.com

image_rotate.py--------------------

'''
    made by 안산드레아스
    이미지 회전하기
    필요 패키지 : python-opencv
'''
import cv2


'''
    이미지를 돌리는 함수
    :param
        img    : 이미지
        degree : 회전각
'''
def im_rotate(img, degree):
    h, w = img.shape[:-1]

    crossLine = int(((w * h + h * w) ** 0.5))
    centerRotatePT = int(w / 2), int(h / 2)
    new_h, new_w = h, w

    rotatefigure = cv2.getRotationMatrix2D(centerRotatePT, degree, 1)
    result = cv2.warpAffine(img, rotatefigure, (new_w, new_h))
    return result


# 현재경로의 이미지 불러오기
sample = cv2.imread('./t.jpg')
cv2.imshow('org', sample)

# 함수 실행하기
# 원본영상의 자를 영역 원점 x,y좌표 지정. 그리고 새로 생성할 이미지 frame크기 지정
result = im_rotate(sample, 1.2) # 시계방향 회전은 마이너스
cv2.imshow('result', result)
cv2.imwrite('result.jpg', result)

cv2.waitKey(0)
cv2.destroyAllWindows()