私はあなたが何をしようとして考えるのかもしれませんPEP 342 — Coroutines via Enhanced Generatorsに記載されているコルーチンを使用することで非常に一般的な方法で解決されました。以下は、任意のサイズのグリッド上にサムネイル画像の作成とレイアウトを処理するためのコードです。複数のサムネイルページが生成される可能性があります。そのページ数は、画像がいくつあり、一杯になるまでグリッドに収まるかによって異なります。
私は、その柔軟性を高めるためにハードコーディングされた数字を使用することを避けようとしました。サムネイルサイズとグリッドレイアウトの両方が変数になりました。
注:サムネイル出力イメージを実際に作成して保存するすべての呼び出しは、テストモードで簡単かつ迅速に実行できるようにコメントアウトされています。実際に出力イメージを生成するには、それらのコメントを外す必要があります。ここで
from glob import iglob
from PIL import Image
import os
def thumbnailer(thumbpath, grid, thumb_size, background_color):
""" Coroutine to receive image file names and produce thumbnail pages of
them laid-out in a grid.
"""
page_num = 0
page_extent = grid[0]*thumb_size[0], grid[1]*thumb_size[1]
try:
while True:
paste_cnt = 0
#new_img = Image.new('RGB', page_extent, background_color)
for x in xrange(0, page_extent[0], thumb_size[0]):
for y in xrange(0, page_extent[1], thumb_size[1]):
try:
filepath = (yield)
except GeneratorExit:
print('GeneratorExit received')
return
filename = os.path.basename(filepath)
print('{} thumbnail -> ({}, {})'.format(filename, x, y))
#thumbnail_img = Image.open(filepath)
#thumbnail_img.thumbnail(thumb_size)
#new_img.paste(thumbnail_img, (x,y))
paste_cnt += 1
else:
continue # no break, continue outer loop
break # break occurred, terminate outer loop
print('====> thumbnail page completed')
if paste_cnt:
page_num += 1
print('Saving thumbpage{}.png'.format(page_num))
#img.save(
# os.path.join(thumbpath, 'thumbpage{}.png'.format(page_num)))
finally:
print('====> finally')
if paste_cnt:
page_num += 1
print('Saving thumbpage{}.png'.format(page_num))
#img.save(
# os.path.join(thumbpath, 'thumbpage{}.png'.format(page_num)))
path = '/media'
#npath = [infile for infile in iglob(os.path.join(path, '*.png'))]
npath = ['image{}.png'.format(i) for i in xrange(1, 37+1)] # test names
coroutine = thumbnailer(path, (3,6), (1000,1000), 'white')
coroutine.next() # start it
for filepath in npath:
coroutine.send(filepath)
print('====> closing coroutine')
coroutine.close()
がレイアウトされた3×6のグリッド時37個のダミーの画像ファイルから3つのサムネイルページを生成する上からの出力です:
image1.png thumbnail -> (0, 0)
image2.png thumbnail -> (0, 1000)
image3.png thumbnail -> (0, 2000)
image4.png thumbnail -> (0, 3000)
image5.png thumbnail -> (0, 4000)
image6.png thumbnail -> (0, 5000)
image7.png thumbnail -> (1000, 0)
image8.png thumbnail -> (1000, 1000)
image9.png thumbnail -> (1000, 2000)
image10.png thumbnail -> (1000, 3000)
image11.png thumbnail -> (1000, 4000)
image12.png thumbnail -> (1000, 5000)
image13.png thumbnail -> (2000, 0)
image14.png thumbnail -> (2000, 1000)
image15.png thumbnail -> (2000, 2000)
image16.png thumbnail -> (2000, 3000)
image17.png thumbnail -> (2000, 4000)
image18.png thumbnail -> (2000, 5000)
====> thumbnail page completed
Saving thumbpage1.png
image19.png thumbnail -> (0, 0)
image20.png thumbnail -> (0, 1000)
image21.png thumbnail -> (0, 2000)
image22.png thumbnail -> (0, 3000)
image23.png thumbnail -> (0, 4000)
image24.png thumbnail -> (0, 5000)
image25.png thumbnail -> (1000, 0)
image26.png thumbnail -> (1000, 1000)
image27.png thumbnail -> (1000, 2000)
image28.png thumbnail -> (1000, 3000)
image29.png thumbnail -> (1000, 4000)
image30.png thumbnail -> (1000, 5000)
image31.png thumbnail -> (2000, 0)
image32.png thumbnail -> (2000, 1000)
image33.png thumbnail -> (2000, 2000)
image34.png thumbnail -> (2000, 3000)
image35.png thumbnail -> (2000, 4000)
image36.png thumbnail -> (2000, 5000)
====> thumbnail page completed
Saving thumbpage2.png
image37.png thumbnail -> (0, 0)
====> closing coroutine
GeneratorExit received
====> finally
Saving thumbpage3.png
あなたが期待していることは明確ではありません。何がうまくいかないのですか?そしてそれはどうやって期待していますか? – rfkortekaas
ディレクトリのイメージを表示します。私はそれらの画像から3x6キャンバスを作成したい。各画像は異なる画像でなければなりません。並んで。 @ rfkortekaas –
しかし、あなたが持っている問題は何ですか?画像は間違った場所にありますか?エラーがありますか? – rfkortekaas