Created
November 16, 2012 06:36
-
-
Save derekbrokeit/4084838 to your computer and use it in GitHub Desktop.
View images with the matplotlib library
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 | |
# Use the power of matplotlib to display an image file | |
# This can be helpful when logged in to a remote server | |
# and you need more power than xview, or may xview hasn't | |
# been installed. | |
# 2012 Derek Ashley Thomas | |
import matplotlib as mpl | |
import matplotlib.image as mpimg | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from os.path import expanduser | |
import sys | |
def load_image(fname): | |
fname = expanduser(fname) | |
# load image | |
img = mpimg.imread(fname) | |
return img | |
def plot_img(data): | |
fig = plt.figure(frameon=False) | |
ax = plt.Axes(fig, [0., 0., 1., 1.]) | |
ax.set_axis_off() | |
fig.add_axes(ax) | |
ax.imshow(data, aspect='normal') | |
plt.show() | |
def test_save(fname=None): | |
if not fname: | |
fname = "/tmp/test.png" | |
x = np.arange(100) | |
y = x**2 | |
plt.plot(x,y) | |
plt.savefig(fname) | |
plt.close() | |
return fname | |
def print_usage(err=1): | |
print "USAGE: imv [filename]" | |
sys.exit(err) | |
if __name__ == "__main__": | |
try: | |
img_file = sys.argv[1] | |
if img_file == "--test": | |
img_file = test_save() | |
print "*** Using test value" | |
except IndexError: | |
print_usage() | |
img = load_image(img_file) | |
plot_img(img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment