In [1]:
import numpy as np
from PIL import Image
In [2]:
im_gray = np.array(Image.open('data/src/lena_square_half.png').convert('L'))
print(type(im_gray))
In [3]:
thresh = 128
In [4]:
im_bool = im_gray > thresh
print(im_bool)
In [5]:
maxval = 255
In [6]:
im_bin = (im_gray > thresh) * maxval
print(im_bin)
In [7]:
Image.fromarray(np.uint8(im_bin)).save('data/dst/numpy_binarization.png')
In [8]:
im_bin_keep = (im_gray > thresh) * im_gray
print(im_bin_keep)
In [9]:
Image.fromarray(np.uint8(im_bin_keep)).save('data/dst/numpy_binarization_keep.png')
In [10]:
im_bool = im_gray > 128
im_dst = np.empty((*im_gray.shape, 3))
r, g, b = 255, 128, 32
im_dst[:, :, 0] = im_bool * r
im_dst[:, :, 1] = im_bool * g
im_dst[:, :, 2] = im_bool * b
In [11]:
Image.fromarray(np.uint8(im_dst)).save('data/dst/numpy_binarization_color.png')
In [12]:
im_bool = im_gray > 128
im_dst = np.empty((*im_gray.shape, 3))
r, g, b = 128, 160, 192
im_dst[:, :, 0] = im_bool * r
im_dst[:, :, 1] = ~im_bool * g
im_dst[:, :, 2] = im_bool * b
In [13]:
Image.fromarray(np.uint8(im_dst)).save('data/dst/numpy_binarization_color2.png')
In [14]:
im = np.array(Image.open('data/src/lena_square_half.png'))
In [15]:
im_th = np.empty_like(im)
In [16]:
thresh = 128
maxval = 255
for i in range(3):
im_th[:, :, i] = (im[:, :, i] > thresh) * maxval
In [17]:
Image.fromarray(np.uint8(im_th)).save('data/dst/numpy_binarization_from_color.png')
In [18]:
l_thresh = [64, 128, 192]
l_maxval = [64, 128, 192]
for i, thresh, maxval in zip(range(3), l_thresh, l_maxval):
im_th[:, :, i] = (im[:, :, i] > thresh) * maxval
In [19]:
Image.fromarray(np.uint8(im_th)).save('data/dst/numpy_binarization_from_color2.png')