python PIL错误

时间:2017-03-20 18:55:50

标签: python matplotlib python-imaging-library pillow

尝试编写一个python脚本,该脚本从文件中获取图像,调整它们的大小,并将它们放在一个新文件中,然后将所有这些图像相互组合。这是我所拥有的,但我得到了很多错误,只能调整大小和重命名图像,而不是组合。救命啊!

from __future__ import print_function
import PIL
import matplotlib.pyplot as plt # single use of plt is commented out
import os.path  
import PIL.ImageDraw     
import glob
from PIL import Image


import os, sys
path = "U:/img/"
dirs = os.listdir(path)

def resize():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            f, e = os.path.splitext(path+item)
            imResize = im.resize((400,400), Image.ANTIALIAS)
            imResize.save('U:/new/' + item + ' resized.jpg', 'JPEG', quality=90)
             if IOError:
                 pass
         if IOError:
             pass
         if OSError:
             pass
     return

 def combine():
     resize()
     files = [ 
        'U:/new/dinosaur_stock___14_by_nigav_stock-d68h3eh.jpg resized'
        'U:/new/imgres.jpg resized'
        'U:/new/stock-footage-guide1.jpg resized'
    ] 
     result = Image.new("RGB", (800, 800)) 


    for index, file in enumerate(files): 
        path = os.path.expanduser(file) 
        img = Image.open(path) 
        img.thumbnail((400, 400), Image.ANTIALIAS) 
        x = index // 2 * 400 
        y = index % 2 * 400 
        w, h = img.size 
        print('pos {0},{1} size {2},{3}'.format(x, y, w, h)) 
        result.paste(img, (x, y, x + w, y + h)) 


        result.save(os.path.expanduser('~/image.jpg'))
        if IOError:
            pass
        if OSError:
            pass 
combine()

0 个答案:

没有答案