defFD_judge(path): result = "" if"*"in path: result = "Files" elif os.path.isdir(path): result = "Directory" elif os.path.isfile(path): result = "File" else: result = "path is incorrect" return result
print(FD_judge(INPUT)) Typ_in = FD_judge(INPUT)
3 Resize Determine
import PIL.Image as Image
defCalc_WH(w,h,W_img,H_img,R_img): if W_img != "NA"and H_img == "NA": #Resize by Width print('Resize by Width') R = w/ int(W_img) w=int(w/R) h=int(h/R) elif H_img != "NA"and W_img == "NA": #Resize by Height print("Resize by Height") R = h/ int(H_img) w=int(w/R) h=int(h/R) elif H_img != "NA"and W_img != "NA": #Resize by Width and Height print('Resize by Width and Height') w=int(W_img) h=int(H_img) else: print("Resize by ratio, R="+str(R_img)) w=int(w/R_img) h=int(h/R_img) return w,h
reading all pictures from OUT directory, and all result where out put to test file
update
Print information
2020/2/9
Adding arguments
parser.add_argument('-inf','-INF','--infor',default = "None") #resize by Height
INFORM = args.infor
Def functions:
defResize_loop(): if Typ_in == "File": Result = Resize(INPUT,W_img,H_img,R_img) Result.save(OUTPUT, quality=Quality) elif Typ_in == "Files": List = os.popen("ls "+INPUT).read().split('\n')[:-1] for i in List: Result = Resize(i,W_img,H_img,R_img) Result.save(OUTPUT + i.split('/')[-1], quality=Quality) elif Typ_in == "Directory": List = os.popen("ls "+INPUT+"/*").read().split('\n')[:-1] for i in List: Result = Resize(i,W_img,H_img,R_img) Result.save(OUTPUT+i.split('/')[-1], quality=Quality)
defIMG_inf(INPUT): Space = size_format(getsize(INPUT)) Img = Image.open(INPUT) Name = Img.filename Format = Img.format_description Mode = Img.mode
try: Bit = "bit:" + str(Img.bits) except: Bit = "bit:NA" try: Dpi = "dpi:" + 'x'.join([str(x) for x in Img.info['dpi']]) except: Dpi = "dpi: NA" Size = "size:" + 'x'.join([str(x) for x in Img.size]) Result = Name +"\t"+" ".join([Space, Size, Dpi, Format,Mode,Bit]) return Result
Logic:
if INFORM == "None": # Resize Typ_in = FD_judge(INPUT) print(Typ_in) OUTPUT = OUT_fig(INPUT,OUTPUT) Resize_loop() else: # img information Typ_in = FD_judge(INPUT) if Typ_in == "File": print(IMG_inf(INPUT)) elif Typ_in == "Files": #List = os.popen("ls "+INPUT).read().split('\n')[:-1] List = os.popen("ls "+INPUT).read().split('\n\n')[0].split('\n')[:-1] for i in List: print(IMG_inf(i)) elif Typ_in == "Directory": List = os.popen("ls "+INPUT+"/*").read().split('\n\n')[0].split('\n')[:-1] for i in List: print(IMG_inf(i))