2014-08-30 63 views
0

我正在使用python腳本將大圖像(10000乘10000像素)拼接在一起。我可以一次將八張圖像中的前六張拼接在一起,絕對沒問題。但是,當我縫合更多的圖像超出這一點時,我得到'Python.exe已停止工作'。下面將大圖像拼接在一起--Python.exe已停止工作

代碼:

from PIL import Image 
import getopt, sys 

args = sys.argv 
ImageFileList = [] 
MasterWidth = 0 
MasterHeight = 0 
filename = "" 
row = True 

print """ 
Usage: python imageconnector.py [OPTIONS] [FILENAME] [FILE1] [FILE2] ...[FILE...]... 
Combines [FILE1,2,...] into one file called [FILENAME] 

OPTIONS: 

-o <r/c>  Stitch images into a row or a column. Default is row. 
-c <colour>  Change background fill colour. Default is black. 

""" 

def main(argv): 
    global args, MasterWidth, MasterHeight, ImageFileList, filename, deletename 
    try: 
     opts, args_files = getopt.getopt(argv, 'o:c:') 
    except getopt.GetoptError: 
     print "Illegal arguments!" 
     sys.exit(-1) 

    if '-o' in args: 
     index = args.index('-o') 
     cr = args[index + 1] 

     if cr == 'r': 
      row = True 

    elif cr == 'c': 
     row = False 

    else: 
     row = True 

    if '-c' in args: 
     index = args.index('-c') 
     colour = args[index + 1] 

    else: 
     colour = 'black' 

    filename = args_files.pop(0) 

    print('Combining the following images:') 
    if row: 
     for x in args_files: 

      try: 
       im = Image.open(x) 
       print(x) 


       MasterWidth += im.size[0] 
       if im.size[1] > MasterHeight: 
        MasterHeight = im.size[1] 
       else: 
        MasterHeight = MasterHeight 


       ImageFileList.append(x) 
      except: 
       raise 

     final_image = Image.new("RGB", (MasterWidth, MasterHeight), colour) 
     offset = 0 
     for x in ImageFileList: 
      temp_image = Image.open(x) 
      final_image.paste(temp_image, (offset, 0)) 
      offset += temp_image.size[0] 

     final_image.save(filename) 
    else: 
     for x in args_files: 

      try: 
       im = Image.open(x) 
       print(x) 


       MasterHeight += im.size[1] 
       if im.size[0] > MasterWidth: 
        MasterWidth = im.size[0] 
       else: 
        MasterWidth = MasterWidth 


       ImageFileList.append(x) 
      except: 
       raise 
     final_image = Image.new("RGB", (MasterWidth, MasterHeight), colour) 
     offset = 0 
     for x in ImageFileList: 
      temp_image = Image.open(x) 
      final_image.paste(temp_image, (0, offset)) 
      offset += temp_image.size[1] 

     final_image.save(filename) 

if __name__ == "__main__": 
    try: 
    main(sys.argv[1:]) 
    except IOError: 
    print 'One of more of the input image files is not valid.' 
    sys.exit(-1) 

    except SystemExit: 
    pass 

    except ValueError: 
    print 'Not a valid colour value.' 
+0

看起來你的縮進是關閉的(從第一個if)..? – thebjorn 2014-08-30 10:58:53

+0

你的文件有多大(每像素多少位)?你使用的是32位的Python嗎? (如果是這樣,你的圖像的組合大小是否適合可用內存?) – thebjorn 2014-08-30 11:02:02

+0

你永遠不會關閉'im',因此最終會耗盡所有可用內存。當你完成它時你需要關閉「im'。 – 2014-08-30 11:21:32

回答