首页 > 解决方案 > 按白天/夜晚对图像进行排序

问题描述

我有一个程序可以根据时间(它的名字)对图像进行排序。我命令他们查看 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)

我想要解决的是图像为IR39_MSG2-SEVI-MSG15-0100-NA-20100110**21**1241IR39_MSG2-SEVI-MSG15-0100-NA-20100111**00**1240或被IR39_MSG2-SEVI-MSG15-0100-NA-20100104**00**1241排序为白天,我不知道为什么(高亮显示的小时)。

问题是 00、03 或 21 等带有 hous 的图像将始终是夜间,而 09 或 12 将始终是白天。

已编辑

所以你可以理解我为什么使用这个标准,我将附上一张照片作为数据框,你可以在其中查看 6 点 18 点是白天还是晚上,具体取决于太阳是否升起:

数据框

看清楚数据并不重要,只是让你了解我为什么要这样做。

PD:我不认为我的图像是在不同的分钟内拍摄的。

谢谢你的帮助。

标签: pythonpython-3.ximagedatesorting

解决方案


问题似乎在第一个条件(mes == 1 or mes == 2 or mes == 11 or mes == 12 and 6 < hora < 18)。将其更改为(mes in (1, 2, 11, 12) and 6 < hora < 18),它应该可以工作:

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) )

印刷:

False
False
False

推荐阅读