Skip to content

Instantly share code, notes, and snippets.

@yunsu3042
Created November 17, 2016 13:01
Show Gist options
  • Save yunsu3042/9c27db0aa6b62aba73d131b41beab3a9 to your computer and use it in GitHub Desktop.
Save yunsu3042/9c27db0aa6b62aba73d131b41beab3a9 to your computer and use it in GitHub Desktop.
#이미지를 받아, 한가지 인자만 갖는 흑백 어레이 리턴
##사이즈와 이미지 url을 받아, 이미지를 배열로 바꾼후, RGB 세인자를 적절한 계산을 통해 하나의 값으로 바꿔준다.
import numpy as np
def img_into_array(img_url,size)
img = Image.open(img_url)
img.thumbnail(size)
width,height = img.size
img_list = np.array(img).reshape(height,width,3)
img_gray = np.zeros((height,width),np.uint8)
# 이미지 배열을 흑백화 하는 과정 img = PIL.Image.open("foo.jpg").convert("L")
#와 비슷함
for x in range(0,height):
for y in range(0,width):
a = img_list[x,y,0]
b = img_list[x,y,1]
c = img_list[x,y,2]
gray = a*0.2989 + b*0.5870 + c*0.1140
if gray >255:
gray = 255
img_gray[x,y] = gray
return img_gray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment