본문 바로가기

개발하자

주피터 노트북에 이미지 그리드 표시

반응형

주피터 노트북에 이미지 그리드 표시

나는 495개의 URL 행을 포함하는 열이 있는 데이터 프레임을 가지고 있다. 나는 이 URL들을 주피터 노트북에 이미지 그리드로 표시하고 싶다. 데이터 프레임의 첫 번째 행이 여기에 표시됩니다. 아무쪼록 잘 부탁드립니다.

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

나는 다음과 같은 방법을 시도했다.

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

그러나 위의 코드를 실행하면 메시지가 표시됩니다.

> 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



아래 코드를 셀에 추가하십시오.

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

그리고 나서, 같은 휴대전화에서, 당신이 보여주고 싶은 다른 어떤 것에 대한 통화를 사용하세요.


위의 이 코드를 사용하면 특정 출력 셀에 사용자 지정 CSS를 추가할 수 있습니다. 플렉스 박스(디스플레이:플렉스)를 사용하고 있지만, 원하는 대로 변경할 수 있습니다.




주피터 노트북에서 이미지 그리드를 표시하는 가장 좋은 방법은 를 사용하여 이미지를 축에 플롯할 수도 있기 때문에 그리드를 만드는 것입니다.

저는 3x165 그리드를 사용하고 있습니다. 정확히 495이기 때문입니다. 그리드의 치수를 변경하기 위해 그것을 마음대로 만지작거립니다.

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



나는 오직 "무서운 힘"에 의해서만 할 수 있다.

그러나 수동으로만 수행할 수 있습니다.

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')
   ....

도움이 되는지 모르겠다.




HTML과 함께 사용하는 당신의 아이디어는 그런 종류의 작업에 가장 좋은 접근법이다. 이와 같이 많은 수의 이미지를 플로팅하는 경우(특히 URL로 사용하는 경우)는 매우 비효율적입니다. 그 컨셉을 바탕으로 만든 작은 패키지가 있습니다. 이름이 그것입니다.

import ipyplot

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

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

다음과 유사한 그림을 얻을 수 있습니다.


반응형