mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-16 11:21:25 +08:00
allow qt_plugin imsave() function to write to file-like objects
This commit is contained in:
@@ -7,8 +7,8 @@ import sys
|
||||
window_manager.acquire('qt')
|
||||
|
||||
try:
|
||||
from PyQt4.QtGui import (QApplication, QMainWindow, QImage, QPixmap,
|
||||
QLabel, QWidget)
|
||||
from PyQt4.QtGui import (QApplication, QImage,
|
||||
QLabel, QMainWindow, QPixmap, QWidget)
|
||||
from PyQt4 import QtCore, QtGui
|
||||
import sip
|
||||
import warnings
|
||||
@@ -148,12 +148,21 @@ def _app_show():
|
||||
print 'No images to show. See `imshow`.'
|
||||
|
||||
|
||||
def imsave(filename, img):
|
||||
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)
|
||||
saved = qimg.save(filename)
|
||||
if _is_filelike(filename):
|
||||
byte_array = QtCore.QByteArray()
|
||||
qbuffer = QtCore.QBuffer()
|
||||
qbuffer.open(QtCore.QIODevice.ReadWrite)
|
||||
saved = qimg.save(qbuffer, format_str.upper())
|
||||
qbuffer.seek(0)
|
||||
filename.write(qbuffer.readAll())
|
||||
qbuffer.close()
|
||||
else:
|
||||
saved = qimg.save(filename)
|
||||
if not saved:
|
||||
from textwrap import dedent
|
||||
msg = dedent(
|
||||
@@ -161,3 +170,7 @@ def imsave(filename, img):
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user