Ask Your Question
0

Problem showing images

asked 2012-08-20 04:27:05 -0500

mcirri gravatar image

I am quite new to SAGE so I am probably asking something silly but some days of google search did not provide a simple answer to my question. Using instructions from this website I created a small sage program:

from numpy import *
import scipy.ndimage
import matplotlib.image
import matplotlib.pyplot
img=matplotlib.image.imread(DATA+'dummy.png')
print 'Image dtype: %s'%(img.dtype)
print 'Image size: %6d'%(img.size)
print 'Image shape: %3dx%3d'%(img.shape[0],img.shape[1])
print 'Max value %1.2f at pixel %6d'%(img.max(),img.argmax())
print 'Min value %1.2f at pixel %6d'%(img.min(),img.argmin())
print 'Variance: %1.5f\nStandard deviation: %1.5f'%(img.var(),img.std())

where dummy.png is an existing PNG image that I loaded into the worksheet. The script works perfectly and I get the required info out of it:

Image dtype: float32
Image size: 17335905
Image shape: 2305x2507
Max value 1.00 at pixel   1767
Min value 0.00 at pixel 2032962
Variance: 0.06310
Standard deviation: 0.25120

now if I want to actually see the picture everything fails. I tried to type:

img
show(img)
img.show
img.show()

My final goal was to overlay the image, a photo, with the plot resulting from an analysis to show the accuracy of the prediction. Does anyone know how to do that?

thanks, mcirri

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2012-08-25 03:53:57 -0500

Volker Braun gravatar image

In fact, img is a numpy array containing (r,g,b) values. In Sage, you can plot it with

sage: matrix_plot(img, origin='lower')

Colors may be inverted (is 1.0 white or black?), you can try

sage: matrix_plot(1.0-img, origin='lower')

to flip it around.

edit flag offensive delete link more

Comments

Considering how often I use `matrix_plot`, I should have tried this... but I got stuck in mpl mode, which I don't really get that well :)

kcrisman gravatar imagekcrisman ( 2012-08-27 03:39:19 -0500 )edit
0

answered 2012-08-24 10:27:38 -0500

kcrisman gravatar image

img isn't a graphic, as the blog post indicates.

sage: type(img) 
<type 'numpy.ndarray'>

The "right" thing to do is use imshow like here, but I can never figure out how to actually get it to show up in the notebook. But this might give you enough hints if you've used mpl in the past.

edit flag offensive delete link more
0

answered 2012-10-05 09:22:54 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

You can do

import Image
img = Image.open(DATA+"filename") # if you load the image from disk
img = Image.fromarray(imgarray) # if the image is numpy array

img.save("test.png")

It will show up in the notebook. But it will also save a copy of the image to your disk. If I find out how to write the image to /dev/null, then maybe I could avoid writing to disk.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-08-20 04:27:05 -0500

Seen: 650 times

Last updated: Oct 05 '12