簡體   English   中英

如何使用jupyter筆記本在網格中顯示多個png

[英]how to display multiple pngs in a grid using jupyter notebook

我有一個文件名列表,位於與jupyter筆記本相同的文件夾中。

fnames = ['foo.png', 'my_img.png', 'img1.png', ..... 'last_img.png']

我想在筆記本輸出單元格的網格中顯示這些圖像,指定:

  • 行數
  • 列數
  • 圖像變暗(以像素為單位)顯示(每個圖像的相同暗淡)

試試這個

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

更深入的潛水,您可以使用ipywidgets作為替代方案在Tabs查看

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)

有關更多詳細信息,請訪問文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM