首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按白天/夜晚对图像进行排序

按白天/夜晚对图像进行排序
EN

Stack Overflow用户
提问于 2020-11-02 13:40:02
回答 1查看 158关注 0票数 2

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

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

如您所见,我从照片的名称中得到时间、日期或月份,因为名称类似于:IR39_MSG2-SEVI-MSG15-0100-NA-20080701151239

代码语言:javascript
运行
复制
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:我不认为我的照片是在不同的分钟内拍摄的。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-02 13:49:13

这个问题在第一个条件下似乎是(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),它应该可以工作:

代码语言:javascript
运行
复制
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) )

指纹:

代码语言:javascript
运行
复制
False
False
False
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64646656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档