GLM-Image与Python爬虫结合实战:自动化采集图片素材并生成艺术创作
你有没有遇到过这样的场景?想用AI生成一张特定主题的图片,但手头没有合适的素材参考;或者需要批量处理大量图片素材,但一张张上传、描述太费时间。今天我要分享的,就是把Python爬虫和GLM-Image结合起来,打造一个自动化图片采集与AI创作的完整工作流。
这个方案的核心思路很简单:用爬虫自动从网上抓取你需要的图片素材,然后交给GLM-Image进行二次创作。比如你想生成“未来城市”主题的艺术作品,爬虫可以帮你收集各种现代建筑、科技元素的图片,GLM-Image再基于这些素材生成全新的创意图像。
整个过程就像有个不知疲倦的助手,帮你找素材、做创作,效率提升不是一点半点。下面我就带你一步步实现这个自动化流程。
1. 为什么需要爬虫+AI图像生成的组合?
在开始技术细节之前,我们先看看这个组合能解决哪些实际问题。
传统AI图像生成的痛点:
- 需要自己准备大量参考图片
- 描述词(prompt)写不准,生成效果不理想
- 批量处理时手动操作繁琐
- 特定领域素材难找
爬虫+GLM-Image的优势:
- 自动获取最新、最相关的图片素材
- 基于真实图片生成更准确的描述词
- 批量自动化处理,解放双手
- 结合具体业务场景定制化采集
举个例子,如果你是电商运营,需要为新产品生成宣传图。传统做法是找设计师做图,或者用AI生成但效果不稳定。用我们的方案,可以先爬取竞品的产品图,分析它们的视觉特点,再让GLM-Image生成风格类似但内容全新的产品图。
2. 环境准备与工具选择
2.1 需要安装的Python库
我们先准备好需要的工具。打开终端,安装以下Python库:
# 基础爬虫和网络请求 pip install requests beautifulsoup4 selenium # 图像处理 pip install pillow opencv-python # AI模型调用 pip install zhipuai # 智谱AI官方SDK # 其他工具 pip install python-dotenv # 管理API密钥 pip install tqdm # 进度条显示如果你需要更复杂的爬虫功能,比如处理JavaScript渲染的页面,建议用Selenium。如果只是简单的静态页面,用requests+BeautifulSoup就够了。
2.2 获取GLM-Image API访问权限
GLM-Image是智谱AI推出的图像生成模型,我们需要先获取API访问权限:
- 访问智谱AI开放平台(bigmodel.cn)
- 注册账号并完成实名认证
- 在控制台创建API Key
- 查看GLM-Image的调用文档和价格
建议先申请免费额度试用,熟悉后再根据实际使用量充值。
2.3 项目结构规划
创建一个清晰的项目结构,方便后续维护:
glm-image-crawler/ ├── config/ │ ├── __init__.py │ └── settings.py # 配置文件 ├── crawler/ │ ├── __init__.py │ ├── base_crawler.py # 爬虫基类 │ ├── image_crawler.py # 图片爬虫 │ └── utils.py # 爬虫工具函数 ├── processor/ │ ├── __init__.py │ ├── image_processor.py # 图片处理 │ └── prompt_generator.py # 提示词生成 ├── glm/ │ ├── __init__.py │ └── image_client.py # GLM-Image客户端 ├── outputs/ │ ├── raw_images/ # 原始爬取图片 │ ├── processed/ # 处理后的图片 │ └── generated/ # AI生成的图片 ├── main.py # 主程序 └── requirements.txt # 依赖列表3. 构建智能图片爬虫
3.1 基础爬虫实现
我们先实现一个通用的图片爬虫,支持常见的图片网站。这里以爬取Unsplash(免费高质量图片网站)为例:
# crawler/image_crawler.py import os import time import requests from bs4 import BeautifulSoup from urllib.parse import urljoin, urlparse from PIL import Image from io import BytesIO import logging class ImageCrawler: def __init__(self, output_dir="outputs/raw_images"): self.output_dir = output_dir self.session = requests.Session() self.session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' }) # 创建输出目录 os.makedirs(output_dir, exist_ok=True) # 设置日志 logging.basicConfig(level=logging.INFO) self.logger = logging.getLogger(__name__) def crawl_unsplash(self, keyword, max_images=20): """爬取Unsplash上的图片""" self.logger.info(f"开始爬取关键词: {keyword}") # Unsplash搜索URL search_url = f"https://unsplash.com/s/photos/{keyword}" try: response = self.session.get(search_url, timeout=10) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # 查找图片元素 - Unsplash的图片在特定的div中 image_elements = [] # 方法1: 查找带有特定data-test属性的img标签 img_tags = soup.find_all('img', {'data-test': 'photo-grid-masonry-img'}) # 方法2: 查找所有img标签,然后过滤 if not img_tags: all_imgs = soup.find_all('img') img_tags = [img for img in all_imgs if 'srcset' in str(img) and 'sizes' in str(img)] self.logger.info(f"找到 {len(img_tags)} 个图片元素") downloaded = 0 for i, img in enumerate(img_tags[:max_images]): try: # 获取图片URL if 'src' in img.attrs: img_url = img['src'] elif 'data-src' in img.attrs: img_url = img['data-src'] else: continue # 确保是完整的URL if not img_url.startswith('http'): img_url = urljoin('https://unsplash.com', img_url) # 下载图片 if self.download_image(img_url, keyword, i): downloaded += 1 time.sleep(0.5) # 礼貌性延迟 except Exception as e: self.logger.error(f"下载图片失败: {e}") continue self.logger.info(f"成功下载 {downloaded} 张图片") return downloaded except Exception as e: self.logger.error(f"爬取过程出错: {e}") return 0 def download_image(self, url, keyword, index): """下载单张图片""" try: response = self.session.get(url, timeout=15, stream=True) response.raise_for_status() # 从URL提取文件名 parsed_url = urlparse(url) filename = os.path.basename(parsed_url.path) # 如果没有扩展名,添加一个 if not '.' in filename: filename = f"{keyword}_{index}.jpg" else: # 保留原始文件名但添加前缀 name, ext = os.path.splitext(filename) filename = f"{keyword}_{index}{ext}" filepath = os.path.join(self.output_dir, filename) # 保存图片 with open(filepath, 'wb') as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) self.logger.info(f"已保存: {filename}") return True except Exception as e: self.logger.error(f"下载失败 {url}: {e}") return False def crawl_custom_site(self, url, img_selectors): """自定义网站爬取""" # 这里可以根据具体网站结构定制爬取逻辑 pass3.2 处理反爬机制
很多网站都有反爬措施,我们需要做一些处理:
# crawler/utils.py import random import time from fake_useragent import UserAgent class AntiAntiCrawler: """反反爬虫工具类""" def __init__(self): self.ua = UserAgent() self.request_count = 0 self.last_request_time = time.time() def get_random_headers(self): """生成随机请求头""" headers = { 'User-Agent': self.ua.random, 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', } return headers def random_delay(self, min_seconds=1, max_seconds=3): """随机延迟,模拟人工操作""" delay = random.uniform(min_seconds, max_seconds) time.sleep(delay) def should_take_break(self): """判断是否需要休息(防止请求过于频繁)""" self.request_count += 1 current_time = time.time() # 每10次请求休息一下 if self.request_count % 10 == 0: time.sleep(random.uniform(5, 10)) return True # 距离上次请求太近也休息 if current_time - self.last_request_time < 0.5: time.sleep(random.uniform(0.5, 1.5)) self.last_request_time = current_time return False3.3 图片质量筛选
不是所有爬到的图片都适合作为AI生成的参考,我们需要筛选:
# processor/image_processor.py from PIL import Image import os import numpy as np class ImageQualityFilter: """图片质量筛选器""" @staticmethod def filter_by_size(image_path, min_width=400, min_height=400): """按尺寸筛选""" try: with Image.open(image_path) as img: width, height = img.size return width >= min_width and height >= min_height except: return False @staticmethod def filter_by_aspect_ratio(image_path, min_ratio=0.5, max_ratio=2.0): """按宽高比筛选""" try: with Image.open(image_path) as img: width, height = img.size ratio = width / height return min_ratio <= ratio <= max_ratio except: return False @staticmethod def filter_by_entropy(image_path, threshold=5.0): """按图像熵筛选(剔除纯色或简单图片)""" try: with Image.open(image_path) as img: # 转换为灰度图 gray_img = img.convert('L') hist = gray_img.histogram() # 计算熵 hist = [h for h in hist if h > 0] total = sum(hist) entropy = -sum((h/total) * np.log2(h/total) for h in hist) return entropy >= threshold except: return False @staticmethod def batch_filter(image_dir, output_dir): """批量筛选图片""" os.makedirs(output_dir, exist_ok=True) qualified_images = [] for filename in os.listdir(image_dir): if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.webp')): filepath = os.path.join(image_dir, filename) # 应用所有筛选条件 if (ImageQualityFilter.filter_by_size(filepath) and ImageQualityFilter.filter_by_aspect_ratio(filepath) and ImageQualityFilter.filter_by_entropy(filepath)): # 复制到输出目录 import shutil shutil.copy2(filepath, os.path.join(output_dir, filename)) qualified_images.append(filename) return qualified_images4. 图片分析与提示词生成
4.1 使用GLM-4V分析图片内容
GLM-4V是智谱的视觉理解模型,可以帮我们分析图片内容,生成准确的描述:
# processor/prompt_generator.py from zhipuai import ZhipuAI import base64 import os class ImageAnalyzer: """图片分析器,使用GLM-4V生成描述""" def __init__(self, api_key): self.client = ZhipuAI(api_key=api_key) def analyze_image(self, image_path): """分析单张图片,生成详细描述""" try: # 读取图片并编码为base64 with open(image_path, "rb") as image_file: base64_image = base64.b64encode(image_file.read()).decode('utf-8') # 构建消息 messages = [ { "role": "user", "content": [ { "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } }, { "type": "text", "text": """请详细描述这张图片,包括: 1. 主要物体和场景 2. 颜色和光线特点 3. 风格和氛围 4. 构图特点 请用自然流畅的中文描述,适合作为AI图像生成的提示词参考。""" } ] } ] # 调用GLM-4V response = self.client.chat.completions.create( model="glm-4v", # 使用GLM-4V视觉模型 messages=messages, temperature=0.7, max_tokens=500 ) description = response.choices[0].message.content return description.strip() except Exception as e: print(f"图片分析失败: {e}") return None def generate_prompt_from_images(self, image_paths, style="艺术创作"): """基于多张图片生成综合提示词""" descriptions = [] for img_path in image_paths[:3]: # 最多分析3张 desc = self.analyze_image(img_path) if desc: descriptions.append(desc) if not descriptions: return None # 基于描述生成综合提示词 combined_desc = "\n".join(descriptions[:2]) # 取前两个描述 prompt_messages = [ { "role": "user", "content": f"""基于以下图片描述,生成一个适合AI图像生成的提示词: 图片描述: {combined_desc} 要求: 1. 融合图片的核心元素和风格 2. 突出{style}风格 3. 包含具体的视觉细节 4. 长度在50-100字左右 5. 用英文生成(AI图像生成对英文提示词响应更好) 请直接输出提示词,不要额外解释。""" } ] try: response = self.client.chat.completions.create( model="glm-4", messages=prompt_messages, temperature=0.8, max_tokens=200 ) prompt = response.choices[0].message.content.strip() return prompt except Exception as e: print(f"提示词生成失败: {e}") return None4.2 提示词优化技巧
好的提示词能让GLM-Image生成更好的效果。这里分享一些实用技巧:
# processor/prompt_optimizer.py class PromptOptimizer: """提示词优化器""" @staticmethod def add_style_keywords(prompt, style="digital art"): """添加风格关键词""" style_keywords = { "digital art": "digital art, trending on artstation, detailed, sharp focus", "oil painting": "oil painting, brush strokes, textured, masterpiece", "anime": "anime style, vibrant colors, detailed eyes, studio ghibli", "realistic": "photorealistic, 8k, detailed, professional photography", "watercolor": "watercolor painting, soft edges, transparent washes", "cyberpunk": "cyberpunk, neon lights, futuristic, blade runner style" } if style in style_keywords: return f"{prompt}, {style_keywords[style]}" return prompt @staticmethod def add_quality_keywords(prompt): """添加质量关键词""" quality_words = "high quality, detailed, sharp focus, masterpiece, best quality" return f"{prompt}, {quality_words}" @staticmethod def add_lighting_keywords(prompt, lighting="dramatic"): """添加光线关键词""" lighting_keywords = { "dramatic": "dramatic lighting, chiaroscuro", "soft": "soft lighting, diffused light", "golden hour": "golden hour, warm sunlight", "studio": "studio lighting, professional lighting", "neon": "neon lighting, colorful glow" } if lighting in lighting_keywords: return f"{prompt}, {lighting_keywords[lighting]}" return prompt @staticmethod def optimize_for_glm_image(prompt, style="digital art", lighting="dramatic"): """为GLM-Image优化提示词""" prompt = PromptOptimizer.add_style_keywords(prompt, style) prompt = PromptOptimizer.add_quality_keywords(prompt) prompt = PromptOptimizer.add_lighting_keywords(prompt, lighting) # GLM-Image对中文支持很好,但混合使用效果更佳 return prompt5. GLM-Image图像生成实战
5.1 封装GLM-Image客户端
# glm/image_client.py from zhipuai import ZhipuAI import base64 import time from PIL import Image from io import BytesIO class GLMImageClient: """GLM-Image客户端封装""" def __init__(self, api_key): self.client = ZhipuAI(api_key=api_key) self.model = "glm-image" # GLM-Image模型 def generate_image(self, prompt, size="1024x1024", num_images=1): """生成单张图片""" try: response = self.client.images.generations.create( model=self.model, prompt=prompt, size=size, n=num_images, response_format="url" # 返回图片URL ) # GLM-Image返回的是包含图片URL的对象 image_urls = [] for img_data in response.data: image_urls.append(img_data.url) return image_urls except Exception as e: print(f"图片生成失败: {e}") return None def generate_with_reference(self, prompt, reference_image_path, strength=0.7): """基于参考图生成图片""" try: # 读取参考图并编码 with open(reference_image_path, "rb") as img_file: reference_image = base64.b64encode(img_file.read()).decode('utf-8') # GLM-Image支持参考图生成(具体参数需查看最新文档) response = self.client.images.generations.create( model=self.model, prompt=prompt, reference_images=[reference_image], reference_strength=strength, size="1024x1024", n=1 ) return response.data[0].url if response.data else None except Exception as e: print(f"参考图生成失败: {e}") return None def download_image(self, image_url, save_path): """下载生成的图片""" try: import requests response = requests.get(image_url, timeout=30) response.raise_for_status() # 保存图片 with open(save_path, 'wb') as f: f.write(response.content) print(f"图片已保存: {save_path}") return True except Exception as e: print(f"图片下载失败: {e}") return False def batch_generate(self, prompts, output_dir, size="1024x1024"): """批量生成图片""" import os os.makedirs(output_dir, exist_ok=True) results = [] for i, prompt in enumerate(prompts): print(f"生成第 {i+1}/{len(prompts)} 张: {prompt[:50]}...") try: image_urls = self.generate_image(prompt, size=size) if image_urls: for j, url in enumerate(image_urls): filename = f"generated_{i}_{j}_{int(time.time())}.png" save_path = os.path.join(output_dir, filename) if self.download_image(url, save_path): results.append({ "prompt": prompt, "path": save_path, "success": True }) else: results.append({ "prompt": prompt, "error": "下载失败", "success": False }) # 避免请求过于频繁 time.sleep(1) except Exception as e: print(f"批量生成失败: {e}") results.append({ "prompt": prompt, "error": str(e), "success": False }) return results5.2 实际生成示例
让我们看一个完整的生成示例:
# examples/complete_workflow.py import sys import os sys.path.append(os.path.dirname(os.path.dirname(__file__))) from crawler.image_crawler import ImageCrawler from processor.image_processor import ImageQualityFilter from processor.prompt_generator import ImageAnalyzer from glm.image_client import GLMImageClient from processor.prompt_optimizer import PromptOptimizer def complete_workflow(api_key, keyword="mountain landscape", style="digital art"): """完整工作流示例""" print("=" * 50) print(f"开始处理关键词: {keyword}") print("=" * 50) # 1. 爬取图片 print("\n1. 爬取参考图片...") crawler = ImageCrawler(output_dir="outputs/raw_images") downloaded = crawler.crawl_unsplash(keyword, max_images=10) if downloaded == 0: print("爬取失败,使用备用图片") # 这里可以添加备用方案 return # 2. 筛选图片 print("\n2. 筛选高质量图片...") raw_dir = "outputs/raw_images" processed_dir = "outputs/processed" qualified = ImageQualityFilter.batch_filter(raw_dir, processed_dir) print(f"筛选出 {len(qualified)} 张合格图片") if not qualified: print("没有合格图片,退出流程") return # 3. 分析图片生成提示词 print("\n3. 分析图片生成提示词...") analyzer = ImageAnalyzer(api_key) # 取前3张图片分析 sample_images = [os.path.join(processed_dir, f) for f in qualified[:3]] base_prompt = analyzer.generate_prompt_from_images(sample_images, style) if not base_prompt: print("提示词生成失败,使用默认提示词") base_prompt = f"{keyword}, {style}, detailed, high quality" print(f"生成的提示词: {base_prompt[:100]}...") # 4. 优化提示词 print("\n4. 优化提示词...") optimized_prompt = PromptOptimizer.optimize_for_glm_image( base_prompt, style=style, lighting="dramatic" ) print(f"优化后的提示词: {optimized_prompt[:150]}...") # 5. 生成图片 print("\n5. 使用GLM-Image生成图片...") glm_client = GLMImageClient(api_key) # 生成多张变体 prompts = [ optimized_prompt, f"{optimized_prompt}, sunrise lighting", f"{optimized_prompt}, night time, stars", f"{optimized_prompt}, foggy atmosphere" ] results = glm_client.batch_generate( prompts, output_dir="outputs/generated", size="1024x1024" ) # 6. 输出结果 print("\n" + "=" * 50) print("生成完成!") print("=" * 50) success_count = sum(1 for r in results if r.get("success", False)) print(f"成功生成 {success_count}/{len(results)} 张图片") for i, result in enumerate(results): if result.get("success"): print(f" {i+1}. ✓ 成功: {result['path']}") else: print(f" {i+1}. ✗ 失败: {result.get('error', '未知错误')}") return results # 使用示例 if __name__ == "__main__": # 从环境变量读取API Key import os from dotenv import load_dotenv load_dotenv() api_key = os.getenv("ZHIPUAI_API_KEY") if not api_key: print("请设置ZHIPUAI_API_KEY环境变量") else: # 运行完整工作流 results = complete_workflow( api_key=api_key, keyword="forest waterfall", style="fantasy art" )6. 高级应用场景
6.1 电商产品图生成
电商场景下,我们可以用这个方案自动生成产品图:
# examples/ecommerce_example.py class EcommerceImageGenerator: """电商图片生成器""" def __init__(self, api_key): self.glm_client = GLMImageClient(api_key) self.analyzer = ImageAnalyzer(api_key) def generate_product_background(self, product_image_path, style="clean studio"): """为产品生成背景""" # 分析产品图 product_desc = self.analyzer.analyze_image(product_image_path) # 生成背景提示词 prompt = f"""Professional product photography background, {style} style, clean and minimalist, suitable for ecommerce, product is: {product_desc[:100]}..., high quality, studio lighting, sharp focus""" # 生成背景 background_url = self.glm_client.generate_image(prompt, size="1024x1024")[0] return background_url def generate_lifestyle_image(self, product_desc, scene="cozy home"): """生成生活方式场景图""" prompt = f"""Lifestyle product photo, {scene} setting, product: {product_desc}, natural lighting, authentic scene, people using product, ecommerce style, high quality, detailed""" return self.glm_client.generate_image(prompt, size="1024x1024")[0] def batch_product_images(self, product_list, output_dir): """批量生成产品图""" results = [] for product in product_list: print(f"处理产品: {product['name']}") # 生成主图 main_prompt = f"""Professional ecommerce product photo of {product['name']}, {product.get('category', '')} category, clean white background, studio lighting, high detail, product centered, {product.get('description', '')}""" main_image = self.glm_client.generate_image(main_prompt)[0] # 生成场景图 scene_prompt = f"""Lifestyle photo of {product['name']} in use, {product.get('usage_scene', 'home environment')}, happy people, natural lighting, social media style, authentic""" scene_image = self.glm_client.generate_image(scene_prompt)[0] results.append({ "product": product['name'], "main_image": main_image, "scene_image": scene_image }) # 避免速率限制 import time time.sleep(2) return results6.2 社交媒体内容创作
对于社交媒体运营,可以批量生成各种风格的图片:
# examples/social_media_example.py class SocialMediaContentCreator: """社交媒体内容创作器""" def __init__(self, api_key): self.client = GLMImageClient(api_key) def generate_post_image(self, topic, platform="instagram", style="modern"): """生成社交媒体帖子图片""" platform_styles = { "instagram": "instagram aesthetic, square format, trendy", "twitter": "minimalist, clean, attention-grabbing", "linkedin": "professional, corporate, clean design", "pinterest": "inspirational, beautiful, detailed" } style_keywords = platform_styles.get(platform, "modern, clean") prompt = f"""Social media post image about {topic}, {style} style, {style_keywords}, engaging and shareable, high quality graphic design, suitable for {platform}, trending style""" return self.client.generate_image(prompt, size="1080x1080")[0] def generate_quote_image(self, quote, background_theme="inspirational"): """生成名言图片""" prompt = f"""Beautiful quote image with text: "{quote}", {background_theme} background, elegant typography, inspirational, social media quote post, soft lighting, artistic, shareable content""" return self.client.generate_image(prompt, size="1080x1350")[0] # Instagram故事尺寸 def create_content_calendar(self, topics, days=7): """创建一周的内容日历""" content_plan = [] for i, topic in enumerate(topics[:days]): # 每天生成不同类型的图片 if i % 3 == 0: image_type = "educational infographic" elif i % 3 == 1: image_type = "inspirational quote" else: image_type = "behind the scenes" prompt = f"""Social media content image for day {i+1}, topic: {topic}, type: {image_type}, engaging, shareable, high quality, consistent brand style, professional design""" image_url = self.client.generate_image(prompt)[0] content_plan.append({ "day": i+1, "topic": topic, "type": image_type, "image_url": image_url }) return content_plan7. 性能优化与最佳实践
7.1 批量处理优化
# utils/batch_processor.py import concurrent.futures import threading from queue import Queue import time class BatchProcessor: """批量处理器,支持并发处理""" def __init__(self, max_workers=3): self.max_workers = max_workers self.request_lock = threading.Lock() self.request_count = 0 self.last_reset_time = time.time() def process_batch(self, items, process_func, *args, **kwargs): """批量处理项目""" results = [] with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor: # 提交所有任务 future_to_item = { executor.submit(self._safe_process, process_func, item, *args, **kwargs): item for item in items } # 收集结果 for future in concurrent.futures.as_completed(future_to_item): item = future_to_item[future] try: result = future.result(timeout=60) results.append((item, result)) except Exception as e: print(f"处理失败 {item}: {e}") results.append((item, None)) return results def _safe_process(self, process_func, item, *args, **kwargs): """安全处理,包含速率限制""" with self.request_lock: current_time = time.time() # 重置计数器(每分钟) if current_time - self.last_reset_time > 60: self.request_count = 0 self.last_reset_time = current_time # 检查速率限制(假设限制为30次/分钟) if self.request_count >= 30: wait_time = 60 - (current_time - self.last_reset_time) if wait_time > 0: print(f"达到速率限制,等待 {wait_time:.1f}秒") time.sleep(wait_time) self.request_count = 0 self.last_reset_time = time.time() self.request_count += 1 # 执行处理函数 return process_func(item, *args, **kwargs)7.2 错误处理与重试
# utils/retry_handler.py import time from functools import wraps def retry_on_failure(max_retries=3, delay=1, backoff=2): """重试装饰器""" def decorator(func): @wraps(func) def wrapper(*args, **kwargs): retries = 0 last_exception = None while retries < max_retries: try: return func(*args, **kwargs) except Exception as e: last_exception = e retries += 1 if retries < max_retries: wait_time = delay * (backoff ** (retries - 1)) print(f"重试 {retries}/{max_retries}: {str(e)[:100]}...") time.sleep(wait_time) else: print(f"达到最大重试次数: {e}") raise last_exception raise last_exception return wrapper return decorator class ErrorHandler: """错误处理器""" @staticmethod def handle_api_error(error, operation="API调用"): """处理API错误""" error_msg = str(error) if "rate limit" in error_msg.lower(): print(f"{operation}: 达到速率限制,等待后重试") time.sleep(30) return "retry" elif "quota" in error_msg.lower() or "balance" in error_msg.lower(): print(f"{operation}: 额度不足,请检查账户余额") return "quota_exceeded" elif "timeout" in error_msg.lower(): print(f"{operation}: 请求超时") return "timeout" else: print(f"{operation}: 未知错误 - {error_msg[:200]}") return "unknown"8. 总结与建议
把Python爬虫和GLM-Image结合起来,确实能大大提升图片素材处理和创作的效率。从实际使用来看,这个方案有几个明显的优势:一是自动化程度高,从找素材到生成作品基本不用人工干预;二是灵活性好,可以根据不同需求调整爬取策略和生成参数;三是效果可控,通过优化提示词和参考图,能获得比较稳定的输出质量。
不过在实际应用中,我也发现了一些需要注意的地方。首先是成本控制,GLM-Image的API调用是收费的,批量生成时要注意监控使用量。其次是爬虫的稳定性,不同网站的反爬策略不一样,可能需要针对性地调整。还有就是生成结果的一致性,虽然GLM-Image效果不错,但完全复现特定风格还需要一些技巧。
如果你打算在实际项目中使用这个方案,我建议先从简单的场景开始,比如批量生成社交媒体配图。等熟悉了整个流程,再尝试更复杂的应用,比如电商产品图生成或者创意设计。过程中多积累一些高质量的提示词模板,这对提升生成效果很有帮助。
另外,记得定期检查爬虫的合规性,尊重网站的robots.txt,避免给目标网站造成负担。对于生成的图片,也要注意版权问题,特别是商业用途时。
总的来说,这个技术组合为内容创作和设计工作提供了新的可能性。随着AI图像生成技术的不断进步,相信未来会有更多创新的应用场景出现。
获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。