-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_process.py
97 lines (85 loc) · 3.98 KB
/
image_process.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
"""
########################################################################################################################
# Image Dataset Generator #
# Celestial Tech Co. #
# Copyright © 2015-2017 #
# For more check : www.tianshicangxie.com #
########################################################################################################################
Before Using Please Read README File for tutorial and help
"""
# Code in UFT-8
import psutil
import numpy as np
import toolfuncs as tool
from PIL import Image
# Read a single pixel data (RGB) from a image file
# Return : numpy.array(shape=[3])
def read_pixel_from_image(self, image_dir, x, y):
if tool.is_path_valid(image_dir):
with Image.open(image_dir) as tempimage:
if self.__image_show is True:
tempimage.show()
if self.__image_reshape is True:
tempimage = tempimage.resize(self.__image_std_shape)
r, g, b = tempimage.getpixel((x, y))
result = np.array([r, g, b])
return result
# Read a single channel from a image file
# Return : numpy.matrix(shape=[imagelength, imagewidth])
def read_channel_from_image(self, image_dir, channel_name):
if tool.is_path_valid(image_dir):
with Image.open(image_dir) as tempimage:
if self.__image_show is True:
tempimage.show()
if self.__image_reshape is True:
tempimage = tempimage.resize(self.__image_std_shape)
if channel_name in self.__channel_r_name:
r, g, b = tempimage.split()
data = np.matrix(r)
return data
elif channel_name in self.__channel_g_name:
r, g, b = tempimage.split()
data = np.matrix(g)
return data
elif channel_name in self.__channel_b_name:
r, g, b = tempimage.split()
data = np.matrix(b)
return data
else:
raise Exception('ERROR : Invalid Image Channel Name!')
# Read image data from jpeg file
# Return : numpy.matrix(shape=[imagelength, imagewidth, 3])
def read_rgb_from_image(self, image_dir):
if tool.is_path_valid(image_dir):
with Image.open(image_dir) as tempimage:
if self.__image_show is True:
tempimage.show()
if self.__image_reshape is True:
tempimage = tempimage.resize(self.__image_std_shape)
if self.__image_reshape is True:
data = np.zeros(shape=[self.__image_std_length, self.__image_std_width, self.__image_std_channel])
for x in range(self.__image_std_length):
for y in range(self.__image_std_width):
r, g, b = tempimage.getpixel((x, y))
data[x, y, 0] = r
data[x, y, 1] = g
data[x, y, 2] = b
return data
else:
length, width = tempimage.size()
data = np.zeros(shape=[length, width, 3])
for x in range(length):
for y in range(width):
r, g, b = tempimage.getpixel((x, y))
data[x, y, 0] = r
data[x, y, 1] = g
data[x, y, 2] = b
return data
# Close the window built by Image.show() method (Pillow package)
# Args : None
# Return : None
def kill_image_window(self):
if self.__image_show is True:
for process in psutil.process_iter():
if process.name() == "display":
process.kill()