python - 按白天/夜晚对图像进行排序
问题描述
我有一个程序可以根据时间(它的名字)对图像进行排序。我命令他们查看 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**1241
,IR39_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:我不认为我的图像是在不同的分钟内拍摄的。
谢谢你的帮助。
解决方案
问题似乎在第一个条件(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
推荐阅读
- ansible - Ansible Collections:如何使用 Google Cloud Compute Engine 库存源
- c++ - 如何转发打包的可变参数
- c# - 从 PropertyChanged 中删除 lambda 表达式(相当于 p.PropertyChanged -= (s,a) => { ... })
- android - 是否可以将状态栏覆盖在活动上,但不能覆盖导航栏
- android - Android Studio 4.0.2 Gradle 错误:没有为属性“enableGradleWorkers”指定值
- php - Laravel 8 护照 UUIDS
- java - 限制java中的特殊变量
- reactjs - 使用 ul 和 li 反应 js 树视图
- wordpress - 是否可以在帖子中禁用古腾堡,但不能在 wordress 的页面上禁用?
- python - 如何在没有 Colab 打印功能的 PyCharm 中打印项目?