이미지 반전 & 회전
이미지를 수평·수직으로 반전하고 90°, 180°, 270° 회전합니다. 모두 브라우저에서 처리.
자주 묻는 질문
코드 구현
# Flip and rotate images with Pillow
# pip install Pillow
from PIL import Image
import os
def flip_and_rotate(input_path: str, output_path: str,
flip_horizontal: bool = False,
flip_vertical: bool = False,
rotate_degrees: float = 0,
expand: bool = True,
fill_color: tuple = (255, 255, 255)) -> None:
"""
Flip and/or rotate an image.
rotate_degrees: Positive = counter-clockwise, Negative = clockwise.
expand: If True, expand the canvas to fit rotated content (no cropping).
fill_color: Background color for empty areas after arbitrary rotation.
"""
img = Image.open(input_path)
# Horizontal flip (left-right mirror)
if flip_horizontal:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
# Vertical flip (top-bottom mirror)
if flip_vertical:
img = img.transpose(Image.FLIP_TOP_BOTTOM)
# Rotate — Pillow rotates counter-clockwise by default
if rotate_degrees != 0:
if rotate_degrees % 90 == 0:
# Lossless 90/180/270 degree rotation
img = img.rotate(rotate_degrees, expand=True)
else:
# Arbitrary angle — needs expand + fill
img = img.rotate(
rotate_degrees,
expand=expand,
fillcolor=fill_color,
resample=Image.BICUBIC,
)
img.save(output_path)
print(f"Saved: {output_path} ({img.size})")
# Examples
flip_and_rotate("photo.jpg", "flipped_h.jpg", flip_horizontal=True)
flip_and_rotate("photo.jpg", "flipped_v.jpg", flip_vertical=True)
flip_and_rotate("photo.jpg", "rotated_90.jpg", rotate_degrees=90)
flip_and_rotate("photo.jpg", "rotated_180.jpg", rotate_degrees=180)
flip_and_rotate("photo.jpg", "rotated_270.jpg", rotate_degrees=270)
flip_and_rotate("photo.jpg", "rotated_15.jpg", rotate_degrees=15, fill_color=(255, 255, 255))
# Fix a photo taken sideways (clockwise) using EXIF orientation
from PIL import ImageOps
def auto_orient(input_path: str, output_path: str) -> None:
"""Rotate image to match EXIF orientation tag, then strip EXIF."""
img = Image.open(input_path)
img = ImageOps.exif_transpose(img) # Auto-rotate based on EXIF tag
img.save(output_path)
print("Auto-oriented and saved:", output_path)
auto_orient("sideways_photo.jpg", "corrected.jpg")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.