728x90
반응형
remain only text or black thing.
remove backgrounds
배경은 삭제하고, 검은 영역만 남기기
import cv2
import numpy as np
from PIL import Image, ImageFilter, ImageOps
import glob
list = glob.glob("./*")
list.remove("./transparent.py")
# want to make transparent image.
# turn background as transparent.
# only remain black text.
for item in list:
img = Image.open(item).convert("RGBA")
nm = item.split("/")[-1]
datas = img.getdata()
newData = []
for item in datas:
if (item[0] in range(150)) or (item[1] in range(150)) or (item[2] in range(150)):
# if item[0] == 0 and item[1] == 0 and item[2] == 0:
newData.append((0, 0, 0, 255))
else:
newData.append((255, 255, 255, 0))
img.putdata(newData)
img.save("./trans_{}".format(nm))
728x90
반응형
'사소한 Tip . 오류 해결법 > python' 카테고리의 다른 글
[pandas / DataFrame] MultiIndex df.to_excel 저장 시 빈 행(row) 삭제하기 (0) | 2024.01.03 |
---|---|
[python/PIL] image to pdf convert (0) | 2023.10.17 |
[python] open() got an unexpected keyword argument 'encoding' / python 3.9 (0) | 2023.09.06 |
[python] 클래스 별 랜덤 컬러 정의 (0) | 2023.08.23 |
[streamlit] OSError: [Errno 28] inotify watch limit reached (0) | 2023.08.17 |