简体   繁体   English

在 jupyter 笔记本中显示图像网格

[英]displaying grid of images in jupyter notebook

I have a dataframe with a column containing 495 rows of URLs.我有一个 dataframe,其中一列包含 495 行 URL。 I want to display these URLs in jupyter notebook as a grid of images.我想在 jupyter notebook 中将这些 URL 显示为图像网格。 The first row of the dataframe is shown here.此处显示 dataframe 的第一行。 Any help is appreciated.任何帮助表示赞赏。

id           latitude     longitude     owner             title                          url
23969985288 37.721238   -123.071023 7679729@N07 There she blows!    https://farm5.staticflickr.com/4491/2396998528...

I have tried the following way,我尝试了以下方式,

from IPython.core.display import display, HTML
for index, row in data1.iterrows():
  display(HTML("<img src='%s'>"%(i["url"])))

However, running of above code displays message但是,上面代码的运行会显示消息

> TypeError                         Traceback (most recent call last)
<ipython-input-117-4c2081563c17> in <module>()
      1 from IPython.core.display import display, HTML
      2 for index, row in data1.iterrows():
----> 3   display(HTML("<img src='%s'>"%(i["url"])))

TypeError: string indices must be integers

Your idea of using IPython.core.display with HTML is imho the best approach for that kind of task.您将IPython.core.display与 HTML 结合使用的想法是此类任务的最佳方法。 matplotlib is super inefficient when it comes to plotting such a huge number of images (especially if you have them as URLs). matplotlib在绘制如此大量的图像时效率非常低(尤其是当你将它们作为 URL 时)。
There's a small package I built based on that concept - it's called ipyplot我基于这个概念构建了一个小包——它叫做ipyplot

import ipyplot

images = data1['url'].values
labels = data1['id'].values

ipyplot.plot_images(images, labels, img_width=150)

You would get a plot similar to this:你会得到一个类似于这样的情节:
在此处输入图片说明

The best way to show a grid of images in the Jupyter notebook is probably using matplotlib to create the grid, since you can also plot images on matplotlib axes using imshow .在 Jupyter notebook 中显示图像网格的最佳方法可能是使用matplotlib创建网格,因为您还可以使用imshowmatplotlib轴上绘制图像。

I'm using a 3x165 grid, since that is 495 exactly.我使用的是 3x165 网格,因为正好是 495。 Feel free to mess around with that to change the dimensions of the grid.随意改变网格的尺寸。

import urllib
f, axarr = plt.subplots(3, 165)
curr_row = 0
for index, row in data1.iterrows():
     # fetch the url as a file type object, then read the image
     f = urllib.request.urlopen(row["url"])
     a = plt.imread(f)

     # find the column by taking the current index modulo 3
     col = index % 3
     # plot on relevant subplot
     axarr[col,curr_row].imshow(a)
     if col == 2:
         # we have finished the current row, so increment row counter
         curr_row += 1

Just add this code below to your cell:只需将下面的代码添加到您的单元格中:

display(IPython.display.HTML('''
  <flexthis></flexthis>
  <style> div.jp-Cell-outputArea:has(flexthis) {
    display:flex; flex-wrap: wrap;
  }</style>
'''))

and then, on the same cell, use calls to IPython.display.Image or anything else you want to show.然后,在同一个单元格上,使用对IPython.display.Image或您想要显示的任何其他内容的调用。


This code above allows you to add custom CSS to that specific output cell.上面的代码允许您将自定义 CSS 添加到特定的 output 单元格。 I'm using a flex box (display:flex), but you can change it to your liking.我使用的是弹性框 (display:flex),但您可以根据自己的喜好进行更改。

I can only do it by "brute force":我只能通过“蛮力”来做到这一点:

However, I only manage to do it manually:但是,我只能手动完成:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

%matplotlib inline

img1=mpimg.imread('Variable_8.png')
img2=mpimg.imread('Variable_17.png')
img3=mpimg.imread('Variable_18.png')
          ...

fig, ((ax1, ax2, ax3), (ax4,ax5,ax6)) = plt.subplots(2, 3, sharex=True, sharey=True) 

ax1.imshow(img1)
ax1.axis('off')
ax2.imshow(img2)
ax2.axis('off')
   ....

Do not know if ot helps不知道有没有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM