Отображать изображения в сетке

1

У меня есть коллекция отдельных изображений в папке и вы хотите отображать их в пользовательской сетке (размер и форма которых будет меняться, но я буду использовать 4 * 16 в приведенном ниже коде). Мой текущий код использует matplotlib и numpy, но он очень медленный (> 1 мин для 64 изображений), а разрешение конечного изображения плохое.

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

Как я могу добиться того же результата быстрее и контролировать разрешение вывода (вплоть до сохранения исходного разрешения файлов изображений)? Спасибо!

Теги:
jupyter-notebook
image

1 ответ

0
Лучший ответ

Я решил найти более чистый способ сделать это, используя 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)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню