Python Resize Multiple images ask user to continue

103 Views Asked by At

I'm trying to make a script that resize multiple or a single image based on a data pulled from XML. My question is if i have multiple images how can I print out a qusetion like "There are more than 1 image do you wish to resize image 2 also?... than maybe " Would you liek to resize image 3 also ?"

My script so far is as follow,the only problem is taht it resizez all the images at start :

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)  

Thank you in advance.

Zapo

1

There are 1 best solutions below

1
On BEST ANSWER

Change your final loop to:

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