Reputation: 455
I have about 1,300,000 images. To reduce I/O cost, I want to make one file out of all of the images. So, file need to be (1300000, 256, 256, 3).
I intended to solve it with numpy
, unfortunately, I can't read and write all the images at once because I have just 16gb ram.
Could you suggest some way to solve it efficiently?
Upvotes: 0
Views: 40
Reputation: 2685
You will need to concatenate the images one at a time. Here is a possible solution using PIL/Pillow
import sys
import glob
from PIL import Image
def concat_two_images(image1, image2):
images = map(Image.open, [image1, image2])
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save('concat_image.jpg')
return 'concat_image.jpg'
first_concat_image = concat_two_images('test_image1.jpg', 'test_image2.jpg')
for image in glob.glob('*.jpg'):
# TODO - Add code to skip the first 2 images
new_image = concat_two_images(first_concat_image, image)
Upvotes: 1