Matt
Matt

Reputation: 813

Display grid of jpg images in Python

Let the following mock pandas.DataFrame:

import pandas as pd

Cat=pd.DataFrame(({'RA' :[230.82102945, 173.65985309999999, 173.66313018, 
                          173.84698746999999, 189.96874310999999, 
                          170.20006370999999, 170.20416528000001,
                          170.16438034000001, 170.24896294000001, 
                          189.84348857000001, 212.74040604000001, 
                          212.68784378000001, 154.36278941, 
                          154.40930130000001, 154.41919107000001],
                    'Dec': [-1.0481932199999999, 0.18865839000000001, 0.1247615, 
                            0.090550759999999994, 0.12548559000000001, 
                            0.46857110000000002, 0.45924195000000001, 
                            0.45747362000000003, 0.53422636000000001, 0.46023247, 
                            1.03574006, 1.04634373, -0.49560479000000002,
                            -0.45308465999999997, -0.48165697000000002],
                  'Morph':['Ei', 'Er', 'Sc', 'Er', 'Sb', 'Ser', 'Er', 'Ser', 
                        'Sc', 'Ec', 'Sb', 'Sb', 'Ser', 'Ei(o)', 'Ei(o)']}))

I want to display a grid of images from the web in a Jupyter notebook. Those images are galaxies from SDSS, which coordinates in degree are in my DataFrame. They are jpg. I don't need to store them on disk, but it's the only way I've found yet. I'm only able to display them in column, and I use a print for the caption, this way:

import urllib
from IPython.display import Image
from IPython.display import display
from astropy import units as u


impix = 10
imsize = 0.1*u.arcmin
cutoutbaseurl = 'http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx'

for i in range(16):
    query_string = urllib.urlencode(dict(ra=Cat.iloc[i]['RA'], 
                                         dec=Cat.iloc[i]['Dec'], 
                                         width=impix, height=impix, 
                                         scale=imsize.to(u.arcsec).value/impix))
    url = cutoutbaseurl + '?' + query_string
    urllib.urlretrieve(url, '%i.jpg'%i)
    print Cat.iloc[i]['Morph']
    x = Image(filename='%i.jpg'%i, height=20*impix, width=20*impix)) 
    display(x)

This produces the following output:

enter image description here

enter image description here

I would like to display this on a 4x4 grid (with labels). I've tried to insert something like plt.subplot(441+i) before displaying, but it doesn't work. Moreover, if it's possible not to store the images on disk, it would be perfect.

Thank you.

Upvotes: 0

Views: 3825

Answers (1)

Matt
Matt

Reputation: 813

So I had finally sorted this out. I come back to post my code.

import matplotlib as plt

def getgal(x,i):
    query_string = urllib.urlencode(dict(ra=x['RA'],
                                         dec=x['Dec'],
                                         width=impix, height=impix,
                                         scale=imsize.to(u.arcsec).value/impix))
    return cutoutbaseurl + '?' + query_string

def url_to_image(url):
    resp = urllib.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    return image

def galshow(size,width,height,start):
    fig = plt.figure(figsize=(size,size))
    for i in range(0,width*height):
        j = i+start
        url = getgal(Yang.iloc[j],j)
        img = url_to_image(url)
        plt.subplot(height,width,i+1)
        SpecObjID, RA, Dec = Yang.iloc[j]['SpecObjID'],Yang.iloc[j]['RA'],Yang.iloc[j]['Dec']
        label = "%i\nRA %f, Dec %f\n%s"%(SpecObjID, RA, Dec,Yang.iloc[j]['Morph'])
        plt.title(label)    
        plt.xticks([]), plt.yticks([])
        plt.tight_layout()
        plt.imshow(img)
    plt.show()

Upvotes: 2

Related Questions