2016-07-17 7 views
1

ディレクトリのイメージをぼかします。これらの画像から3x6キャンバスを作成して、そのディレクトリの画像を1つの画像/キャンバスに並べて表示する新しい画像を作成します。各画像は異なる画像でなければなりません。並んで。 -ディレクトリから複数の異なるイメージを3x6のキャンバスに結合する

私は次のコードを持っています。ディレクトリに格納されているイメージファイル名をリストに読み込みます。次に、各画像を3x6キャンバスにコピー/結合しようとします。しかし、私が望む結果は起こらない。 私は間違っていますか?

import Image 
import os 
import PIL 
import glob 
import matplotlib.pyplot as plt 

# path 
path = "/media/" 
listing = os.listdir(path) 

# getting all path+filename in a list 
npath=[] 
im=[] 
for infile in listing: 
    im.append(infile) 
    npath.append(os.path.join(path, infile)) 

#creates a new empty image, RGB mode, and size 400 by 400. 
new_im = Image.new('RGB', (2100,2400)) 


#Here I resize my opened image, so it is no bigger than **** 
#Iterate through a grid with some spacing, to place my image 
for i in xrange(0,2100,700): 
    for j in xrange(0,2400, 400): 
     for imagefile in npath: 
      im=Image.open(imagefile) 
      im.thumbnail((1000,1000)) 
      #paste the image at location i,j: 
      new_im.paste(im, (i,j)) 
      new_im.show() 
#saving 
new_im.save('/media/test.png') 

ソリューション

import Image 
import os 
import PIL 
import glob 
import matplotlib.pyplot as plt 

# path 
path = "/media/" 
listing = os.listdir(path) 


# getting all path+filename in a list 
npath=[] 
im=[] 

for infile in listing: 
    im.append(infile) 
    npath.append(os.path.join(path, infile)) 

#creates a new empty image, RGB mode, and size 400 by 400. 
new_im = Image.new('RGB', (2500,3000)) 

for i in xrange(0,2500,800): 
    for j in xrange(0,3000, 500): 
     im=Image.open(npath.pop(0)) 
     im.thumbnail((1000,1000)) 
     #paste the image at location i,j: 
     new_im.paste(im, (i,j)) 
    new_im.save('/media/test.png') 
+0

あなたが期待していることは明確ではありません。何がうまくいかないのですか?そしてそれはどうやって期待していますか? – rfkortekaas

+0

ディレクトリのイメージを表示します。私はそれらの画像から3x6キャンバスを作成したい。各画像は異なる画像でなければなりません。並んで。 @ rfkortekaas –

+0

しかし、あなたが持っている問題は何ですか?画像は間違った場所にありますか?エラーがありますか? – rfkortekaas

答えて

4

位置毎の画像一覧を反復しないが、代わりにリストを消費:

for i in xrange(0, 2100, 700): 
    for j in xrange(0, 2400, 400): 
     try: 
      filepath = npath.pop(0) 
     except IndexError: 
      break 
     im = Image.open(filepath) 
     im.thumbnail((1000,1000)) 
     # paste the image at location i,j 
     new_im.paste(im, (i,j)) 
    else: 
     continue # executed if inner loop ended normally (no break) 
    break # executed if 'continue' was skipped (break occurred) 
+0

npathが以前にデータでいっぱいになっている間、次のエラーが発生します。ただし、ループが空の後。 'トレースバック(最新の最後の呼び出し): ファイルrun_code内の" /usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py "、行2820、 exec code_obj in self.user_global_ns、self.user_ns ファイル ""、行25、 im = Image.open(npath.pop(0)) IndexError:空のリストからポップアップする –

+0

はい、popがリストを消費しています。見た目には、リストに十分な画像があります。空のリストをチェックするためにtry catchを追加してください – rfkortekaas

+0

パス変数に18個のイメージを置いています。再び同じエラーが発生します。 –

1

私はあなたが何をしようとして考えるのかもしれません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 
0

あなたはスーパーフォルダ内の画像を課すことをしたい場合は、 Pythonでblend()を使用してください。

関連する問題