12.09.2016 г.

Show image, with options to resize it.

 #IMPORT SOME MODULE NEED pip install pillow  
 #if not esists  
 import os, sys, Tkinter  
 from PIL import Image, ImageTk  
 #FIRST GLOBAL PARAM TO PATH  
 #USES Pictures folder of user that run script  
 PIC = os.path.expanduser('~')+"\Pictures"  
 #CALCULATE RESULT IS HERE  
 list_with_pic = []  
 old_label_image = None  
 #FUNCTION CLOSE PICTURE WHEN PUSH LEFT MOUSE BOTTON  
 def button_click_exit_mainloop (event):  
   """FUNCTION CLOSE PICTURE WHEN PUSH LEFT MOUSE BOTTON"""  
   event.widget.quit() # this will cause mainloop to unblock.  
 #THIS SEARCH FOR PICTURES IN PIC FOLDER  
 #RETURN ABSOLUTE PATH  
 def search_picture(PATH):  
   """THIS SEARCH FOR PICTURES IN PIC FOLDER / RETURN ABSOLUTE PATH"""  
   for root_p, dirname, filename in os.walk(PATH):  
     for file in filename:  
       if file.endswith(("jpg","png")):  
         yield os.path.join(root_p, file)  
 #RETURN RESULT SPARES MEMORY  
 def return_orig():  
   """RETURN RESULT SPARES MEMORY"""  
   for result in search_picture(PIC):  
     yield result  
 #SHOW PICTURES USE TKINTER TO VISUALIZE  
 def show_pic():  
   """SHOW PICTURES USE TKINTER TO VISUALIZE"""  
   len_of_pictures = [] #CALCULATE RESULT  
   for m in return_orig():  
     len_of_pictures.append(m)  
   print ""  
   #PRINT TOTAL LEN OF FOUND PICTURE'S  
   print "The total len of pictures is {}".format(len(len_of_pictures))  
   print ""  
   #ASK TO OPEN PICTURE FOR VIEW OR NOT, TKINTER CLOSE WHEN HIT N/n or somthing else  
   ask = raw_input("Do you want to open all image's : Y or N _\b" )  
   if ask == 'Y' or ask == 'y':  
     root = Tkinter.Tk()  
     root.bind("<Button>", button_click_exit_mainloop)  
     root.geometry('+%d+%d' % (200,200))  
     for root_d, dirname, filename in os.walk(PIC):  
         for file in filename:  
           if file.endswith(("jpg","png")):  
             list_with_pic.append(os.path.join(root_d, file))  
     for result in list_with_pic:  
       #OPEN IMAGE  
       image1 = Image.open(result)  
       #ORIGINAL GEOMETRY IS HERE TAKE FROM IMAGE  
       root.geometry('%dx%d' % (image1.size[0],image1.size[1]))  
       tk_pik = ImageTk.PhotoImage(image1)  
       #LABEL IS HERE SHOW IN TOP OF THE TKINTER CANVAS  
       label_image = Tkinter.Label(root, image=tk_pik)  
       #PLACE TO POSITION  
       label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])  
       root.title(result)  
       if old_label_image is not None:  
         old_label_image.destroy()  
       odl_lavel_image = label_image  
       root.mainloop()  
   #IF ASK IS N/n exit from program  
   elif ask == 'N' or ask == 'n':  
     print "Refused by the user"  
     sys.exit()  
   else:  
     sys.exit()  
 #THIS USE TO RESIZE A PICTURE'S  
 def resize_pic():  
   """THIS USE TO RESIZE A PICTURE'S"""  
   if os.path.exists(PIC):  
     TUMBNAILS = "thumbnail"  
     if os.path.exists(PIC+'\\'+TUMBNAILS):  
       TUMBNAILS = PIC+'\\'+TUMBNAILS  
     else:  
       os.makedirs(PIC+'/'+TUMBNAILS)  
       TUMBNAILS = PIC+'\\'+TUMBNAILS  
   #ASK TO USER PUSH WIDTH AND HEIGHT OF THUMBNAIL  
   width = raw_input("Enter width :")  
   height = raw_input("Enter height :")  
   for result in return_orig():  
     image = Image.open(result)  
     image = image.resize((int(width), int(height)), Image.ANTIALIAS)  
     image.save(TUMBNAILS+'\\'+result.split('\\')[-1], 'jpeg', quality=90)  
 def ask_for_what():  
   print "-------------------------------------------"  
   print "1 - For list pictures with original size -"  
   print "2 - Corect pictures to tumbnails     -"  
   print "-------------------------------------------"  
   answer = raw_input('Choice whant you want :')  
   print ""  
   print "You choice {answer}".format(answer=answer)  
   if answer == '1':  
     show_pic()  
   elif answer == '2':  
     resize_pic()  
   else:  
     "???"  
 if __name__ == "__main__":  
   print "Default path is {path}".format(path=PIC)  
   print ""  
   PIC = raw_input("Enter for Default or write absolute path to pictures :")  
   if PIC == "":  
     PIC = os.path.expanduser('~')+"\Pictures"  
   else:  
     PIC = PIC  
   ask_for_what()  

Няма коментари :

Публикуване на коментар