0

Python スクリプトを使用して、大きな画像 (10000 x 10000 ピクセル) を連続してつなぎ合わせています。8 枚の画像のうち最初の 6 枚を一度に 1 つずつつなぎ合わせることができます。ただし、この時点を超えてさらに画像をステッチすると、「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.'
4

1 に答える 1