Skip to content

Instantly share code, notes, and snippets.

@alexland
Created June 3, 2014 01:13
Show Gist options
  • Save alexland/042b9efcc152bbffabd8 to your computer and use it in GitHub Desktop.
Save alexland/042b9efcc152bbffabd8 to your computer and use it in GitHub Desktop.
>>> from scipy import misc as MSC
>>> import os
>>> df = os.path.expanduser("~/any-image.png")
>>> # now read the image in as a multi-dimensional array (matrix)
>>> # 'imread' is a wrapper over a PIL method
>>> mg1 = MSC.imread(df)
>>> mg1.shape
(409, 600, 4)
'''
as you can see the image has six dimensions
two first two represent the x,y cartesian plane upon which pixels are placed
and the other four are required to represent a pixel (rgba)
'''
>>> # so for example, let's look at a single pixel from the center of this image
>>> mg1[200,300,:]
array([ 18, 24, 28, 255], dtype=uint8)
'''
of course the last elment in this four-element array is
the alpha channel, which is absent by default if the
image read in is jpg format
'''
>>> # image size in MB
>>> print("{0:,}".format(mg1.nbytes/1000))
981,600
>>> # remove the axis that encodes the alpha channel
>>> mg2 = mg1[:,:,:-1]
>>> mg2.shape
(409, 600, 3)
>>> # doing this gives you a 5D image, 1 each for r, g, b,
>>> # and 1 each for the two axes, x and y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment