wangzhibo
2025-08-03 56d194a4300610a17cc3c6682aaf63f039e9c4ab
auto_media_publisher/utils/utils_text2image_pollinations.py
@@ -12,6 +12,7 @@
    POLLINATIONS_DEFAULT_HEIGHT ,
    POLLINATIONS_DEFAULT_MODEL,
)
from .utils_images import resize_and_crop_center_any
class Text2ImageGenerator:
@@ -61,12 +62,12 @@
            try:
                response = requests.get(url, timeout=30)
                response.raise_for_status()
                return response.content
                return resize_and_crop_center_any(response.content,self.width,self.height)
            except Exception as e:
                print(f"[!] 请求第 {attempt} 次失败:{e}")
        return None
    def generate_image(self, prompt: str, filename: str = None) -> str:
    def generate_image_with_trans(self, prompt: str, filename: str = None) -> str:
        if not prompt.strip():
            raise ValueError("提示词不能为空")
@@ -90,7 +91,31 @@
        print(f"[✓] 图片已保存:{abs_path}")
        return abs_path
    
    def generate_image_to_stream(self, prompt: str) -> BytesIO:
    def generate_image_without_trans(self, prompt: str, filename: str = None) -> str:
        if not prompt.strip():
            raise ValueError("提示词不能为空")
        url = self.build_url(prompt)
        print(url)
        content = self._fetch_with_retry(url)
        if content is None:
            return None
        if not filename:
            now = datetime.datetime.now()
            filename = now.strftime("%Y%m%d%H%M%S") + ".png"
        if not filename.lower().endswith(".png"):
            filename += ".png"
        with open(filename, "wb") as f:
            f.write(content)
        abs_path = os.path.abspath(filename)
        print(f"[✓] 图片已保存:{abs_path}")
        return abs_path
    def generate_image_with_trans_to_stream(self, prompt: str) -> BytesIO:
        if not prompt.strip():
            raise ValueError("提示词不能为空")