Skip to content

Commit

Permalink
add test and rework tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Oct 26, 2023
1 parent 0166a2c commit 14af113
Showing 1 changed file with 46 additions and 37 deletions.
83 changes: 46 additions & 37 deletions src/silx/gui/utils/test/test_image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# /*##########################################################################
#
# Copyright (c) 2017-2018 European Synchrotron Radiation Facility
# Copyright (c) 2017-2023 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -28,50 +28,59 @@
__date__ = "16/01/2017"

import numpy
import pytest

from silx.gui import qt
from silx.utils.testutils import ParametricTestCase
from silx.gui.utils.testutils import TestCaseQt
from silx.gui.utils.image import convertArrayToQImage, convertQImageToArray


class TestQImageConversion(TestCaseQt, ParametricTestCase):
"""Tests conversion of QImage to/from numpy array."""
@pytest.mark.parametrize(
"format_, channels",
[
(qt.QImage.Format_RGB888, 3), # Native support
(qt.QImage.Format_ARGB32, 4), # Native support
]
)
def testConvertArrayToQImage(format_, channels):
"""Test conversion of numpy array to QImage"""
image = numpy.arange(
3*3*channels, dtype=numpy.uint8).reshape(3, 3, channels)
qimage = convertArrayToQImage(image)

def testConvertArrayToQImage(self):
"""Test conversion of numpy array to QImage"""
for format_, channels in [('Format_RGB888', 3),
('Format_ARGB32', 4)]:
with self.subTest(format_):
image = numpy.arange(
3*3*channels, dtype=numpy.uint8).reshape(3, 3, channels)
qimage = convertArrayToQImage(image)
assert (qimage.height(), qimage.width()) == image.shape[:2]
assert qimage.format() == format_

self.assertEqual(qimage.height(), image.shape[0])
self.assertEqual(qimage.width(), image.shape[1])
self.assertEqual(qimage.format(), getattr(qt.QImage, format_))
for row in range(3):
for col in range(3):
# Qrgb has no alpha channel, not compared
# Qt uses x,y while array is row,col...
assert qt.QColor(qimage.pixel(col, row)) == qt.QColor(*image[row, col, :3])

for row in range(3):
for col in range(3):
# Qrgb has no alpha channel, not compared
# Qt uses x,y while array is row,col...
self.assertEqual(qt.QColor(qimage.pixel(col, row)),
qt.QColor(*image[row, col, :3]))

@pytest.mark.parametrize(
"format_, channels",
[
(qt.QImage.Format_RGB888, 3), # Native support
(qt.QImage.Format_ARGB32, 4), # Native support
(qt.QImage.Format_RGB32, 3), # Conversion to RGB
]
)
def testConvertQImageToArray(format_, channels):
"""Test conversion of QImage to numpy array"""
color = numpy.arange(channels) # RGB(A) values
qimage = qt.QImage(3, 3, format_)
qimage.fill(qt.QColor(*color))
image = convertQImageToArray(qimage)

def testConvertQImageToArray(self):
"""Test conversion of QImage to numpy array"""
for format_, channels in [
('Format_RGB888', 3), # Native support
('Format_ARGB32', 4), # Native support
('Format_RGB32', 3)]: # Conversion to RGB
with self.subTest(format_):
color = numpy.arange(channels) # RGB(A) values
qimage = qt.QImage(3, 3, getattr(qt.QImage, format_))
qimage.fill(qt.QColor(*color))
image = convertQImageToArray(qimage)
assert (qimage.height(), qimage.width(), len(color)) == image.shape
assert numpy.all(numpy.equal(image, color))

self.assertEqual(qimage.height(), image.shape[0])
self.assertEqual(qimage.width(), image.shape[1])
self.assertEqual(image.shape[2], len(color))
self.assertTrue(numpy.all(numpy.equal(image, color)))

def testConvertQImageToArrayGrayscale():
"""Test conversion of grayscale QImage to numpy array"""
qimage = qt.QImage(3, 3, qt.QImage.Format_Grayscale8)
qimage.fill(1)
image = convertQImageToArray(qimage)

assert (qimage.height(), qimage.width()) == image.shape
assert numpy.all(numpy.equal(image, 1))

0 comments on commit 14af113

Please sign in to comment.