-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_values.py
101 lines (83 loc) · 2.77 KB
/
read_values.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from PIL import Image
import numpy, math
import scipy.misc as smp
import sys, os
# Size, always square
size = int(sys.argv[1])
# X value of source tile
x_val = int(sys.argv[2])
# Y value of source tile
y_val = int(sys.argv[3])
# Max Z value of source tile
z_val = int(sys.argv[4])
# X value to start crop
crop_x = int(sys.argv[5])
# Y value to start crop
crop_y = int(sys.argv[6])
# range conversion - not so effective, but could be used elsewhere...
def convert(old_value):
old_max = 1
old_min = 0
new_max = 255
new_min = 0
old_range = old_max - old_min
new_range = new_max - new_min
value = (((old_value - old_min) * new_range) / old_range) + new_min
return int(value)
# Find minimum and maximum of image
def find_min_max(array):
mins_maxs = []
for line in array:
mins_maxs.append(min(line))
mins_maxs.append(max(line))
return min(mins_maxs), max(mins_maxs)
# Removing 'nan' from lists - replacing with 0.0 for now, creating new array
def normalise(array):
data = []
for line in array:
new_items = [x if not math.isnan(x) else 0 for x in line]
data.append(new_items)
return data
# converting TIFF into arrays that we can work with
def process_image(x, y, z):
# Open image, convert to numpy array
img = Image.open('data/voxels/all_mk/vox390.MK.'+str(x)+'.'+str(y)+'.'+str(z)+'.tif')
numpy_array = numpy.array(img)
# image_array contains 260 arrays of values!
image_array = normalise(numpy_array)
return image_array
# create an array of each image for this section
images = [process_image(x_val, y_val, x) for x in range(0,z_val + 1)]
def plot_cross(images, l):
# creating new grayscale image
new = Image.new('L', (260, 260))
# enumerate over images
for i, image in enumerate(images):
# grab 'line' of image
line = image[l]
for x, value in enumerate(line):
# place pixel in x position, at image index, with density value
new.putpixel((x, 259 - i), (value * (255 * 5)))
new.save("data/voxels/cross/mk-7-23/mk-7-23-{0:04d}.png".format(l))
return
def png_convert(images):
new = Image.new('RGBA', (260, 260))
for i, image in enumerate(images):
for x, line in enumerate(image):
for y, value in enumerate(line):
new.putpixel((x, y), ((convert(value)), (convert(value)), (convert(value)), (convert(value))))
# w, h = new.size
# new_image = new.crop((crop_x, crop_y, crop_x + size, crop_y + size))
# resized = new_image.resize((50, 50), Image.ANTIALIAS)
directory = "data/voxels/png/mk-"+str(x_val)+"-"+str(y_val)+"/crop/"+str(size) + str(crop_x) + str(crop_y)
if not os.path.exists(directory):
os.makedirs(directory)
filename = "mk"+str(i)+".png"
print directory, filename
new.save(directory + "/" + filename)
print "Saved!"
# creates 260 images
# for i in range(0, 259):
# # i = 'line' of image
# png_convert(images, i)
png_convert(images)