0

I have a directory of image files that represent inidvudal images in a 12x8 grid. Each file is named "VID1433_A11_00d00h00m.tif", "VID1433A2_1_00d00h00m.tif" etc.

I want to be able to arrange the images such that I have a grid of images that look like this:

A1_A2_A3_A4_A5_A6_A7_A8_A9_A10_A11_A12
B1_B2_B3_B4_B5_B6_B7_B8_B9_B10_B11_B12
C1_C2_C3_C4_C5_C6_C7_C8_C9_C10_C11_C12
D1_D2_D3_D4_D5_D6_D7_D8_D9_D10_D11_D12
E1_E2_E3_E4_E5_E6_E7_E8_E9_E10_E11_E12
F1_F2_F3_F4_F5_F6_F7_F8_F9_F10_F11_F12
G1_G2_G3_G4_G5_G6_G7_G8_G9_G10_G11_G12
H1_H2_H3_H4_H5_H6_H7_H8_H9_H10_H11_H12

So far I have this:

    # Config:
images_dir = 'C:\\Users\\pateld1\\Pictures\\Incucyte\\P1'
result_grid_filename = './grid.jpg'
result_figsize_resolution = 40 # 1 = 100px

images_list = os.listdir(images_dir)

images_count = len(images_list)
print('Images: ', images_list)
print('Images count: ', images_count)




# Create plt plot:
fig, axes = plt.subplots(12, 8, figsize=(result_figsize_resolution, result_figsize_resolution))

Which creates a list of the image names & a figure of a 12x8 grid

How can I assign the images to a position in the grid based on their id, i.e. A1 to the 1x1 square, A2 to the 2x1 square etc.

I can upload the folder of images if need be

  • How are they assigned to positions currently, what is wrong with that? – mkrieger1 Aug 27 '22 at 13:02
  • I don't actually know how to assign them to the positions is the thing. But if i was to assign them to positions along the x axis by going through the list of images, the list gets sorted incorrectly by starting with A10, A11, A12, and then A1, A2 etc. So if i tried to assort them like that the order would be wrong – Danyn Patel Aug 27 '22 at 13:33

1 Answers1

0

axes should contain an list of a list of axes. You can loop through both dimensions or sort the list of images and do:

import re

def natural_sort(l):
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)]
    return sorted(l, key=alphanum_key)


for ax, image in zip(axes.flat, natural_sort(images_list)):
    ax.imshow...  # plot your image

Natural sort function copied from https://stackoverflow.com/a/4836734/7415199

Johannes Heuel
  • 156
  • 2
  • 4