mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-28 04:55:16 +08:00
b6dcf3c336
Update Travis build to use Anaconda Travis updates and fixes More travis fixes Another travis attempt Revert changes Use PIL and Pillow Refactor travis into 4 different builds Fix activation error Remove explicit mpl in build_versions.py Make matplotlib an explicit requirement Rearrange travis Make pillow a hard requirement Try again to make Pillow optional Fix bash syntax error Fix bash syntax error Bump required cython version More rearrangments Remove mpl from build_versions, rearrange travis Fix version check Make matplotlib explicit again Conda install into test env Check for proper install Allow tests to skip if networkx is not available Allow tests to skip if networkx is not available Try swapping pillow for matplotlib Allow tests to pass when matplotlib is not present Remove matplotlib from build_versions Print PIL version Get pillow from PIP Allow tests to skip if matplotlib is not present. Allow tests to skip if networkx is not present. travis fix Remove unused mpl import that caused test error Use nose-cov and do not run doctests without optional libs Bump required numpy version and fix nose calls Make overlay test repeatable bump numpy version again Move low-end numpy to python 2.7 Play with minimum versions Add version requirements and use functions Add version requirements and use functions Allow require to skip a test More implementation of require decorator Update require decorator and clean up tests Only use requires decorator when needed Fix python3 error in version_requirements Fix build errors Fix handling of require with tests More fixes for require handler Use latest miniconda Fix more build errors Fix another dict comprehension and travis file. Fix missing imports Fix dictionary again Fix import warning Fix last failing test on 2.6 Skip doc examples on python2.6 Do not run doctests on python2.6 Fix typo in travis.yml Make numpy-1.6 compatibility changes Use numpy-1.6 in travis python2.6 Add tests for version requirements Fix line noise in PR Add additional io plugins Fix simpleitk test. Fix python 3 error in freeimage_plugin. Install imread in Travis. Put matplotlib settings in XDG recommended directory Fix formatting in travis yml Fix formatting in travis yml Make sure to close PIL file atexit Fix name of apt package xcftools Fix pil fp closing Fix matplotlibrc creation Only download SimpleITK on py2x, run coverage on py27 Fix travis yml syntax error Run coveralls on py2.7 Install SimpleITK on py3.3 and run coverage on py3.3 Make simpleitk install quiet Use standard nose and clean up incantation Fix travis yml syntax error Put in miniconda workout for libc error. Fix imread plugin. Fix travis syntax Remove unused import Remove miniconda libpng in favor of system png Fix imread install and move libm removal to after optional pkg install. Fix png header copy in travis yml Another attempt to use png headers Debug freeimage Add jpeg library for freeimage and debug imread. More debug for imread and freeimage More freeimage and imread debugging More debugging Use correct paths for test env Make sure imread is tied to libpng15 Add a TODO note for simpleitk test causing error. Fix typo in yml Cleanup and add more comments to travis yml Update comment Try and add 3.2 support. Docstring formatting Add more travis comments. Try numpy 1.6 on python 2.7 Fix travis syntax error Rename CONDA to ENV for clarity Alias python on python 3.2 Use python 3.2 as the system python Clean up libfreeimage install Fix order on py3.2 pre_install Move old numpy back to py26 Use the appropriate python calls. Debug 3.2 build. Update comment Fix syntax error Another fix for syntax error. Install scipy after downloading import tools More debugging for py32 Do not install conda on py3.2 (duh) Fix typo in travis yml Fix py32 qt install, separate pyfits and imread to find error Fix syntax error and front-load option lib check for debug pyfits is not supported in py3.2, try imread now imread is also not supported on py3.2 install imread before pyfits to show relationship with libs Make pip builds quiet Minor formatting to retrigger build Allow simpleitk to fail to download without breaking the build Use travis_retry for SimpleITK See what breaks when we keep libm in Now remove libm again
176 lines
5.4 KiB
Python
176 lines
5.4 KiB
Python
from .util import prepare_for_display, window_manager
|
|
import numpy as np
|
|
|
|
# We try to aquire the gui lock first or else the gui import might
|
|
# trample another GUI's PyOS_InputHook.
|
|
window_manager.acquire('qt')
|
|
|
|
try:
|
|
from PyQt4.QtGui import (QApplication, QImage,
|
|
QLabel, QMainWindow, QPixmap, QWidget)
|
|
from PyQt4 import QtCore, QtGui
|
|
import sip
|
|
import warnings
|
|
|
|
except ImportError:
|
|
window_manager._release('qt')
|
|
|
|
raise ImportError("""\
|
|
PyQt4 libraries not installed. Please refer to
|
|
|
|
http://www.riverbankcomputing.co.uk/software/pyqt/intro
|
|
|
|
for more information. PyQt4 is GPL licensed. For an
|
|
LGPL equivalent, see
|
|
|
|
http://www.pyside.org
|
|
""")
|
|
|
|
app = None
|
|
|
|
|
|
class ImageLabel(QLabel):
|
|
def __init__(self, parent, arr):
|
|
QLabel.__init__(self)
|
|
|
|
# we need to hold a reference to
|
|
# arr because QImage doesn't copy the data
|
|
# and the buffer must be alive as long
|
|
# as the image is alive.
|
|
self.arr = arr
|
|
|
|
# we also need to pass in the row-stride to
|
|
# the constructor, because we can't guarantee
|
|
# that every row of the numpy data is
|
|
# 4-byte aligned. Which Qt would require
|
|
# if we didnt pass the stride.
|
|
self.img = QImage(arr.data, arr.shape[1], arr.shape[0],
|
|
arr.strides[0], QImage.Format_RGB888)
|
|
self.pm = QPixmap.fromImage(self.img)
|
|
self.setPixmap(self.pm)
|
|
self.setAlignment(QtCore.Qt.AlignTop)
|
|
self.setMinimumSize(100, 100)
|
|
|
|
def resizeEvent(self, evt):
|
|
width = self.width()
|
|
pm = QPixmap.fromImage(self.img)
|
|
self.pm = pm.scaledToWidth(width)
|
|
self.setPixmap(self.pm)
|
|
|
|
|
|
class ImageWindow(QMainWindow):
|
|
def __init__(self, arr, mgr):
|
|
QMainWindow.__init__(self)
|
|
self.setWindowTitle('skimage')
|
|
self.mgr = mgr
|
|
self.main_widget = QWidget()
|
|
self.layout = QtGui.QGridLayout(self.main_widget)
|
|
self.setCentralWidget(self.main_widget)
|
|
|
|
self.label = ImageLabel(self, arr)
|
|
self.layout.addWidget(self.label, 0, 0)
|
|
self.layout.addLayout
|
|
self.mgr.add_window(self)
|
|
self.main_widget.show()
|
|
|
|
def closeEvent(self, event):
|
|
# Allow window to be destroyed by removing any
|
|
# references to it
|
|
self.mgr.remove_window(self)
|
|
|
|
|
|
def imread_qt(filename):
|
|
"""
|
|
Read an image using QT's QImage.load
|
|
"""
|
|
qtimg = QImage()
|
|
if not qtimg.load(filename):
|
|
# QImage.load() returns false on failure, so raise an exception
|
|
raise IOError('Unable to load file %s' % filename)
|
|
if qtimg.depth() == 1:
|
|
raise IOError('1-bit images currently not supported')
|
|
# TODO: Warn about other odd formats we don't currently handle properly,
|
|
# such as the odd 16-bit packed formats QT supports
|
|
arrayptr = qtimg.bits()
|
|
# QT may pad the image, so we need to use bytesPerLine, not width for
|
|
# the conversion to a numpy array
|
|
bytesPerPixel = qtimg.depth() // 8
|
|
pixelsPerLine = qtimg.bytesPerLine() // bytesPerPixel
|
|
img_size = pixelsPerLine * qtimg.height() * bytesPerPixel
|
|
arrayptr.setsize(img_size)
|
|
img = np.array(arrayptr)
|
|
# Reshape and trim down to correct dimensions
|
|
if bytesPerPixel > 1:
|
|
img = img.reshape((qtimg.height(), pixelsPerLine, bytesPerPixel))
|
|
img = img[:, :qtimg.width(), :]
|
|
else:
|
|
img = img.reshape((qtimg.height(), pixelsPerLine))
|
|
img = img[:, :qtimg.width()]
|
|
# Strip qt's false alpha channel if needed
|
|
# and reorder color axes as required
|
|
if bytesPerPixel == 4 and not qtimg.hasAlphaChannel():
|
|
img = img[:, :, 2::-1]
|
|
elif bytesPerPixel == 4:
|
|
img[:, :, 0:3] = img[:, :, 2::-1]
|
|
return img
|
|
|
|
if sip.SIP_VERSION >= 0x040c00:
|
|
# sip.voidptr only acquired a buffer view in 4.12.0, so our imread
|
|
# doesn't work with earlier versions
|
|
imread = imread_qt
|
|
else:
|
|
warnings.warn(RuntimeWarning(
|
|
"sip version too old. QT imread disabled"))
|
|
|
|
|
|
def imshow(arr, fancy=False):
|
|
global app
|
|
if not app:
|
|
app = QApplication([])
|
|
|
|
arr = prepare_for_display(arr)
|
|
|
|
if not fancy:
|
|
iw = ImageWindow(arr, window_manager)
|
|
else:
|
|
from .skivi import SkiviImageWindow
|
|
iw = SkiviImageWindow(arr, window_manager)
|
|
|
|
iw.show()
|
|
|
|
|
|
def _app_show():
|
|
global app
|
|
if app and window_manager.has_windows():
|
|
app.exec_()
|
|
else:
|
|
print('No images to show. See `imshow`.')
|
|
|
|
|
|
def imsave(filename, img, format_str=None):
|
|
# we can add support for other than 3D uint8 here...
|
|
img = prepare_for_display(img)
|
|
qimg = QImage(img.data, img.shape[1], img.shape[0],
|
|
img.strides[0], QImage.Format_RGB888)
|
|
if _is_filelike(filename):
|
|
byte_array = QtCore.QByteArray()
|
|
qbuffer = QtCore.QBuffer(byte_array)
|
|
qbuffer.open(QtCore.QIODevice.ReadWrite)
|
|
saved = qimg.save(qbuffer, format_str.upper())
|
|
qbuffer.seek(0)
|
|
filename.write(qbuffer.readAll().data())
|
|
qbuffer.close()
|
|
else:
|
|
saved = qimg.save(filename)
|
|
if not saved:
|
|
from textwrap import dedent
|
|
msg = dedent(
|
|
'''The image was not saved. Allowable file formats
|
|
for the QT imsave plugin are:
|
|
BMP, JPG, JPEG, PNG, PPM, TIFF, XBM, XPM''')
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
def _is_filelike(possible_filelike):
|
|
return callable(getattr(possible_filelike, 'write', None))
|