在Python中显示jpg图像的网格

时间:2017-01-25 05:23:05

标签: python image jupyter-notebook

让以下模拟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)']}))

我想在Jupyter笔记本中显示来自网络的图像网格。这些图像是来自SDSS的星系,其度数坐标在我的DataFrame中。他们是jpg。我不需要将它们存储在磁盘上,但这是我发现的唯一方法。我只能在列中显示它们,并且我使用字幕打印,这样:

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)

这会产生以下输出:

enter image description here

enter image description here

我想在4x4网格(带标签)上显示。我在展示之前尝试插入类似plt.subplot(441+i)的内容,但它不起作用。此外,如果可以不将图像存储在磁盘上,那将是完美的。

谢谢。

1 个答案:

答案 0 :(得分:2)

所以我终于把它排除了。我回来发布我的代码。

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()