def test_rgb_view(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGB32) qimg.fill(QtGui.qRgb(23, 0, 0)) v = qimage2ndarray.rgb_view(qimg) qimg.setPixel(12, 10, QtGui.qRgb(12, 34, 56)) assert_equal(list(v[10, 10]), [23, 0, 0]) assert_equal(list(v[10, 12]), [12, 34, 56])
def test_scalar2qimage(): a = numpy.zeros((240, 320), dtype = float) a[12,10] = 42.42 a[13,10] = -10 qImg = qimage2ndarray.array2qimage(a) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_RGB32) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(42,42,42))) # max pixel assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(0,0,0))) # zero pixel assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0))) # min pixel
def test_RGBX64(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGBX64) qimg.fill(QtGui.qRgb(0, 0, 0)) v = _qimageview(qimg) qimg.setPixel(12, 10, QtGui.qRgb(0x12, 0x34, 0x56)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 0xffff000000000000 if sys.byteorder == 'little' else 0xffff) assert_equal( v[10, 12], 0xffff565634341212 if sys.byteorder == 'little' else 0x121234345656ffff) assert_equal(v.nbytes, numBytes(qimg))
def test_rgb2qimage(): a = numpy.zeros((240, 320, 3), dtype = float) a[12,10] = (42.42, 20, 14) a[13,10] = (-10, 0, -14) qImg = qimage2ndarray.array2qimage(a) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_RGB32) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(42,20,14))) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0))) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(0,0,0)))
def test_gray2qimage(): a = numpy.zeros((240, 320), dtype = float) a[12,10] = 42.42 a[13,10] = -10 qImg = qimage2ndarray.gray2qimage(a) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_Indexed8) assert_equal(a.nbytes, numBytes(qImg) * a.itemsize) assert_equal(numColors(qImg), 256) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(42,42,42))) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(0,0,0))) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0)))
def test_rgb2qimage_normalize(): a = numpy.zeros((240, 320, 3), dtype = float) a[12,10] = (42.42, 20, 14) a[13,10] = (-10, 20, 0) qImg = qimage2ndarray.array2qimage(a, normalize = True) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_RGB32) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(255,(255*30.0/52.42),(255*24/52.42)))) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,(255*30.0/52.42),(255*10/52.42)))) x = int(255 * 10.0 / 52.42) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(x,x,x))) # zero pixel
def test_scalar2qimage_normalize_domain(): a = numpy.zeros((240, 320), dtype = float) a[12,10] = 42.42 a[13,10] = -10 qImg = qimage2ndarray.array2qimage(a, normalize = (-100, 100)) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_RGB32) x = int(255 * 142.42 / 200.0) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(x,x,x))) x = int(255 * 90.0 / 200.0) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(x,x,x))) x = int(255 * 100.0 / 200.0) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(x,x,x)))
def test_bool2qimage_normalize(): a = numpy.zeros((240, 320), dtype = bool) a[12,10] = True # normalization should scale to 0/255 # (not raise a numpy exception, see issue #17) qImg = qimage2ndarray.gray2qimage(a, normalize = True) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(255,255,255))) assert_equal(hex(qImg.pixel(0,0)), hex(QtGui.qRgb(0,0,0))) a[:] = True qImg = qimage2ndarray.gray2qimage(a, normalize = True) # for boolean arrays, I would assume True should always map to 255 assert_equal(hex(qImg.pixel(0,0)), hex(QtGui.qRgb(255,255,255)))
def test_gray2qimage_normalize_onlymax(): a = numpy.zeros((240, 320), dtype = float) a[12,10] = 42.42 a[13,10] = -10 qImg = qimage2ndarray.gray2qimage(a, normalize = 80) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_Indexed8) assert_equal(a.nbytes, qImg.numBytes() * a.itemsize) assert_equal(qImg.numColors(), 256) x = int(255 * 42.42 / 80.0) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(x,x,x))) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0))) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(0,0,0)))
def test_alpha_view(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_ARGB32) qimg.fill(23) v = qimage2ndarray.alpha_view(qimg) qimg.setPixel(12, 10, QtGui.qRgb(12,34,56)) assert_equal(v[10,12], 255) assert_equal(v[10,11], 0)
def test_scalar2qimage_masked(): a = numpy.zeros((240, 320), dtype = float) a[12,10] = 42.42 a[13,10] = -10 a[:,160:] = 100 a = numpy.ma.masked_greater(a, 99) qImg = qimage2ndarray.array2qimage(a, normalize = True) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_ARGB32) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(255,255,255))) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0))) x = int(255 * 10.0 / 52.42) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(x,x,x))) assert_equal(QtGui.qAlpha(qImg.pixel(0,10)), 255) assert_equal(QtGui.qAlpha(qImg.pixel(200,10)), 0)
def test_gray2qimage_normalize_domain(): a = numpy.zeros((240, 320), dtype = float) a[12,10] = 42.42 a[13,10] = -10 qImg = qimage2ndarray.gray2qimage(a, normalize = (-100, 100)) assert not qImg.isNull() assert_equal(qImg.width(), 320) assert_equal(qImg.height(), 240) assert_equal(qImg.format(), QtGui.QImage.Format_Indexed8) assert_equal(a.nbytes, numBytes(qImg) * a.itemsize) assert_equal(numColors(qImg), 256) x = int(255 * 142.42 / 200.0) assert_equal(hex(qImg.pixel(10,12)), hex(QtGui.qRgb(x,x,x))) x = int(255 * 90.0 / 200.0) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(x,x,x))) x = int(255 * 100.0 / 200.0) assert_equal(hex(qImg.pixel(10,14)), hex(QtGui.qRgb(x,x,x)))
def test_RGBA8888(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGBA8888) qimg.fill(0) v = _qimageview(qimg) qimg.setPixel(12, 10, QtGui.qRgb(0x12, 0x34, 0x56)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 12], 0x123456ff if sys.byteorder == 'big' else 0xff563412) assert_equal(v.nbytes, numBytes(qimg))
def test_ARGB32(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_ARGB32) qimg.fill(0) v = _qimageview(qimg) qimg.setPixel(12, 10, QtGui.qRgb(0x12, 0x34, 0x56)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 12], 0xff123456 if sys.byteorder == 'little' else 0x563412ff) assert_equal(v.nbytes, numBytes(qimg))
def test_raw_rgb32(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGB32) qimg.fill(0) v = qimage2ndarray.raw_view(qimg) qimg.fill(23) qimg.setPixel(12, 10, QtGui.qRgb(0, 0, 42)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 23 | 0xff000000) assert_equal(v[10, 12], 42 | 0xff000000) assert_equal(v.nbytes, numBytes(qimg))
def test_raw_rgb16(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGB16) qimg.fill(0) v = qimage2ndarray.raw_view(qimg) qimg.fill(23) qimg.setPixel(12, 10, QtGui.qRgb(0, 0, 91)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 23) assert_equal(v[10, 12], 91 >> 3) assert_equal(v.nbytes, numBytes(qimg))
def test_raw_grayscale8(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Grayscale8) qimg.fill(0) v = qimage2ndarray.raw_view(qimg) qimg.fill(1) qimg.setPixel(12, 10, QtGui.qRgb(42, 42, 42)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 1) assert_equal(v[10, 12], 42) assert_equal(v.nbytes, numBytes(qimg))
def test_raw_rgba64(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGBA64) qimg.fill(0) v = qimage2ndarray.raw_view(qimg) qimg.fill(1) qimg.setPixel(12, 10, QtGui.qRgb(0x12, 0x34, 0x56)) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 0x010100000000) assert_equal(v[10, 12], 0xffff565634341212) assert_equal(v.nbytes, numBytes(qimg))
def test_recarray_view(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_ARGB32) qimg.fill(23) v = qimage2ndarray.recarray_view(qimg) qimg.setPixel(12, 10, QtGui.qRgb(12,34,56)) assert_equal(v["g"][10,12], 34) assert_equal(v["g"].sum(), 34) assert_equal(v["green"].sum(), 34) assert_equal(v.g[10,12], 34) # this worked in the past, but with NumPy 1.2.1, I get: # TypeError: function takes at most 2 arguments (3 given) assert_equal(v[10,12]["g"], 34)
def test_empty2qimage(): a = numpy.ones((240, 320), dtype = float) qImg = qimage2ndarray.gray2qimage(a, normalize = True) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0))) qImg = qimage2ndarray.array2qimage(a, normalize = True) assert_equal(hex(qImg.pixel(10,13)), hex(QtGui.qRgb(0,0,0)))
def test_rgb_view(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGB32) qimg.fill(23) v = qimage2ndarray.rgb_view(qimg) qimg.setPixel(12, 10, QtGui.qRgb(12,34,56)) assert_equal(list(v[10,12]), [12,34,56])