Skip to content

Instantly share code, notes, and snippets.

@rkdgusrn1212
Created May 22, 2019 10:58
Show Gist options
  • Save rkdgusrn1212/e0028f08c0031e48432f29474b6bf595 to your computer and use it in GitHub Desktop.
Save rkdgusrn1212/e0028f08c0031e48432f29474b6bf595 to your computer and use it in GitHub Desktop.
Image Scaling using Pillow in Python
from PIL import Image
import numpy as np
img_path = input("변환시킬 이미지의 경로를 입력하세요 : ")#크기를 변환할 이미지 경로 입력.
img = Image.open(img_path)
img.show("변환 전 이미지")
[height, width, band] = np.shape(img)# 이미지의 row, col, 채널 수
height_rate = float(input("새로방향 변환 비율 : "))
width_rate = float(input("가로방향 변환 비율 : "))
new_height = int(height*height_rate) #새 이미지의 새로
new_width = int(width*width_rate) #새 이미지의 가로
new_img = Image.new("RGB",(new_width, new_height)) #새 이미지 생성(순방향)
new_img_rev = Image.new("RGB",(new_width, new_height)) #새 이미지 생성(역방향)
#순방향으로 이미지 맵핑하기
for i in range(0, height):
new_y = int(i*height_rate)#새 이미지의 y값 찾기
for j in range(0, width):
new_x = int(j*width_rate)#새 이미지의 x값 찾기
new_img.putpixel((new_x, new_y), img.getpixel((j,i)))
new_img.show()
new_img.save("sized_image.png")
#역방향으로 이미지 맵핑하기
for i in range(0, new_height):
ori_y = int(i/height_rate)#기존 y값 찾기
for j in range(0, new_width):
ori_x = int(j/width_rate)#기존 x값 찾기
new_img_rev.putpixel((j, i), img.getpixel((ori_x,ori_y)))
new_img_rev.show()
new_img_rev.save("rev_sized_image.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment