YanmHa commited on
Commit
00905c3
·
verified ·
1 Parent(s): 7382af5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -3,46 +3,47 @@ import os
3
  import shutil
4
  from zipfile import ZipFile
5
 
6
- # 根目录和目标文件名
7
  BASE_DIR = "/data/images/images/NeuroPictor"
8
  TARGET_FILENAMES = {"10046_0.jpg", "59194_0.jpg"}
9
  TEMP_DIR = "/tmp/npictor_download"
10
  ZIP_OUTPUT = "/tmp/npictor_download.zip"
11
 
12
  def collect_target_images():
13
- # 清理临时目录
14
  if os.path.exists(TEMP_DIR):
15
  shutil.rmtree(TEMP_DIR)
16
  os.makedirs(TEMP_DIR, exist_ok=True)
17
 
18
  found = []
19
 
20
- # 遍历子目录查找目标文件
21
- for root, dirs, files in os.walk(BASE_DIR):
22
  for file in files:
23
  if file in TARGET_FILENAMES:
 
 
24
  src_path = os.path.join(root, file)
25
- dst_path = os.path.join(TEMP_DIR, file)
26
  shutil.copy(src_path, dst_path)
27
- found.append(file)
28
-
29
- # 压缩成 zip 文件
30
- with ZipFile(ZIP_OUTPUT, 'w') as zipf:
31
- for file in os.listdir(TEMP_DIR):
32
- zipf.write(os.path.join(TEMP_DIR, file), arcname=file)
33
 
 
34
  if found:
 
 
 
35
  return ZIP_OUTPUT
36
  else:
37
- return "未找到目标图像文件。请确认文件是否存在于子目录中。"
38
 
39
  # Gradio 接口
40
  iface = gr.Interface(
41
  fn=collect_target_images,
42
  inputs=[],
43
- outputs=gr.File(label="下载压缩包"),
44
- title="NeuroPictor 图像下载器",
45
- description="下载四个子文件夹下的 10046_0.jpg 59194_0.jpg 文件"
46
  )
47
 
48
  if __name__ == "__main__":
 
3
  import shutil
4
  from zipfile import ZipFile
5
 
6
+ # 路径和设置
7
  BASE_DIR = "/data/images/images/NeuroPictor"
8
  TARGET_FILENAMES = {"10046_0.jpg", "59194_0.jpg"}
9
  TEMP_DIR = "/tmp/npictor_download"
10
  ZIP_OUTPUT = "/tmp/npictor_download.zip"
11
 
12
  def collect_target_images():
13
+ # 清理旧的临时目录和压缩包
14
  if os.path.exists(TEMP_DIR):
15
  shutil.rmtree(TEMP_DIR)
16
  os.makedirs(TEMP_DIR, exist_ok=True)
17
 
18
  found = []
19
 
20
+ # 遍历所有子目录
21
+ for root, _, files in os.walk(BASE_DIR):
22
  for file in files:
23
  if file in TARGET_FILENAMES:
24
+ subfolder_name = os.path.basename(root)
25
+ new_name = f"{subfolder_name}_{file}"
26
  src_path = os.path.join(root, file)
27
+ dst_path = os.path.join(TEMP_DIR, new_name)
28
  shutil.copy(src_path, dst_path)
29
+ found.append(new_name)
 
 
 
 
 
30
 
31
+ # 打包为 zip
32
  if found:
33
+ with ZipFile(ZIP_OUTPUT, 'w') as zipf:
34
+ for file in os.listdir(TEMP_DIR):
35
+ zipf.write(os.path.join(TEMP_DIR, file), arcname=file)
36
  return ZIP_OUTPUT
37
  else:
38
+ return "❌ 未找到目标图像文件。请检查文件名或目录结构是否正确。"
39
 
40
  # Gradio 接口
41
  iface = gr.Interface(
42
  fn=collect_target_images,
43
  inputs=[],
44
+ outputs=gr.File(label="点击下载打包图像"),
45
+ title="NeuroPictor 图像文件打包器",
46
+ description="递归查找10046_0.jpg和59194_0.jpg,并重命名为<子文件夹名>_文件名后打包下载"
47
  )
48
 
49
  if __name__ == "__main__":