matplotlib 在一个图中显示80个图像,所有图像相互连接[重复]

v9tzhpje  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(118)

此问题在此处已有答案

How to plot images in subplots(1个答案)
Eliminate white space between subplots in a matplotlib figure(1个答案)
How to remove gaps between image subplots(2个答案)
How to combine gridspec with plt.subplots() to eliminate space between rows of subplots(1个答案)
How to remove the space between subplots(5个回答)
11天前关闭。
我想在一个10列和8行,并从垂直和水平使用python连接到彼此的一个图80images
预计这一产出
enter image description here
这是我的代码,

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os
from PIL import Image

# Specify the path to the folder containing the images
image_folder = '/Users/shaimaa/Downloads/Speaker_image.fld'

# Get a list of image file names in the folder
image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]

# Create a 10x8 grid using gridspec
fig = plt.figure(figsize=(16, 20))
gs = gridspec.GridSpec(8, 10)

# Initialize an index to keep track of the current image
index = 0

for i in range(8):
    for j in range(10):
        if index < len(image_files):
            image_path = os.path.join(image_folder, image_files[index])
            img = Image.open(image_path)
            img = img.resize((50, 50))  # Resize the image to 50x50 pixels

            ax = plt.subplot(gs[i, j])
            ax.imshow(img)
            ax.axis('off')  # Turn off the axis labels
            index += 1

# Remove space between subplots (both vertically and horizontally)
plt.subplots_adjust(wspace=0, hspace=0)

plt.show()

字符串
多谢帮忙

ctrmrzij

ctrmrzij1#

我能想到的两个选择:
1.直接使用plt.subplots:

import matplotlib.pyplot as plt
import os

from PIL import Image

image_folder = "/Users/shaimaa/Downloads/Speaker_image.fld"
n_row = 8
n_col = 10
fig, axes = plt.subplots(n_row, n_col, sharex=True, sharey=True, figsize=(n_col, n_row))
fig.subplots_adjust(wspace=0, hspace=0)

image_files = [f for f in os.listdir(image_folder) if f.endswith(".png")][
    : n_row * n_col
]

for ax, file in zip(axes.flatten(), image_files):
    image_path = os.path.join(image_folder, file)
    img = Image.open(image_path)
    img = img.resize((50, 50))
    ax.imshow(img)
    ax.axis("off")

plt.show()

字符串
然而,这仍然留下了一些小的差距(原因我不知道)

两个完全避免这些差距,你可以

  1. concatenate the images绘制之前
import matplotlib.pyplot as plt
import os

from PIL import Image

image_folder = "/Users/shaimaa/Downloads/Speaker_image.fld"
n_row = 8
n_col = 10

image_files = [f for f in os.listdir(image_folder) if f.endswith(".png")][
    : n_row * n_col
]

def get_concat_h(im1, im2):
    if im1 is None:
        return im2
    dst = Image.new("RGB", (im1.width + im2.width, im1.height))
    dst.paste(im1, (0, 0))
    dst.paste(im2, (im1.width, 0))
    return dst

def get_concat_v(im1, im2):
    if im1 is None:
        return im2
    dst = Image.new("RGB", (im1.width, im1.height + im2.height))
    dst.paste(im1, (0, 0))
    dst.paste(im2, (0, im1.height))
    return dst

super_row = None
super_image = None

for i_row in range(n_row):
    for i_col in range(n_col):
        file = image_files[i_col * i_row + i_row * n_col]
        image_path = os.path.join(image_folder, file)
        img = Image.open(image_path)
        img = img.resize((50, 50))
        super_row = get_concat_h(super_row, img)
    super_image = get_concat_v(super_image, super_row)
    super_row = None

plt.imshow(super_image)
plt.show()

相关问题