Skip to content

Instantly share code, notes, and snippets.

@oliver-batchelor
Created August 30, 2019 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliver-batchelor/469243ae2e02ef38a6ef614af9ca6ccc to your computer and use it in GitHub Desktop.
Save oliver-batchelor/469243ae2e02ef38a6ef614af9ca6ccc to your computer and use it in GitHub Desktop.
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