Created
August 30, 2019 06:59
-
-
Save oliver-batchelor/469243ae2e02ef38a6ef614af9ca6ccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import os.path as path | |
import os | |
from matplotlib import pyplot as plt | |
def valid(file): | |
_, ext = path.splitext(file) | |
return ext in ['.jpg', '.png', '.jpeg'] | |
def process(filename): | |
img = cv2.imread(filename) | |
blur = cv2.GaussianBlur(img, (15, 15), 0) | |
mask = cv2.inRange(blur, (250, 60, 10), (255, 255, 250)) | |
mask = cv2.bitwise_and(cv2.medianBlur(mask, 25), mask) | |
mask = np.where(mask, 0, 255) | |
rgba = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA) | |
rgba[:, :, 3] = mask | |
base, ext = path.splitext(filename) | |
cv2.imwrite(path.join("masked", base + ".png"), rgba) | |
images = [process(file) for file in os.listdir(".") if valid(file)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment