-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy2qimage.py
More file actions
114 lines (99 loc) · 4.38 KB
/
numpy2qimage.py
File metadata and controls
114 lines (99 loc) · 4.38 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
Functions to convert from an ndarray to a QImage.
Modified from:
http://femr.googlecode.com/hg-history/e06e4336439755f075dc693acaffb72093fd8c45/src/contrib/qimage2ndarray.py
'''
import numpy as numpy
from PyQt5 import QtGui
# -- Convert from ndarray to QImage --
def numpy2qimage(array):
if numpy.ndim(array) == 2:
return gray2qimage(array)
elif numpy.ndim(array) == 3:
return rgb2qimage(array)
raise ValueError("can only convert 2D or 3D arrays")
def gray2qimage(gray):
'''
Convert numpy array to QtImage
'''
if len(gray.shape) != 2:
raise ValueError("gray2QImage can only convert 2D arrays")
h, w = gray.shape
if gray.dtype == numpy.uint8:
gray = numpy.require(gray, numpy.uint8, 'C')
result = QtGui.QImage(gray.data, w, h, w, QtGui.QImage.Format_Indexed8)
result.ndarray = gray
for i in range(256):
result.setColor(i, QtGui.QColor(i, i, i).rgb())
elif gray.dtype == numpy.uint16:
# This assumes that a 16bit image is only 12bit resolution: 2^16-2^12=16
gray = (gray/16).astype(numpy.uint8)
gray = numpy.require(gray, numpy.uint8, 'C')
h, w = gray.shape
result = QtGui.QImage(gray.data, w, h, QtGui.QImage.Format_Indexed8)
result.ndarray = gray
#for i in range(256):
# result.setColor(i, QtGui.QColor(i, i, i).rgb())
else:
# Convert by multiplying by 256 and making it uint8
#print gray * 2**(8-12)
#gray = (gray * 256).astype(numpy.uint8)
gray = (gray * 2**(8-12)).astype(numpy.uint8)
gray = numpy.require(gray, numpy.uint8, 'C')
h, w = gray.shape
result = QtGui.QImage(gray.data, w, h, QtGui.QImage.Format_Indexed8)
result.ndarray = gray
for i in range(256):
result.setColor(i, QtGui.QColor(i, i, i).rgb())
return result
def OLDgray2qimage(gray):
"""Convert the 2D numpy array `gray` into a 8-bit QImage with a gray
colormap. The first dimension represents the vertical image axis.
ATTENTION: This QImage carries an attribute `ndimage` with a
reference to the underlying numpy array that holds the data. On
Windows, the conversion into a QPixmap does not copy the data, so
that you have to take care that the QImage does not get garbage
collected (otherwise PyQt will throw away the wrapper, effectively
freeing the underlying memory - boom!)."""
if len(gray.shape) != 2:
raise ValueError("gray2QImage can only convert 2D arrays")
# If the type is not numpy.uint8, then convert by multiplying by 256
if type(gray[0][0]) == numpy.float64:
gray = (gray * 256).astype(numpy.uint8)
gray = numpy.require(gray, numpy.uint8, 'C')
h, w = gray.shape
result = QtGui.QImage(gray.data, w, h, QtGui.QImage.Format_Indexed8)
result.ndarray = gray
for i in range(256):
result.setColor(i, QtGui.QColor(i, i, i).rgb())
return result
def rgb2qimage(rgb):
"""Convert the 3D numpy array `rgb` into a 32-bit QImage. `rgb` must
have three dimensions with the vertical, horizontal and RGB image axes.
ATTENTION: This QImage carries an attribute `ndimage` with a
reference to the underlying numpy array that holds the data. On
Windows, the conversion into a QPixmap does not copy the data, so
that you have to take care that the QImage does not get garbage
collected (otherwise PyQt will throw away the wrapper, effectively
freeing the underlying memory - boom!)."""
if len(rgb.shape) != 3:
raise ValueError("rgb2QImage can only convert 3D arrays")
if rgb.shape[2] not in (3, 4):
raise ValueError("rgb2QImage can expects the last dimension to contain exactly three (R,G,B) or four (R,G,B,A) channels")
h, w, channels = rgb.shape
# Qt expects 32bit BGRA data for color images:
bgra = numpy.empty((h, w, 4), numpy.uint8, 'C')
bgra[...,0] = rgb[...,2]
bgra[...,1] = rgb[...,1]
bgra[...,2] = rgb[...,0]
if rgb.shape[2] == 3:
bgra[...,3].fill(255)
fmt = QtGui.QImage.Format_RGB32
else:
bgra[...,3] = rgb[...,3]
fmt = QtGui.QImage.Format_ARGB32
result = QtGui.QImage(bgra.data, w, h, fmt)
result.ndarray = bgra
return result