From 14af113170b5b654910c6aeda1b2c275d6891d72 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 26 Oct 2023 17:42:42 +0200 Subject: [PATCH] add test and rework tests to pytest --- src/silx/gui/utils/test/test_image.py | 83 +++++++++++++++------------ 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/src/silx/gui/utils/test/test_image.py b/src/silx/gui/utils/test/test_image.py index dc0ae54e9a..6158020eac 100644 --- a/src/silx/gui/utils/test/test_image.py +++ b/src/silx/gui/utils/test/test_image.py @@ -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 @@ -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))