From 43e2c92802dbb71dd0d5fe10afdd0d566b79aaf2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 19 Jun 2015 15:35:56 +1000 Subject: [PATCH] Removed unused imports --- PIL/ImageQt.py | 47 ++++++++++++++++++--------------- Tests/test_image_fromqimage.py | 7 ++--- Tests/test_image_fromqpixmap.py | 10 +++---- Tests/test_image_toqimage.py | 1 + Tests/test_image_toqpixmap.py | 3 ++- Tests/test_imageqt.py | 31 +++++++++++++--------- 6 files changed, 52 insertions(+), 47 deletions(-) diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index 480564481..95819029d 100644 --- a/PIL/ImageQt.py +++ b/PIL/ImageQt.py @@ -18,20 +18,23 @@ import PIL from PIL._util import isPath -import sys qt_is_installed = True +qt_version = None try: - from PyQt5.QtGui import QGuiApplication, QImage, qRgb, qRgba, QPixmap + from PyQt5.QtGui import QImage, qRgba, QPixmap from PyQt5.QtCore import QBuffer, QIODevice + qt_version = '5' except ImportError: try: - from PyQt4.QtGui import QGuiApplication, QImage, qRgb, qRgba, QPixmap + from PyQt4.QtGui import QImage, qRgba, QPixmap from PyQt4.QtCore import QBuffer, QIODevice + qt_version = '4' except ImportError: try: - from PySide.QtGui import QGuiApplication, QImage, qRgb, qRgba, QPixmap + from PySide.QtGui import QImage, qRgba, QPixmap from PySide.QtCore import QBuffer, QIODevice + qt_version = 'side' except ImportError: qt_is_installed = False @@ -123,23 +126,6 @@ def _toqclass_helper(im): 'data': __data, 'im': im, 'format': format, 'colortable': colortable } - -def toqimage(im): - return ImageQt(im) - - -def toqpixmap(im): - # This doesn't work. For now using a dumb approach. - # im_data = _toqclass_helper(im) - # result = QPixmap(im_data['im'].size[0], im_data['im'].size[1]) - # result.loadFromData(im_data['data']) - # Fix some strange bug that causes - if im.mode == 'RGB': - im = im.convert('RGBA') - qimage = im.toqimage() - qimage.save('/tmp/hopper_{}_qpixmap_qimage.png'.format(im.mode)) - return QPixmap.fromImage(qimage) - ## # An PIL image wrapper for Qt. This is a subclass of PyQt4's QImage # class. @@ -157,4 +143,21 @@ if qt_is_installed: im_data['format'] ) if im_data['colortable']: - self.setColorTable(im_data['colortable']) \ No newline at end of file + self.setColorTable(im_data['colortable']) + + +def toqimage(im): + return ImageQt(im) + + +def toqpixmap(im): + # This doesn't work. For now using a dumb approach. + # im_data = _toqclass_helper(im) + # result = QPixmap(im_data['im'].size[0], im_data['im'].size[1]) + # result.loadFromData(im_data['data']) + # Fix some strange bug that causes + if im.mode == 'RGB': + im = im.convert('RGBA') + qimage = toqimage(im) + qimage.save('/tmp/hopper_{}_qpixmap_qimage.png'.format(im.mode)) + return QPixmap.fromImage(qimage) \ No newline at end of file diff --git a/Tests/test_image_fromqimage.py b/Tests/test_image_fromqimage.py index 56dac0c66..957ec9cc1 100644 --- a/Tests/test_image_fromqimage.py +++ b/Tests/test_image_fromqimage.py @@ -1,16 +1,13 @@ from helper import unittest, PillowTestCase, hopper from test_imageqt import PillowQtTestCase -from PIL import Image, ImageQt - -if ImageQt.qt_is_installed: - from PIL.ImageQt import QImage +from PIL import ImageQt class TestFromQImage(PillowQtTestCase, PillowTestCase): def roundtrip(self, expected): - result = Image.fromqimage(expected.toqimage()) + result = ImageQt.fromqimage(expected.toqimage()) # Qt saves all images as rgb self.assert_image_equal(result, expected.convert('RGB')) diff --git a/Tests/test_image_fromqpixmap.py b/Tests/test_image_fromqpixmap.py index e96b3a374..78e4d6770 100644 --- a/Tests/test_image_fromqpixmap.py +++ b/Tests/test_image_fromqpixmap.py @@ -1,16 +1,14 @@ from helper import unittest, PillowTestCase, hopper -from test_imageqt import PillowQPixmapTestCase +from test_imageqt import PillowQtTestCase, PillowQPixmapTestCase -from PIL import Image, ImageQt - -if ImageQt.qt_is_installed: - from PIL.ImageQt import QPixmap +from PIL import ImageQt class TestFromQPixmap(PillowQPixmapTestCase, PillowTestCase): def roundtrip(self, expected): - result = Image.fromqpixmap(expected.toqpixmap()) + PillowQtTestCase.setUp(self) + result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected)) # Qt saves all pixmaps as rgb self.assert_image_equal(result, expected.convert('RGB')) diff --git a/Tests/test_image_toqimage.py b/Tests/test_image_toqimage.py index 125728bf0..d44a64847 100644 --- a/Tests/test_image_toqimage.py +++ b/Tests/test_image_toqimage.py @@ -11,6 +11,7 @@ if ImageQt.qt_is_installed: class TestToQImage(PillowQtTestCase, PillowTestCase): def test_sanity(self): + PillowQtTestCase.setUp(self) for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): data = ImageQt.toqimage(hopper(mode)) data.save('/tmp/hopper_{}_qimage.png'.format(mode)) diff --git a/Tests/test_image_toqpixmap.py b/Tests/test_image_toqpixmap.py index b857a66ca..dd527732f 100644 --- a/Tests/test_image_toqpixmap.py +++ b/Tests/test_image_toqpixmap.py @@ -1,5 +1,5 @@ from helper import unittest, PillowTestCase, hopper -from test_imageqt import PillowQPixmapTestCase +from test_imageqt import PillowQtTestCase, PillowQPixmapTestCase from PIL import ImageQt @@ -10,6 +10,7 @@ if ImageQt.qt_is_installed: class TestToQPixmap(PillowQPixmapTestCase, PillowTestCase): def test_sanity(self): + PillowQtTestCase.setUp(self) QPixmap('Tests/images/hopper.ppm').save( '/tmp/hopper_RGB_qpixmap_file.png') for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): diff --git a/Tests/test_imageqt.py b/Tests/test_imageqt.py index ec76f55d0..5959bee34 100644 --- a/Tests/test_imageqt.py +++ b/Tests/test_imageqt.py @@ -1,10 +1,10 @@ -from helper import unittest, PillowTestCase, hopper +from helper import unittest, PillowTestCase from PIL import ImageQt if ImageQt.qt_is_installed: - from PIL.ImageQt import QGuiApplication, QImage, qRgb, qRgba + from PIL.ImageQt import qRgba def skip_if_qt_is_not_installed(_): pass @@ -16,26 +16,25 @@ else: class PillowQtTestCase: def setUp(self): - try: - from PyQt5.QtGui import QImage, qRgb, qRgba - except ImportError: - try: - from PyQt4.QtGui import QImage, qRgb, qRgba - except ImportError: - try: - from PySide.QtGui import QImage, qRgb, qRgba - except ImportError: - self.skipTest('PyQt4 or 5 or PySide not installed') skip_if_qt_is_not_installed(self) def tearDown(self): pass - class PillowQPixmapTestCase(PillowQtTestCase): def setUp(self): PillowQtTestCase.setUp(self) + try: + if ImageQt.qt_version == '5': + from PyQt5.QtGui import QGuiApplication + elif ImageQt.qt_version == '4': + from PyQt4.QtGui import QGuiApplication + elif ImageQt.qt_version == 'side': + from PySide.QtGui import QGuiApplication + except ImportError: + self.skipTest('QGuiApplication not installed') + self.app = QGuiApplication([]) def tearDown(self): @@ -50,6 +49,12 @@ class TestImageQt(PillowQtTestCase, PillowTestCase): # typedef QRgb # An ARGB quadruplet on the format #AARRGGBB, # equivalent to an unsigned int. + if ImageQt.qt_version == '5': + from PyQt5.QtGui import qRgb + elif ImageQt.qt_version == '4': + from PyQt4.QtGui import qRgb + elif ImageQt.qt_version == 'side': + from PySide.QtGui import qRgb self.assertEqual(qRgb(0, 0, 0), qRgba(0, 0, 0, 255))