gpt4 book ai didi

python - 在网格中显示图像

转载 作者:行者123 更新时间:2023-12-01 09:19:53 28 4
gpt4 key购买 nike

我在一个文件夹中有一组单独的图像,并希望在自定义网格中显示它们(其大小和形状会有所不同,但我将在下面的代码中使用 4*16)。我当前的代码使用 matplotlib 和 numpy,但速度非常慢(64 个图像>1 分钟),并且最终图像的分辨率很差。

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os

def make_array(folder):
filename_list = [];
im_list = [];

workingdir = os.getcwd();

if folder != "":
workingdir += "/"+folder

for file in os.listdir(workingdir):
if file.endswith(".JPG"):
filename_list.append(file);

filename_list.sort();

os.chdir(workingdir)

for i in range(0,16):
im_list.append(np.asarray(Image.open(filename_list[i]).convert('RGB')));
im_list.append(np.asarray(Image.open(filename_list[i+16]).convert('RGB')));
im_list.append(np.asarray(Image.open(filename_list[i+32]).convert('RGB')));
im_list.append(np.asarray(Image.open(filename_list[i+48]).convert('RGB')));

return np.array(im_list)

def gallery(array, ncols=4):
nindex, height, width, intensity = array.shape
nrows = nindex//ncols
assert nindex == nrows*ncols
# want result.shape = (height*nrows, width*ncols, intensity)
result = (array.reshape(nrows, ncols, height, width, intensity)
.swapaxes(1,2)
.reshape(height*nrows, width*ncols, intensity))
return result

def plot_array(gallery, name):
f = plt.figure()
f.set_size_inches(30, 120)
axes = plt.gca()
plt.xticks([])
plt.yticks([])
plt.imshow(gallery)
plt.show()
f.savefig(name, bbox_inches='tight')

# EDIT TO MATCH THE DESIRED PARAMETERS
#Note: The images will be ploted in the 'writing order' left to right then top to bottom
name = "4_days_per_particle"; #Name of the output file (.png)
folder="Pictures_4days" #Name of folder containing the pictures in the working directory (if not cwd itself)

#Save initial working directory
mainDir = os.getcwd();

#Creates the array of images
array = make_array(folder)

#Reorders the axis to shape the gallery
gal = gallery(array)

#Plots and saves the figure
plot_array(gal, name)

#Cleanup directory
os.chdir(mainDir);

如何更快地获得相同的结果并控制输出分辨率(最多保持图像文件的原始分辨率)?谢谢!

最佳答案

我最终找到了一种更简洁的方法来使用 OpenCV 来做到这一点,灵感来自于这个要点:

https://gist.github.com/pgorczak/95230f53d3f140e4939c#file-imgmatrix-py

根据我的经验,这种方法速度更快,并且绕过 matplotlib 可以完全控制输出分辨率。

此外,如有必要,cv2.resize() 可用于重新缩放图像,并且 IMWRITE_JPEG_QUALITY 参数可用于将 JPEG 导出质量设置为控制文件大小的句柄。

import itertools
import cv2
import os
import numpy as np

#User defined variables
dirname = "my_directory" #Name of the directory containing the images
name = "my_image_name" + ".jpg" #Name of the exported file
margin = 20 #Margin between pictures in pixels
w = 8 # Width of the matrix (nb of images)
h = 8 # Height of the matrix (nb of images)
n = w*h

filename_list = []

for file in os.listdir(dirname):
if file.endswith(".JPG"):
filename_list.append(file)

filename_list.sort();

print(filename_list)

imgs = [cv2.imread(os.getcwd()+"/"+dirname+"/"+file) for file in filename_list]

#Define the shape of the image to be replicated (all images should have the same shape)
img_h, img_w, img_c = imgs[0].shape

#Define the margins in x and y directions
m_x = margin
m_y = margin

#Size of the full size image
mat_x = img_w * w + m_x * (w - 1)
mat_y = img_h * h + m_y * (h - 1)

#Create a matrix of zeros of the right size and fill with 255 (so margins end up white)
imgmatrix = np.zeros((mat_y, mat_x, img_c),np.uint8)
imgmatrix.fill(255)

#Prepare an iterable with the right dimensions
positions = itertools.product(range(h), range(w))

for (y_i, x_i), img in zip(positions, imgs):
x = x_i * (img_w + m_x)
y = y_i * (img_h + m_y)
imgmatrix[y:y+img_h, x:x+img_w, :] = img

resized = cv2.resize(imgmatrix, (mat_x//3,mat_y//3), interpolation = cv2.INTER_AREA)
compression_params = [cv2.IMWRITE_JPEG_QUALITY, 90]
cv2.imwrite(name, resized, compression_params)

关于python - 在网格中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50881227/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com