-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreshold_manual_image.py
71 lines (65 loc) · 2.33 KB
/
threshold_manual_image.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from PIL import Image
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import math
image1 = input('Enter image name with extension: ')
#im = Image.open('shot7.tif')
im = Image.open(image1)
#im = Image.open('E:/Biology/Pancreas/samples/' + str(image1))
#im = Image.open('GTEX-1117F-1726 (1).svs')
#im.show()
imarray = np.array(im)
print(imarray.shape, imarray.size, len(imarray))
clr_resp = input("Enter 'y' if you want to use default manually tagged color value (R,G,B: 255,255,0): ") # User color response
if clr_resp != 'y':
r_val = int(input('Enter value for Red channel: '))
g_val = int(input('Enter value for Green channel: '))
b_val = int(input('Enter value for Blue channel: '))
i1 = 0
while (i1 < len(imarray)):
i2 = 0
while (i2 < len(imarray[i1])):
if imarray[i1][i2][0] == r_val and imarray[i1][i2][1] == g_val and imarray[i1][i2][2] == b_val:
imarray[i1][i2][0] = int(255)
imarray[i1][i2][1] = int(255)
imarray[i1][i2][2] = int(255)
else:
imarray[i1][i2][0] = int(0)
imarray[i1][i2][1] = int(0)
imarray[i1][i2][2] = int(0)
i2 = i2 + 1
i1 = i1 + 1
else:
i1 = 0
while (i1 < len(imarray)):
i2 = 0
while (i2 < len(imarray[i1])):
if imarray[i1][i2][0] == 255 and imarray[i1][i2][1] == 255 and imarray[i1][i2][2] == 0:
imarray[i1][i2][0] = int(255)
imarray[i1][i2][1] = int(255)
imarray[i1][i2][2] = int(255)
else:
imarray[i1][i2][0] = int(0)
imarray[i1][i2][1] = int(0)
imarray[i1][i2][2] = int(0)
i2 = i2 + 1
i1 = i1 + 1
data = Image.fromarray(imarray)
print('Done')
image1_name = ''
image1_exten = ''
i = len(image1) - 1
while (image1[i] != '.'):
image1_exten = image1_exten + image1[i]
i = i - 1
image1_exten = image1_exten[::-1]
i = len(image1) - len(image1_exten) - 1 # (-1 for '.')
i = i - 1 # since range = 0 -> length - 1
while (i >= 0):
image1_name = image1_name + image1[i]
i = i - 1
image1_name = image1_name[::-1]
image1_name = image1_name + '_bi.' + image1_exten
data.save(image1_name)
#data.save('shot7_bi2.tif')