본문 바로가기
Study/Code

Basemap Screen capture

by Taeho 2021. 1. 10.

Vissim basemap을 만들기 위한 과정으로 여러장의 사진을 카카오맵에서 위성지도를 일일히 캡쳐를 해야하는데,

이를 자동화 할 수 있는 코드이다.

 

이를 활용하여 시간을 절약할 수 있는 작업이 되길 바란다.

 

 

import pyautogui
import time

# ====================숫자 변경이 필요한 variables==================

# 사용자 마다 모니터resolution이 다르기 때문에 최대의 폭과 높이를 알아야함
max_pix_w, max_pix_h = pyautogui.size()
resolution = '4k'

# 카카오맵에서 상화좌우로 ui버튼 및 필요없는 정보들을 제외한 화면캡쳐가 필요함
margin_l = 35 # 왼쪽여백 
margin_r = 60 # 오른쪽여백
margin_top = 70 # 위쪽여백
margin_bottom = 30 # 아래쪽여백

# 사진중첩도
overlap = 0.1

# 축척(20m 또는 30m 만 가능함)
scale = 20

# print screen 버튼 in 칼무리
cap_button = 'f6'

# the unit of width and hight are 'm'
# 찍고자 하는 대상지의 전체 크기
width = 1900
hight = 1000

# =============================================================  
    
# =============================================================  

# 찍는화면의 크기
window_width = max_pix_w - margin_l - margin_r
window_hight = max_pix_h - margin_bottom - margin_top

if resolution == 'fullhd':
    if scale == 20:
        pix4m = 4
    elif scale == 30:
        pix4m = 2

elif resolution == '4k':
    if scale == 20:
        pix4m = 5
    elif scale == 30:
        pix4m = round(70/30)

width_pix, hight_pix = width * pix4m, hight * pix4m

moving_h = round(width_pix / window_width / (1-overlap) + 1)
moving_v = round(hight_pix / window_hight / (1-overlap) + 2)

# =============================================================   
def capture_map(num_width, num_hight, direction = 0):
    for x in range(num_hight):
        if direction % 2 == 0:
            for i in range(num_width):
                pyautogui.moveTo(window_width * (1 - overlap), max_pix_h/2)
                pyautogui.dragTo(margin_l,max_pix_h/2, button='left', duration = 0.2)
                time.sleep(1) # 지도 로딩을 대기하기 위한 시간
                pyautogui.press(cap_button)

        else:
            for i in range(num_width):
                pyautogui.moveTo(margin_l, max_pix_h/2)
                pyautogui.dragTo(window_width * (1 - overlap),max_pix_h/2, button='left', duration = 0.2)
                time.sleep(1)
                pyautogui.press(cap_button)
                
        pyautogui.moveTo(window_width/2, window_hight * (1 - overlap))
        pyautogui.dragTo(window_width/2, margin_top, button='left', duration = 0.2)
        
        time.sleep(1)
        pyautogui.press(cap_button)
        direction += 1
# =============================================================  

time.sleep(2) # 코드실행 후 맵이 있는 브라우저로 전환하기 위한 대기시간
capture_map(moving_h, moving_v)
728x90
반응형

댓글