-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_color.py
31 lines (26 loc) · 911 Bytes
/
example_color.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import glob
import numpy as np
import cv2
import os
from PIL import Image
for infile in glob.glob("I/*"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.load()
data = np.asarray(im)
# define the list of boundaries
boundaries = [
([141,85,36], [255,224,189])] #: a list of lower limits and a list of upper limits.
# loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")
# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(data, lower, upper)
output = cv2.bitwise_and(data, data, mask=mask)
# show the images
#cv2.imshow("images", np.hstack([data, output]))
im = Image.fromarray(output, "RGB")
im.show()