Head vs breakz
[code] - 이미지에서 색상 투명화 하기 본문
이미지파일은 PNG 파일이여야 한다. JPG로 했다가 안된다는 걸 늦게 보았다...
from PIL import Image
for i in range(1,1001):
im = Image.open('./image_test/'+str(i)+'.jpg')
im.save(str(i)+'.png')
img = Image.open(str(i)+'.png')
img = img.convert("RGBA")
datas =img.getdata()
newData = []
cutOff = 50
for item in datas:
if item[0] < cutOff and item[1] < cutOff and item[2] < cutOff:
newData.append((0, 0, 0, 0))
# RGB의 각 요소가 모두 cutOff 이상이면 transparent하게 바꿔줍니다.
# elif item[0] == 191 and item[1] == 206 and item[2] == 225:
# newData.append((0, 0, 0, 0))
# elif item[0] == 219 and item[1] == 211 and item[2] == 224:
# newData.append((0, 0, 0, 0))
# elif item[0] == 199 and item[1] == 191 and item[2] == 204:
# newData.append((0, 0, 0, 0))
# elif item[0] == 189 and item[1] == 205 and item[2] == 228:
# newData.append((0, 0, 0, 0))
# elif item[0] == 194 and item[1] == 206 and item[2] == 222:
# newData.append((0, 0, 0, 0))
# elif 185 <= item[0] and item[0] <= 220 and 185 <= item[1] and item[1] <= 210 and 200 <= item[2] and item[2] <= 225 :
# newData.append((0, 0, 0, 0))
else:
newData.append(item)
img.putdata(newData)
img.save('./test_image/'+str(i)+'.png', "PNG")
현재 code는 RGB값이 50 미만일 경우 (0,0,0,0)의 투명으로 바꿔주는 code이다. elif를 사용한 부분처럼 원하는 RGB값을 지정할 경우 (0,0,0,0)의 값으로 바꿀수 있다. 현재 이미지의 상태가 RGB인지, RGBA 인지 꼭 확인을 하고 투명으로 바꾸는 과정을 해야한다. RGB값일 경우 convert를 통해 RGBA로 바꾸는 code를 추가하여 실행하면 된다.
위의 코드를 실행 시킬 경우
'Head > Code' 카테고리의 다른 글
[code] - 카카오 크레인 인형뽑기 python (0) | 2020.05.02 |
---|---|
[code] - 이미지에서 글씨 찾기 구글 API / windows에서 (0) | 2020.04.20 |
[code] - OpenCV GrabCut (0) | 2020.04.16 |
[code] - 백준 수학 1193,2869,10250,2775,1712,2839,2292 python (0) | 2020.04.08 |
[code] - 백준 문자열 11654,11720,10809,2675,1157,1152,2908,5622,2941,1316번 python (0) | 2020.04.04 |
Comments