Last active
August 29, 2015 14:21
-
-
Save belltailjp/a9538aaf3221f754e5bf to your computer and use it in GitHub Desktop.
Inter-conversion between numpy ndimage and PySide QImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import numpy | |
import skimage.io | |
from PySide.QtGui import * | |
skimg = skimage.io.imread('imgfile.jpg') | |
# convert to QImage (skimg can be grayscale or rgb) | |
h, w = skimg.shape[:2] | |
qimg_format = QImage.Format_RGB888 if len(skimg.shape) == 3 else QImage.Format_Indexed8 | |
qimg = QImage(skimg.flatten(), w, h, qimg_format) | |
# if you can assure that skimg has 3 channels, this conversion can be just like: | |
# qimg = QImage(skimg.flatten(), skimg.shape[1], skimg.shape[0], QImage.Format_RGB888) | |
# re-convert to ndarray | |
skimg_ret = numpy.array(qimg.constBits()).reshape(skimg.shape) | |
assert (skimg == skimg_ret).all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment