def test_data_access(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Indexed8) setNumColors(qimg, 256) qimg.fill(42) v = _qimageview(qimg) assert_equal(v.shape, (240, 320)) assert_equal(v[10,10], 42) assert_equal(v.nbytes, numBytes(qimg))
def test_data_access(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Indexed8) setNumColors(qimg, 256) qimg.fill(42) v = _qimageview(qimg) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 42) 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, 42) assert_equal(v.shape, (240, 320)) assert_equal(v[10,12], 42) 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, 42) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 12], 42) assert_equal(v.nbytes, numBytes(qimg))
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_byte_view_rgb32(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_RGB32) v = qimage2ndarray.byte_view(qimg) qimg.fill(23) qimg.setPixel(12, 10, 42) assert_equal(v.shape, (240, 320, 4)) assert_equal(list(v[10,10]), [23, 0, 0, 0xff]) assert_equal(list(v[10,12]), [42, 0, 0, 0xff]) 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_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_byte_view_indexed(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Indexed8) setNumColors(qimg, 256) v = qimage2ndarray.byte_view(qimg) qimg.fill(23) qimg.setPixel(12, 10, 42) assert_equal(v.shape, (240, 320, 1)) assert_equal(list(v[10,10]), [23]) assert_equal(list(v[10,12]), [42]) 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, 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_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_coordinate_access(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Indexed8) setNumColors(qimg, 256) qimg.fill(0) v = _qimageview(qimg) qimg.fill(23) qimg.setPixel(12, 10, 42) assert_equal(v.shape, (240, 320)) assert_equal(v[10, 10], 23) assert_equal(v[10, 12], 42) assert_equal(v.nbytes, numBytes(qimg))
def test_raw_indexed8(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Indexed8) setNumColors(qimg, 256) qimg.fill(0) v = qimage2ndarray.raw_view(qimg) qimg.fill(23) qimg.setPixel(12, 10, 42) assert_equal(v.shape, (240, 320)) assert_equal(v[10,10], 23) assert_equal(v[10,12], 42) assert_equal(v.nbytes, numBytes(qimg))
def test_coordinate_access(): qimg = QtGui.QImage(320, 240, QtGui.QImage.Format_Indexed8) setNumColors(qimg, 256) qimg.fill(0) v = _qimageview(qimg) qimg.fill(23) qimg.setPixel(12, 10, 42) assert_equal(v.shape, (240, 320)) assert_equal(v[10,10], 23) assert_equal(v[10,12], 42) assert_equal(v.nbytes, numBytes(qimg))
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_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_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_gray2qimage_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.gray2qimage(a, normalize = True) 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(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)