灵感画廊实战教程WebP格式输出EXIF元数据嵌入保存创作信息1. 教程概述今天我要分享一个非常实用的技巧如何为灵感画廊这个优雅的AI艺术创作工具添加WebP格式输出和EXIF元数据嵌入功能。如果你经常使用AI生成艺术作品这个功能将让你的创作过程更加完整和专业。学习目标通过本教程你将学会为灵感画廊添加WebP格式输出支持在生成的图片中嵌入EXIF元数据包括提示词、参数等信息一键保存完整的创作信息前置知识只需要基本的Python知识了解如何使用pip安装包即可。整个过程非常简单我会一步步带你完成。2. 环境准备与依赖安装首先我们需要为灵感画廊添加一些新的依赖包。打开终端执行以下命令pip install Pillow piexif这两个包的作用是PillowPython图像处理库用于处理WebP格式piexif专门用于读写EXIF元数据的库安装完成后我们可以在代码中导入这些包from PIL import Image import piexif import io import json from datetime import datetime3. WebP格式输出实现现在我们来修改灵感画廊的保存功能添加WebP格式支持。找到保存图片的函数通常在app.py中添加以下代码def save_image_webp(image, prompt, negative_prompt, config, quality90): 将生成的图片保存为WebP格式并嵌入EXIF元数据 # 转换图片为PIL格式 if hasattr(image, images): pil_image image.images[0] else: pil_image image # 创建EXIF元数据 exif_dict create_exif_data(prompt, negative_prompt, config) # 将图片保存到内存中的字节流 img_byte_arr io.BytesIO() pil_image.save( img_byte_arr, formatWEBP, qualityquality, exifpiexif.dump(exif_dict) if exif_dict else None ) # 重置字节流指针 img_byte_arr.seek(0) return img_byte_arr def create_exif_data(prompt, negative_prompt, config): 创建包含创作信息的EXIF数据 # 获取当前时间 current_time datetime.now().strftime(%Y:%m:%d %H:%M:%S) # 创建EXIF字典 exif_dict { 0th: { piexif.ImageIFD.DateTime: current_time, piexif.ImageIFD.Software: 灵感画廊 - Atelier of Light and Shadow, }, Exif: { piexif.ExifIFD.DateTimeOriginal: current_time, piexif.ExifIFD.UserComment: piexif.helper.UserComment.dump( fPrompt: {prompt}\nNegative: {negative_prompt}\nConfig: {json.dumps(config)}, encodingunicode ) }, GPS: {}, 1st: {}, thumbnail: None } return exif_dict4. 集成到灵感画廊界面接下来我们需要在Streamlit界面中添加格式选择功能。找到界面设置部分添加以下代码# 在侧边栏添加格式选择 st.sidebar.markdown(---) st.sidebar.markdown(### ️ 输出设置) # 格式选择 output_format st.sidebar.radio( 输出格式, [PNG, WebP], help选择图片保存格式WebP格式更小且支持元数据嵌入 ) # WebP质量设置 if output_format WebP: webp_quality st.sidebar.slider( WebP质量, min_value50, max_value100, value90, help质量越高文件越大推荐80-95之间 ) # 修改保存按钮的逻辑 if st.button( 挥笔成画): # ... 原有的生成代码 ... # 生成图片后根据选择的格式保存 if output_format WebP: img_bytes save_image_webp( image, prompt, negative_prompt, { preset: preset, steps: steps, guidance_scale: guidance_scale, width: width, height: height }, qualitywebp_quality ) # 提供下载 st.download_button( label 下载WebP作品, dataimg_bytes.getvalue(), file_namefinspiration_gallery_{datetime.now().strftime(%Y%m%d_%H%M%S)}.webp, mimeimage/webp ) else: # 原有的PNG保存逻辑 pass5. 完整代码整合将以上代码整合到你的灵感画廊项目中。这里是一个完整的修改示例# 在文件开头添加导入 import io import json from datetime import datetime from PIL import Image import piexif import piexif.helper # 在适当位置添加EXIF创建函数 def create_exif_data(prompt, negative_prompt, config): 创建包含创作信息的EXIF数据 current_time datetime.now().strftime(%Y:%m:%d %H:%M:%S) exif_dict { 0th: { piexif.ImageIFD.DateTime: current_time, piexif.ImageIFD.Software: 灵感画廊 - Atelier of Light and Shadow, piexif.ImageIFD.ImageDescription: fAI生成艺术作品 - {prompt[:50]}... }, Exif: { piexif.ExifIFD.DateTimeOriginal: current_time, piexif.ExifIFD.DateTimeDigitized: current_time, piexif.ExifIFD.UserComment: piexif.helper.UserComment.dump( json.dumps({ prompt: prompt, negative_prompt: negative_prompt, config: config, generator: Stable Diffusion XL 1.0, tool: 灵感画廊 - Atelier of Light and Shadow }, ensure_asciiFalse), encodingunicode ) }, GPS: {}, 1st: {}, thumbnail: None } return exif_dict # 添加WebP保存函数 def save_image_webp(image, prompt, negative_prompt, config, quality90): 保存为WebP格式并嵌入EXIF if hasattr(image, images): pil_image image.images[0] else: pil_image image exif_dict create_exif_data(prompt, negative_prompt, config) img_byte_arr io.BytesIO() pil_image.save( img_byte_arr, formatWEBP, qualityquality, exifpiexif.dump(exif_dict) if exif_dict else None ) img_byte_arr.seek(0) return img_byte_arr # 在Streamlit界面中添加格式选择 def main(): # ... 原有的界面代码 ... # 添加输出格式选择 st.sidebar.markdown(---) st.sidebar.markdown(### ️ 输出设置) output_format st.sidebar.radio( 输出格式, [PNG, WebP], help选择图片保存格式 ) webp_quality 90 if output_format WebP: webp_quality st.sidebar.slider( WebP质量, min_value50, max_value100, value90 ) # 修改生成按钮的处理逻辑 if st.button( 挥笔成画): # ... 原有的生成逻辑 ... with st.spinner(️ 光影正在凝结...): image pipe( promptfull_prompt, negative_promptnegative_prompt, num_inference_stepssteps, guidance_scaleguidance_scale, widthwidth, heightheight ).images[0] # 显示生成的图片 st.image(image, caption您的光影杰作, use_column_widthTrue) # 根据选择的格式提供下载 if output_format WebP: img_bytes save_image_webp( image, prompt, negative_prompt, { preset: preset, steps: steps, guidance_scale: guidance_scale, width: width, height: height }, qualitywebp_quality ) st.download_button( label 下载WebP作品含元数据, dataimg_bytes.getvalue(), file_namefinspiration_gallery_{datetime.now().strftime(%Y%m%d_%H%M%S)}.webp, mimeimage/webp, help下载包含完整创作信息的WebP格式图片 ) else: # 原有的PNG下载逻辑 buf io.BytesIO() image.save(buf, formatPNG) byte_im buf.getvalue() st.download_button( label 下载PNG作品, databyte_im, file_namefinspiration_gallery_{datetime.now().strftime(%Y%m%d_%H%M%S)}.png, mimeimage/png )6. 验证元数据嵌入完成代码修改后让我们验证一下元数据是否正确嵌入。你可以使用以下方法检查方法一使用Python检查EXIF数据import piexif from PIL import Image # 打开保存的WebP图片 image_path 你的图片路径.webp img Image.open(image_path) # 提取EXIF数据 exif_data piexif.load(img.info.get(exif, b)) # 打印用户评论包含完整的创作信息 if Exif in exif_data and piexif.ExifIFD.UserComment in exif_data[Exif]: user_comment piexif.helper.UserComment.load(exif_data[Exif][piexif.ExifIFD.UserComment]) print(创作信息:, user_comment)方法二使用系统工具在Windows上右键图片 → 属性 → 详细信息在macOS上右键图片 → 显示简介 → 更多信息在线工具如 exifinfo.org7. 实用技巧与建议在使用这个功能时这里有一些实用建议WebP质量设置建议90-95高质量文件大小适中推荐日常使用80-89良好质量文件更小适合网络分享75-79中等质量有明显压缩痕迹谨慎使用元数据内容优化 你可以在create_exif_data函数中添加更多信息# 添加更多元数据信息 exif_dict[0th][piexif.ImageIFD.Artist] 灵感画廊创作者 exif_dict[0th][piexif.ImageIFD.Copyright] f© {datetime.now().year} 灵感画廊 # 添加更多技术参数 config_data { prompt: prompt, negative_prompt: negative_prompt, preset: config.get(preset, ), steps: config.get(steps, 0), guidance_scale: config.get(guidance_scale, 0), resolution: f{config.get(width, 0)}x{config.get(height, 0)}, model: Stable Diffusion XL 1.0, generator: 灵感画廊 - Atelier of Light and Shadow, generation_date: current_time }处理大尺寸图片 如果生成非常高分辨率的图片可以考虑添加进度指示# 在保存大图片时显示进度 with st.spinner(️ 正在保存高质量WebP图片...): img_bytes save_image_webp(image, prompt, negative_prompt, config, qualitywebp_quality)8. 总结通过本教程你已经成功为灵感画廊添加了WebP格式输出和EXIF元数据嵌入功能。这个功能的好处包括完整的创作记录所有提示词和参数都保存在图片元数据中更好的文件管理WebP格式通常比PNG小25-35%节省存储空间专业的工作流程元数据让AI艺术创作更加规范化和可追溯现在你的灵感画廊不仅是一个创作工具更是一个完整的艺术创作生态系统。每次生成的作品都携带完整的创作信息方便日后回顾、学习和分享。下一步建议尝试添加更多元数据字段如色彩配置、创作主题分类等考虑添加批量处理功能同时处理多组参数探索添加水印功能保护你的创作版权希望这个教程能提升你的AI艺术创作体验如果有任何问题或建议欢迎交流讨论。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。