Spaces:
Sleeping
Sleeping
Update pages/lastimev4.py
Browse files- pages/lastimev4.py +8 -24
pages/lastimev4.py
CHANGED
|
@@ -117,7 +117,7 @@ async def download_images(bbox, width, height, available_years):
|
|
| 117 |
tasks = [fetch_image(session, get_wms_url(bbox, width, height, date), date, semaphore) for date in available_years]
|
| 118 |
return await asyncio.gather(*tasks)
|
| 119 |
|
| 120 |
-
def process_images_stream(images, format_option, speed, temp_dir
|
| 121 |
results = {}
|
| 122 |
|
| 123 |
if "GIF" in format_option:
|
|
@@ -136,31 +136,15 @@ def process_images_stream(images, format_option, speed, temp_dir, available_year
|
|
| 136 |
writer.append_data(np.array(img))
|
| 137 |
results["MP4"] = mp4_path
|
| 138 |
|
| 139 |
-
if "Images
|
| 140 |
zip_path = os.path.join(temp_dir, "images.zip")
|
| 141 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 142 |
-
|
| 143 |
-
for i, (img, year) in enumerate(zip(images, available_years)):
|
| 144 |
if img is not None:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
# Sauvegarde de l'image en JPEG
|
| 148 |
-
img_path = os.path.join(temp_dir, f"image_{year}.jpg")
|
| 149 |
-
rgb_img.save(img_path, format="JPEG", quality=85) # Ajustez la qualité selon vos besoins
|
| 150 |
zipf.write(img_path, os.path.basename(img_path))
|
| 151 |
-
os.
|
| 152 |
-
|
| 153 |
-
# Création du lien WMS pour cette image
|
| 154 |
-
wms_url = get_wms_url(bbox, width, height, year)
|
| 155 |
-
info_file_content.append(f"{year}: {wms_url}")
|
| 156 |
-
|
| 157 |
-
# Ajout du fichier texte avec les informations
|
| 158 |
-
info_file_path = os.path.join(temp_dir, "image_info.txt")
|
| 159 |
-
with open(info_file_path, 'w') as f:
|
| 160 |
-
f.write("\n".join(info_file_content))
|
| 161 |
-
zipf.write(info_file_path, "image_info.txt")
|
| 162 |
-
os.remove(info_file_path)
|
| 163 |
-
|
| 164 |
results["ZIP"] = zip_path
|
| 165 |
|
| 166 |
return results
|
|
@@ -265,7 +249,7 @@ def app():
|
|
| 265 |
logger.info(f"Récupération réussie de {len(images)} images")
|
| 266 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 267 |
with st.spinner('Traitement des images en cours... Cela peut prendre un certain temps pour les grandes images.'):
|
| 268 |
-
results = process_images_stream(images, format_option, speed, temp_dir
|
| 269 |
|
| 270 |
for format, path in results.items():
|
| 271 |
if os.path.exists(path):
|
|
@@ -273,7 +257,7 @@ def app():
|
|
| 273 |
st.success("Images individuelles (ZIP) créées avec succès!")
|
| 274 |
else:
|
| 275 |
st.success(f"Timelapse {format} créé avec succès!")
|
| 276 |
-
st.markdown(get_binary_file_downloader_html(path, f'{
|
| 277 |
else:
|
| 278 |
st.error(f"Le fichier {format} n'a pas été créé avec succès.")
|
| 279 |
else:
|
|
|
|
| 117 |
tasks = [fetch_image(session, get_wms_url(bbox, width, height, date), date, semaphore) for date in available_years]
|
| 118 |
return await asyncio.gather(*tasks)
|
| 119 |
|
| 120 |
+
def process_images_stream(images, format_option, speed, temp_dir):
|
| 121 |
results = {}
|
| 122 |
|
| 123 |
if "GIF" in format_option:
|
|
|
|
| 136 |
writer.append_data(np.array(img))
|
| 137 |
results["MP4"] = mp4_path
|
| 138 |
|
| 139 |
+
if "Individual Images (ZIP)" in format_option:
|
| 140 |
zip_path = os.path.join(temp_dir, "images.zip")
|
| 141 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 142 |
+
for i, img in enumerate(images):
|
|
|
|
| 143 |
if img is not None:
|
| 144 |
+
img_path = os.path.join(temp_dir, f"image_{i}.png")
|
| 145 |
+
img.save(img_path)
|
|
|
|
|
|
|
|
|
|
| 146 |
zipf.write(img_path, os.path.basename(img_path))
|
| 147 |
+
os.unlink(img_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
results["ZIP"] = zip_path
|
| 149 |
|
| 150 |
return results
|
|
|
|
| 249 |
logger.info(f"Récupération réussie de {len(images)} images")
|
| 250 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 251 |
with st.spinner('Traitement des images en cours... Cela peut prendre un certain temps pour les grandes images.'):
|
| 252 |
+
results = process_images_stream(images, format_option, speed, temp_dir)
|
| 253 |
|
| 254 |
for format, path in results.items():
|
| 255 |
if os.path.exists(path):
|
|
|
|
| 257 |
st.success("Images individuelles (ZIP) créées avec succès!")
|
| 258 |
else:
|
| 259 |
st.success(f"Timelapse {format} créé avec succès!")
|
| 260 |
+
st.markdown(get_binary_file_downloader_html(path, f'Timelapse {format if format != "ZIP" else "Images individuelles (ZIP)"}'), unsafe_allow_html=True)
|
| 261 |
else:
|
| 262 |
st.error(f"Le fichier {format} n'a pas été créé avec succès.")
|
| 263 |
else:
|