Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/a9538aaf3221f754e5bf to your computer and use it in GitHub Desktop.
Save belltailjp/a9538aaf3221f754e5bf to your computer and use it in GitHub Desktop.
Inter-conversion between numpy ndimage and PySide QImage
#!/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