Python 按白天/晚上对图像进行排序

Python 按白天/晚上对图像进行排序,python,python-3.x,image,date,sorting,Python,Python 3.x,Image,Date,Sorting,我有一个程序,根据它的时间(它在它的名字上)对图像进行排序。我命令他们看6点和18点是白天还是晚上(因为拍摄间隔为三个小时,所以我不介意前几个小时还是后几个小时) 我用个人标准来做这件事。我在关注西班牙发生的事情 如你所见,我从照片的名称中得到小时、天或月,因为名称如下:IR39_MSG2-SEVI-MSG15-0100-NA-20080701151239 import os , time, shutil directorio_origen = "D:/TR/Eumetsat_IR_

我有一个程序,根据它的时间(它在它的名字上)对图像进行排序。我命令他们看6点和18点是白天还是晚上(因为拍摄间隔为三个小时,所以我不介意前几个小时还是后几个小时)

我用个人标准来做这件事。我在关注西班牙发生的事情

如你所见,我从照片的名称中得到小时、天或月,因为名称如下:
IR39_MSG2-SEVI-MSG15-0100-NA-20080701151239

import os , time, shutil

directorio_origen = "D:/TR/Eumetsat_IR_photos/"
directorio_destino_dia = "D:/TR/IR_Photos/Daytime"
directorio_destino_noche = "D:/TR/IR_Photos/Nighttime"

def get_hour(file_name):
    return file_name[37:39]

def get_day(file_name):
    return file_name[35:37]

def get_month(file_name):
    return file_name[33:35]


for root, dirs, files in os.walk(directorio_origen, topdown=False):

    for fn in files:
        path_to_file = os.path.join(root, fn)
    
        hora = int(get_hour(fn))
        dia = int(get_day(fn))
        mes = int(get_month(fn))
    
        if ((mes == 1 or mes == 2 or mes == 11 or mes == 12 and 6 < hora < 18) or
        (mes == 3 and dia < 17 and 6 < hora < 18) or (mes == 3 and dia == 17 and 6 <= hora < 18) or(mes == 3 and dia > 17 and 6 <= hora <= 18) or
        (4 <= mes <= 8 and 6 <= hora <= 18) or
        (mes == 9 and dia <= 15 and 6 <= hora <= 18) or (mes == 9 and dia >= 16 and 6 <= hora < 18) or
        (mes == 10 and dia <= 13 and 6 <= hora < 18) or (mes == 10 and dia >= 14  and 6 < hora < 18)): 
            new_directorio = directorio_destino_dia
        else:
            new_directorio = directorio_destino_noche
        try:
            if not os.path.exists(new_directorio):
                os.mkdir(new_directorio)
            shutil.copy(path_to_file, new_directorio)

        except OSError:
            print("El archivo no ha podido ser ordenado" % fn)
导入操作系统、时间、时间
directorio\u origen=“D:/TR/Eumetsat\u IR\u photos/”
directorio_destino_dia=“D:/TR/IR_照片/白天”
directorio\u destino\u noche=“D:/TR/IR\u照片/夜间”
def get_小时(文件名):
返回文件名[37:39]
def get_日(文件名):
返回文件名[35:37]
def get_月(文件名):
返回文件名[33:35]
对于os.walk(directorio\u origen,top-down=False)中的根目录、目录和文件:
对于文件中的fn:
path_to_file=os.path.join(根目录,fn)
hora=int(获取小时(fn))
直径=整数(获取日(fn))
mes=int(每月(fn))
如果((mes==1或mes==2或mes==11或mes==12和6(mes==3和dia<17和6(mes==1或mes==2或mes==11或mes==12和6
。将其更改为
(mes in(1,2,11,12)和6
,它应该可以工作:

def is_day_time(mes, dia, hora):

    if (
        (mes in (1, 2, 11, 12) and 6 < hora < 18) or
        (mes == 3 and dia < 17 and 6 < hora < 18) or 
        (mes == 3 and dia == 17 and 6 <= hora < 18) or
        (mes == 3 and dia > 17 and 6 <= hora <= 18) or
        (4 <= mes <= 8 and 6 <= hora <= 18) or
        (mes == 9 and dia <= 15 and 6 <= hora <= 18) or 
        (mes == 9 and dia >= 16 and 6 <= hora < 18) or
        (mes == 10 and dia <= 13 and 6 <= hora < 18) or 
        (mes == 10 and dia >= 14  and 6 < hora < 18)
        ):
        return True
    return False

print( is_day_time(1, 10, 21) )
print( is_day_time(1, 11, 0) )
print( is_day_time(1, 4, 0) )

旁注:您的第二个字符串开头缺少一个
I
。但是,您的代码希望您的字符串正好是某个长度。RegEx适合提取小时信息的任务,例如,感谢您的帮助@TinNguyen我没有注意到。感谢您的帮助,为什么不使用(mes==1或mes==2或mes==11或mes==12和6((mes==1或mes==2或mes==11或mes==12)和6
False
False
False