본문 바로가기
파이썬 언어

Python으로 사진 합치기

by treeCoder 2021. 9. 21.

from __future__ import print_function
import os
import sys

import PIL
from PIL import Image

def getOutputFileName():
  maxSufix = 9999
  for i in range( 1, maxSufix + 1 ):
      fileName = 'output' + str(i) + '.jpg'
      if os.path.isfile(fileName):
        pass
      else:
        return fileName
      
  if i >= maxSufix :
    print("can't get output file name.")
    print("too many output files.")
    print("program halt.")
    
    sys.exit()
  
files = [
  'V1.jpg',
  'V2.jpg']

v1=Image.open(files[0])
v2=Image.open(files[1])

w, h = v1.size

w1 = min(w, h)
h1 = max(w, h)

new_im = Image.new('RGB', (w1+w1, h1))

x_offset = 0
for im in files:
  tmp=Image.open(im)
  new_im.paste(tmp, (x_offset,0))
  x_offset += w1

new_im.show()

sys.exit()

fn = getOutputFileName()

new_im.save(os.path.expanduser(fn))
new_im.show()




댓글