Моя система имеет 64 ГБ ОЗУ, поэтому я решил, что не должно возникнуть проблем с загрузкой всех изображений одновременно, а затем созданием нового выходного изображения (должно быть того же размера/загрузки ресурсов) и вставкой в него фрагментов изображений. Наконец-то сохраняю на диск.
Ну, смотрю наверх, я вижу, что после загрузки моих изображений программа потребляет 56 ГБ ОЗУ (т. е. 7 ГБ * 8). Во время операции вставки программа завершается.
Могу ли я каким-то образом изменить вызов Pillow open, чтобы он открывался на собственной глубине? Есть ли другая библиотека обработки изображений, которую кто-то мог бы порекомендовать вместо этого (другие языки приемлемы, если это единственный очевидный путь...).
Вот код:
Код: Выделить всё
import os
import math
import numpy as np
from rectpack import newPacker, PackingMode
import rectpack.packer
from PIL import Image, PILLOW_VERSION
dirname = 'output_12_15'
files = sorted(os.listdir(dirname))
images = []
num_pixels = 0
for i, file in enumerate(files[1:]):
filepath = os.path.join(dirname, file)
img = Image.open(filepath)
images.append(img.copy())
img.close()
width, height = images[-1].size
num_pixels += width*height
if not i%1000:
print(i)
print('number of total pixels: {}'.format(num_pixels))
edge_len = math.ceil(num_pixels**(1/2))
theorhetical_num_images_wide = math.ceil(edge_len/1366.)
target_width = (theorhetical_num_images_wide+1)*1366
target_height = target_width
packer = newPacker(mode=PackingMode.Offline, sort_algo=rectpack.packer.SORT_NONE, rotation=False)
# Add the rectangles to packing queue
for img in images:
packer.add_rect(*img.size)
packer.add_bin(target_width, target_height)
print('starting to pack')
packer.pack()
print('finished packing, starting to paste image together')
if len(packer[0])!=len(images):
print('packer failed, bin was too small!')
import pdb;pdb.set_trace()
else:
else:
max_y = 0
max_x = 0
blank = Image.new("1", (target_width,target_height))
for i, rect in enumerate(packer[0]):
x = rect.x
y = rect.y
w = rect.width
h = rect.height
if y+h>max_y:
max_y = y+h
if x+w>max_x:
max_x = x+w
blank.paste(images[i], (x,y))
print('max_x {} max_y {} target_h {}'.format(max_x, max_y, target_height))
blank = blank.convert('1', dither=Image.NONE)
blank.save("59MP.bmp", "BMP")
Код: Выделить всё
0
1000
2000
3000
4000
5000
6000
number of total pixels: 59926682272
starting to pack
finished packing, starting to paste image together
max_x 247246 max_y 247243 target_h 247246
Traceback (most recent call last):
File "stitch_bmps.py", line 75, in
blank.save("59MP.bmp", "BMP")
File "/home/nmz787/.local/lib/python3.7/site-packages/PIL/Image.py", line 2084, in save
save_handler(self, fp, filename)
File "/home/nmz787/.local/lib/python3.7/site-packages/PIL/BmpImagePlugin.py", line 332, in _save
+ o32(offset) # reserved
File "/home/nmz787/.local/lib/python3.7/site-packages/PIL/_binary.py", line 91, in o32le
return pack("
Подробнее здесь: [url]https://stackoverflow.com/questions/59353721/python-pillow-opening-1-bit-depth-files-with-8-bits[/url]
Мобильная версия