화면 비율 계산기
화면 비율을 계산하고 비율을 유지하면서 치수를 조정합니다.
자주 묻는 질문
코드 구현
import math
def simplify_ratio(width: int, height: int) -> tuple[int, int]:
"""Simplify a width:height pair to its lowest integer ratio using GCD."""
g = math.gcd(width, height)
return width // g, height // g
def scale_to_width(orig_w: int, orig_h: int, new_w: int) -> float:
"""Calculate new height given new width, preserving aspect ratio."""
return new_w * (orig_h / orig_w)
def scale_to_height(orig_w: int, orig_h: int, new_h: int) -> float:
"""Calculate new width given new height, preserving aspect ratio."""
return new_h * (orig_w / orig_h)
def ratio_decimal(width: int, height: int) -> float:
"""Return aspect ratio as a decimal (e.g. 1.778 for 16:9)."""
return width / height
# Examples
w, h = simplify_ratio(1920, 1080)
print(f"1920:1080 → {w}:{h}") # 16:9
w, h = simplify_ratio(1280, 720)
print(f"1280:720 → {w}:{h}") # 16:9
w, h = simplify_ratio(800, 600)
print(f"800:600 → {w}:{h}") # 4:3
w, h = simplify_ratio(3840, 2160)
print(f"3840:2160 → {w}:{h}") # 16:9 (4K)
# Scale operations
new_h = scale_to_width(1920, 1080, 1280)
print(f"1920×1080 → width 1280 → height {new_h:.0f}") # 720
new_w = scale_to_height(1920, 1080, 720)
print(f"1920×1080 → height 720 → width {new_w:.0f}") # 1280
# Batch simplification
resolutions = [(1920, 1080), (1280, 720), (3840, 2160), (2560, 1440), (800, 600)]
for rw, rh in resolutions:
sw, sh = simplify_ratio(rw, rh)
print(f"{rw}×{rh} = {sw}:{sh} ({ratio_decimal(rw, rh):.3f})")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.