我在一个文件夹中有一组单独的图像,并希望在自定义网格中显示它们(其大小和形状会有所不同,但我将在下面的代码中使用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)