This repository was archived by the owner on Jan 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageObject.py
More file actions
129 lines (100 loc) · 2.84 KB
/
ImageObject.py
File metadata and controls
129 lines (100 loc) · 2.84 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding:utf-8 -*-
# pyview
# Picture operation helper
"""
Helper functions for image handling
"""
import sys
from PIL import Image, ImagePalette, ImageQt
from PyQt4 import QtGui, QtCore
class ImageObject():
def __init__(self):
self.image = None
return
def pilimage(self):
"""Returns the image representation"""
return self.image
def getsize(self):
"""
Returns the dimensions of the image as a tuple {width, height}
"""
return self.image.size
def getPixel(self, x, y):
"""Gets the pixel at x,y as a 3-tuple"""
# TODO: convert indexed colors to RGB
value = self.image.getpixel((x, y))
value = self.convertColorToRGB(value)
return value
# Converts a color to RGB
def convertColorToRGB(self, color):
"""Converts a color to RGB"""
# Get the image mode
mode = self.image.mode
# If the color is in RGBA mode, skip the alpha
if mode == "RGBA":
return color[0:3]
# If the color is RGB, return it
if mode == "RGB":
return color
# palette image
if mode == "P":
value = self.lut[color]
return value
# grayscale
if mode == "L":
return color,color,color
# bitmaps
if mode == "1":
if color==1:
return 255,255,255
else:
return 0,0,0
# Return something
return None
# Loads an image from a file
def loadImageFromFile(self, filePath):
"""Loads an image from a file and returns a PIL image"""
image = Image.open(str(filePath))
# If it is a palette image, load the LUT
if image.mode == "P":
lut = image.resize((256, 1))
lut.putdata(range(256))
lut = lut.convert("RGB").getdata()
self.lut = lut
else:
self.lut = None
# Set the image and return
self.image = image
return
# Converts a PIL image to a Qt image
# Kudos to: http://mail.python.org/pipermail/image-sig/2004-September/002908.html
def convertToQtImageEx(self, encoder="jpeg", mode="RGB"):
"""
Converts a PIL image to a Qt image.
Optional parameters "encoder" and "mode" determine which method will
be used to convert the images.
The function either returns a QImage or None, in case of an error
"""
# Convert the PIL image to a string
PILstring = self.image.convert(mode).tostring(encoder, mode)
if not PILstring: return None
# Convert the string to a QImage
qImage = QtGui.QImage()
qImage.loadFromData(QtCore.QByteArray(PILstring))
# Return the image
return qImage
# Converts a PIL image to a Qt image
def convertToQtImage(self):
"""
Converts a PIL image to a Qt image.
"""
# TODO Check PIL version and fallback to the other
# function if VERSION < 1.1.6
# ImageQt method seems to have problems with some GIFs!
if self.image.mode == "P":
return self.convertToQtImageEx()
return ImageQt.ImageQt(self.image)
# Gets the image's dimensions
def getSize(self):
"""Gets the dimensions of the loaded image"""
return self.image.size