【问题标题】:Sort images by day/night按白天/夜晚对图像进行排序
【发布时间】:2021-02-15 04:12:33
【问题描述】:

我有一个程序可以根据时间(在它的名字上)对图像进行排序。我命令他们查看 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**1240IR39_MSG2-SEVI-MSG15-0100-NA-20100104**00**1241 的图像被排序为白天,我不知道为什么(高亮显示的小时)。

问题是 00、03 或 21 之类的图像将始终是夜间,而 09 或 12 将始终是白天。

已编辑

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

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

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

感谢您的帮助。

【问题讨论】:

  • 旁注:您的第二个字符串开头缺少I。但是,您的代码希望您的字符串具有一定的长度。 RegEx 适用于提取小时信息的任务,例如regex101.com/r/uzU6F4/1
  • 感谢您的帮助@TinNguyen 我没注意到。

标签: python python-3.x image date sorting


【解决方案1】:

问题似乎在第一个条件(mes == 1 or mes == 2 or mes == 11 or mes == 12 and 6 &lt; hora &lt; 18)。将其更改为(mes in (1, 2, 11, 12) and 6 &lt; hora &lt; 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

【讨论】:

  • 感谢您的帮助,为什么不使用 (mes == 1 or mes == 2 or mes == 11 or mes == 12 and 6
  • @JoanCarlesMonteroJiménez 这就是操作员在 python 中的工作方式。为了使上面的工作,您应该将“或”部分放在括号内:((mes == 1 or mes == 2 or mes == 11 or mes == 12) and 6 &lt; hora &lt; 18)
猜你喜欢
  • 1970-01-01
  • 2021-12-27
  • 2016-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多