forked from Chaostreff-Potsdam/erika3004
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_converter.py
54 lines (41 loc) · 1.71 KB
/
image_converter.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
import sys
from PIL import Image
class NotAnImageException(Exception):
pass
class WrappedImage:
def __init__(self, image_path, threshold=128):
"""
:param image_path: path to image that should be opened
:param threshold: threshold value - pixel is considered "set" if there is a grayscale-equivalent value
at this coordinate that is greater or equal to the given threshold
"""
try:
self.image = Image.open(image_path)
except FileNotFoundError:
raise FileNotFoundError("Exception when opening the file {} - file not found".format(image_path)) \
.with_traceback(sys.exc_info()[2])
except OSError:
# OSError - OS-specific! Results may vary among different operating systems
raise NotAnImageException("Exception when opening the file {} - maybe not an image?".format(image_path)) \
.with_traceback(sys.exc_info()[2])
self.pixels = self.image.load()
self.threshold = threshold
def is_grayscale(self):
return self.image.mode == 'L'
def is_rgb(self):
return self.image.mode == 'RGB' or self.image.mode == 'RGBA'
def is_pixel_set(self, x, y):
if self.is_grayscale():
return self.pixels[x, y] <= self.threshold
elif self.is_rgb():
pixel = self.pixels[x, y]
grayscale_value = (pixel[0] + pixel[1] + pixel[2]) // 3
return grayscale_value <= self.threshold
else:
raise Exception("Image type not supported")
def width(self):
return self.image.width
def height(self):
return self.image.height
def __exit__(self, *args):
self.image.close()