繁体   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