본문 바로가기

개발하자

주피터 노트북을 사용하여 그리드에 여러 png을 표시하는 방법

반응형

주피터 노트북을 사용하여 그리드에 여러 png을 표시하는 방법

나는 주피터 노트북과 같은 폴더에 있는 파일 이름 목록을 가지고 있다.

fnames =

노트북 출력 셀 내부의 그리드에 다음과 같은 이미지를 표시하려고 합니다.

  • 행수
  • 열 수
  • 표시할 이미지 딤(픽셀 단위)(각 이미지에 대해 딤 표시)



다음을 수행하십시오.

import os
import numpy as np
import matplotlib.pyplot as plt

directory = "./Images/"
images = os.listdir(directory)

fig = plt.figure(figsize=(10, 10))
columns = 2
rows = np.ceil(len(images))

for x, i in enumerate(images):
    path =  os.path.join("./Images/",i)
    img = plt.imread(path)
    fig.add_subplot(rows, columns, x+1)
    plt.imshow(img)
plt.show()



:

import os
import ipywidgets as widgets
from IPython.display import display

# Define a useful function
def get_image(f_path):
    '''
    Returns the image from a path
    '''
    img_labs = ['jpg','png']
    if any(x in img_labs for x in f_path.split('.')):
        file = os.path.join(folder,f_path)
        image = open(file,'rb').read()
        return image

# Do the actual work here
folder = 'Some Path to a Folder of Images'
files  = os.listdir(folder)
images = [get_image(x) for x in files]
children = [widgets.Image(value = img) for img in images if str(type(img)) != '<class \'NoneType\'>']
labels = ['{}'.format(i) for i in range(len(children))]

# Customize your layout here:
box_layout = widgets.Layout(
    display='flex',
    flex_flow='column',
    align_items='stretch',
    border='solid',
    width='50%')

# Create the widget
tab = widgets.Tab()
tab.children = children

# Label em'!
for i in range(len(children)):
    tab.set_title(i,labels[i])

display(tab)

자세한 내용은 을 참조하십시오.


반응형