看世界
 
昨日:篇  今日:篇   总帖:篇   会员:
admin
创始人Lv2   
图片按比例输出     
# -*- coding: utf-8 -*-

import os
import os.path
from PIL import Image
import shutil
list1 = []
def get_py(path,list1):
    fileList = os.listdir(path) # 获取path目录下所有文件
    for filename in fileList:
        pathTmp = os.path.join(path,filename) # 获取path与filename组合后的路径
        if os.path.isdir(pathTmp):  # 如果是目录
            get_py(pathTmp,list1) # 则递归查找
        elif filename[-4:] == '.jpg':# 如果不是目录,则比较后缀名
            list1.append(pathTmp)

# path = 'D:\web\PHPnow-1.5.6\\vhosts\img.pictutu.com\\img'

path = r'D:\web\PHPnow-1.5.6\vhosts\img.pictutu.com\img\list\1'


path = 'z:\\100'


# path = 'z:\\TEST'
get_py(path,list1)
print('在%s目录及其子目录下找到%d个jpg文件,分别为:' % ( path,len(list1)))
for filename in list1:
    # print(path)

    img = Image.open(filename)
    w = float(img.size[0])
    h = float(img.size[1])
    print w
    print h

    b= w/h
    if  b > 1.25 :
        path1 = "z:\\new\\1\\"

        print (b)

        # path1 = "z:\\N123\\1\\"
        path1 = r'D:\web\PHPnow-1.5.6\vhosts\img.pictutu.com\img\list\list1'
        shutil.copy(filename, path1)
        # print 'type1:' + str(len(filename))

    elif  0.9< b<=1.25:

        # print '正方形图片'
        # path2 = "z:\\N123\\2\\"

        # path2 = "z:\\new\\2\\"
        path2 = r'D:\web\PHPnow-1.5.6\vhosts\img.pictutu.com\img\list\list2'
        shutil.copy(filename, path2)
        # print'type2:' + str(len(filename))




    else:
        # print "竖型图片"
        # path3 = "z:\\new\\3\\"
        # path3 = "z:\\N123\\3\\"
        path3 = r'D:\web\PHPnow-1.5.6\vhosts\img.pictutu.com\img\list\list3'
        shutil.copy(filename, path3)





 0  已被阅读了15次  楼主 2018-10-19 21:37:42
回复列表

回复:图片按比例输出

LOOK官方站 联系站长
Powered by Look博客