2015-06-15 26 views
0

我試圖製作一個腳本,基於從XML中抽取的數據調整多個或單個圖像的大小。 我的問題是如果我有多個圖像我怎樣才能打印出像「有超過1張圖片你是否想要調整圖片2的大小?......也許你會想要調整圖片3的大小嗎?」Python調整大小多個圖像請求用戶繼續

我的腳本到目前爲止是如下,唯一的問題是taht它resizez所有圖像的開始:

import os, glob 
import sys 
import xml.etree.cElementTree as ET 
import re 
from PIL import Image 

pathNow ='C:\\' 


items = [] 
textPath = [] 
imgPath = [] 

attribValue = [] 

#append system argument to list for later use 

for item in sys.argv: 
    items.append(item) 

#change path directory 
newPath = pathNow + items[1] 
os.chdir(newPath) 
#end 

#get first agrument for doc ref 
for item in items: 
    docxml = items[2] 

#search for file 
for file in glob.glob(docxml + ".xml"): 
    tree = ET.parse(file) 
    rootFile = tree.getroot() 
    for rootChild in rootFile.iter('TextElement'): 
     if "svg" or "pdf" in rootChild.text: 
      try: 
       textPath = re.search('svg="(.+?)"', str(rootChild.text)).group(1) 
       attribValue.append(rootChild.get('elementId')) 
       imgPath.append(textPath) 
      except: 
       continue 

for image in imgPath: 
    new_img_path = image[:-4] + '.png' 
    new_image = Image.open(new_img_path) 
    new_size=int(sys.argv[3]), int(sys.argv[4]) 
    try: 
     new_image.thumbnail(new_size, Image.ANTIALIAS) 
     new_image.save(new_img_path, 'png') 
    except IOError: 
     print("Cannot resize picture '%s'" % new_img_path) 
    finally: 
     new_image.close() 
     print("Done resizeing image: %s " % new_img_path) 

預先感謝您

ZAPO

回答

1

影響您的最終環路:

for idx, image in enumerate(imgPath): 
    #img resizing goes here 

    count_remaining = len(imgPath) - (idx+1) 
    if count_remaining > 0: 
     print("There are {} images left to resize.".format(count_remaining)) 
     response = input("Resize image #{}? (Y/N)".format(idx+2)) 
     #use `raw_input` in place of `input` for Python 2.7 and below 
     if response.lower() != "y": 
      break 
+0

非常感謝,它的工作。我會更深入地解釋你的解釋,因爲我真的不明白這個漏洞。但它做了這項工作。再次感謝你。 – Victor