From e329908d8825d8323244575f3c1a3d3e6d394c89 Mon Sep 17 00:00:00 2001 From: shouchih_chen Date: Mon, 23 Jun 2025 21:39:38 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=AE=8C=E6=88=90=E6=AA=94=E6=A1=88?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=80=8F=E8=A6=BD=E5=99=A8=E4=B8=8B=E8=BC=89?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 51 +++++++++++++++++++++++++++++++++++--------- generate_images.py | 18 +++++++++------- templates/index.html | 32 +++++++++++++++++---------- 3 files changed, 72 insertions(+), 29 deletions(-) diff --git a/app.py b/app.py index aebc83c..31605a2 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,10 @@ - import os import uuid from flask import Flask, render_template, request, send_file import subprocess +from datetime import datetime +import zipfile +import io app = Flask(__name__) @@ -24,6 +26,9 @@ def allowed_file(filename): @app.route("/") def index(): + search_str = 'e771' + + return render_template("index.html") @app.route("/hello") def hello(): @@ -38,15 +43,42 @@ def upload_csv(): if file.filename == '': return "No selected file", 400 if file and allowed_file(file.filename): - - unique_filename = str(uuid.uuid4()) + '.csv' - csv_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename) + uuid_str = str(uuid.uuid4())[:4] + unique_filename = datetime.now().strftime("%Y%m%d_%H%M")+ '_' + uuid_str + csv_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename+'.csv') file.save(csv_path) print(f"CSV file saved to {csv_path}") - + try: - # 呼叫外部 Python 腳本來生成圖片 - subprocess.run([os.path.join(os.getcwd(), '.venv', 'Scripts', 'python'), 'generate_images.py', csv_path], check=True) + subprocess.run([os.path.join(os.getcwd(), '.venv', 'Scripts', 'python'), 'generate_images.py', csv_path, unique_filename ], check=True) + folder = app.config['GENERATED_IMAGES_FOLDER'] + matched_files = [f for f in os.listdir(folder) if uuid_str in f and f.endswith('.png')] + print(f"Matched files: {matched_files}") + + # 壓縮所有圖片到一個 zip + zip_buffer = io.BytesIO() + with zipfile.ZipFile(zip_buffer, 'w') as zip_file: + for filename in matched_files: + file_path = os.path.join(folder, filename) + zip_file.write(file_path, arcname=filename) + zip_buffer.seek(0) + + # 刪除生成的圖片文件 + for filename in matched_files: + file_path = os.path.join(folder, filename) + os.remove(file_path) + print(f"Deleted file: {file_path}") + # 刪除 CSV 文件 + os.remove(csv_path) + + + return send_file( + zip_buffer, + mimetype='application/zip', + as_attachment=True, + download_name=f'{unique_filename}_images.zip' + ) + except subprocess.CalledProcessError as e: print(f"Error generating images: {e}") return "Error generating images", 500 @@ -56,9 +88,8 @@ def upload_csv(): except Exception as e: print(f"An unexpected error occurred: {e}") return "An unexpected error occurred", 500 - - - return "File uploaded successfully", 200 + + return "File uploaded successfully", 200 if __name__ == '__main__': diff --git a/generate_images.py b/generate_images.py index f519c5e..d2f8af6 100644 --- a/generate_images.py +++ b/generate_images.py @@ -101,7 +101,7 @@ class ImageGenerator(QObject): self.stop() @Slot() - def generate_images(self, name_csv): + def generate_images(self, name_csv, out_filename): # Placeholder for image generation logic print("Generating images...1") @@ -161,7 +161,7 @@ class ImageGenerator(QObject): if i > 0 and i % 6 == 5: # Save the current scene to an image file every 5 items - self.scene_to_image(self.scene, f"generated_image_{i//6}.png") + self.scene_to_image(self.scene, f"{out_filename}_{i//6}.png") self.scene.clear() print("Image generated and added to scene.") @@ -177,17 +177,19 @@ if __name__ == "__main__": generator = ImageGenerator() generator.finished.connect(app.quit) - if len(sys.argv) > 1: + csv_file = "resource/sample.csv" + out_filename = "generated_image" + + if len(sys.argv) > 2: csv_file = sys.argv[1] - print(f"Loading CSV file: {csv_file}") - else: - csv_file = "resource/sample.csv" - print(f"No CSV file provided, using default: {csv_file}") + out_filename = sys.argv[2] + print(f"Loading CSV file: {csv_file}, Output filename: {out_filename}") + sample_data = generator.read_csv(csv_file) print(f"Sample data loaded: {sample_data}") - generator.generate_images(sample_data) + generator.generate_images(sample_data, out_filename) #generator.start() # Run the application event loop diff --git a/templates/index.html b/templates/index.html index cde55a5..9901aa7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,13 +3,13 @@ - CSV 轉圖片工具 + 圓形鑰匙圈產生器 -

上傳 CSV 產生圖片

+

圓形鑰匙圈產生器

- +