import tkinter as tk from tkinter import filedialog, messagebox import requests import os from PIL import Image, ImageTk from threading import Thread import time import shutil class WebNovelImageGenerator: def __init__(self, root): self.root = root self.setup_ui() self.folder_path = "" self.latest_image_path = "" def setup_ui(self): self.root.title("网文图片生成器") self.root.configure(bg='black') self.root.state('zoomed') # Set the window to fullscreen self.main_frame = tk.Frame(self.root, bg='black') self.main_frame.pack(fill=tk.BOTH, expand=True) # Left panel for inputs and buttons self.left_panel = tk.Frame(self.main_frame, bg='black') self.left_panel.pack(side=tk.LEFT, fill=tk.Y, padx=20, pady=20) # Right panel for image display self.right_panel = tk.Frame(self.main_frame, bg='black') self.right_panel.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True) label_font = ('Arial', 17) # Enlarged font size tk.Label(self.left_panel, text="网文图片生成器:请在输入框内输入一段网络文学", fg='white', bg='black', font=label_font).pack(anchor='w') self.entry = tk.Entry(self.left_panel, width=50, font=label_font) self.entry.pack(anchor='w', pady=5) self.generate_button = tk.Button(self.left_panel, text="生成图片", command=self.generate_image, height=2, width=20, font=label_font) self.generate_button.pack(anchor='w', pady=5) self.clear_input_button = tk.Button(self.left_panel, text="清除输入", command=self.clear_input, height=2, width=20, font=label_font) self.clear_input_button.pack(anchor='w', pady=5) self.folder_button = tk.Button(self.left_panel, text="选择图片保存文件夹", command=self.select_folder, height=2, width=20, font=label_font) self.folder_button.pack(anchor='w', pady=5) self.copy_button = tk.Button(self.left_panel, text="复制最新图片", command=self.copy_latest_image, height=2, width=20, font=label_font) self.copy_button.pack(anchor='w', pady=5) self.gpt_response_label = tk.Label(self.left_panel, text="", fg='white', bg='black', wraplength=self.entry.winfo_reqwidth(), font=label_font) # Adjusted width self.gpt_response_label.pack(anchor='w', pady=5) self.image_label = tk.Label(self.right_panel, bg='black') self.image_label.pack(pady=10) def select_folder(self): self.folder_path = filedialog.askdirectory(title="选择图片保存的文件夹") if not self.folder_path: messagebox.showwarning("警告", "未选择文件夹!") else: self.display_latest_image() def generate_image(self): if not self.folder_path: messagebox.showerror("错误", "请先选择一个文件夹!") return user_input = self.entry.get() if not user_input: messagebox.showwarning("警告", "输入框为空,请输入一段网文!") return Thread(target=self.fetch_and_display_image, args=(user_input,)).start() def fetch_and_display_image(self, user_input): gpt_response = self.get_prompt_from_openai(user_input) if gpt_response: self.gpt_response_label.config(text=gpt_response) # Display GPT response in the GUI self.download_and_display_image(gpt_response) def get_prompt_from_openai(self, user_input): api_key = "sk-JBQJmyrMcAAJJtWb00816e05Ff80438389BaE80834Ea5480" headers = {"Authorization": f"Bearer {api_key}"} instruction = ( "我将给你一段网络文学,如{0}, 先判断文学是现代文还是古代文。如果是现代文,用现代都市漫画风格。返还给我一段自然语言提示词,提示词要长80英文词。" "如果是古代文,用国风古风武侠漫画风格。画面需要精美,叙事小说风格。请你把这个文学转译成适合stable diffusion作画的英文描述。不要直译,需要一个画面。" "画面要有故事感,提示词以描述人物为主,武侠感强一点,人物白瘦幼。" "重要:提示词最后加上后缀' --niji 6',提示词不要有引号出现。只要回答英文提示词,不要回答其他东西。" ) data = { "model": "gpt-4-1106-preview", "messages": [ {"role": "system", "content": instruction.format(user_input)}, {"role": "user", "content": user_input} ] } response = requests.post("https://oneapi.xty.app/v1/chat/completions", json=data, headers=headers) if response.status_code == 200: completion = response.json() gpt_response = completion['choices'][0]['message']['content'] return gpt_response else: messagebox.showerror("错误", "获取提示词失败!") return None def download_and_display_image(self, prompt): token = "8ed375d196b849098c43dae29b9ab0dc" headers = {"accept": "application/json", "content-type": "application/json"} payload = {"action": "generate", "prompt": prompt, "timeout": 480} response = requests.post(f"https://api.zhishuyun.com/midjourney/imagine?token={token}", json=payload, headers=headers) if response.status_code == 200: data = response.json() image_url = data.get("image_url") if image_url: self.download_image(image_url) else: messagebox.showerror("错误", "未从API获取到图片URL。") else: messagebox.showerror("错误", "图片生成失败!") def download_image(self, image_url): response = requests.get(image_url) if response.status_code == 200: image_filename = f"image_{int(time.time())}.jpg" image_path = os.path.join(self.folder_path, image_filename) with open(image_path, 'wb') as f: f.write(response.content) self.display_image(image_path) else: messagebox.showerror("错误", "下载图片失败!") def display_image(self, image_path): self.latest_image_path = image_path img = Image.open(image_path) img.thumbnail((1000, 1000), Image.Resampling.LANCZOS) photo = ImageTk.PhotoImage(img) self.image_label.config(image=photo) self.image_label.image = photo # keep a reference def display_latest_image(self): if self.folder_path: try: list_of_files = [os.path.join(self.folder_path, file) for file in os.listdir(self.folder_path) if file.endswith('.jpg')] latest_file = max(list_of_files, key=os.path.getctime) self.display_image(latest_file) except ValueError: messagebox.showinfo("信息", "文件夹内没有图片!") def copy_latest_image(self): if not self.latest_image_path: messagebox.showinfo("提示", "没有图片可以复制!") return destination = filedialog.askdirectory(title="选择复制到的文件夹") if not destination: return shutil.copy(self.latest_image_path, destination) messagebox.showinfo("成功", "图片复制成功!") def clear_input(self): # Clears the text in the entry widget self.entry.delete(0, tk.END) def main(): root = tk.Tk() app = WebNovelImageGenerator(root) root.mainloop() if __name__ == "__main__": main()
标签:exe,niji,self,response,tk,网文,path,font,image From: https://www.cnblogs.com/zly324/p/18078869