| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- # import requests
- # url = "http://localhost:8000/auto_post/xiaohongshu/post"
- # data = {
- # "image_paths": ["D:/桌面/研究生/组会/rednote/data/flower.jpg", "D:/桌面/研究生/组会/rednote/data/test_img.png"],
- # "title": "测试标题",
- # "description": "测试描述",
- # "topics": ["测试话题"],
- # "schedule_time": "2025-07-09 10:26"
- # }
- # response = requests.post(url, json=data)
- # print(response.json())
- # curl -X POST "http://localhost:5000/xiaohongshu/post" \
- # -H "Content-Type: application/json" \
- # -d '{
- # "image_paths": ["D:/桌面/研究生/组会/rednote/data/flower.jpg", "D:/桌面/研究生/组会/rednote/data/test_img.png"],
- # "title": "测试标题",
- # "description": "测试描述",
- # "topics": ["测试话题"],
- # "schedule_time": "2025-07-09 10:26"
- # }'
- # import requests
- # def download_video(url, filename):
- # try:
- # # 发起请求,设置stream=True以流式下载
- # response = requests.get(url, stream=True)
- # response.raise_for_status() # 检查请求是否成功
- # # 以二进制写入模式打开文件
- # with open(filename, 'wb') as f:
- # # 逐块写入数据,chunk_size可调整
- # for chunk in response.iter_content(chunk_size=8192):
- # if chunk:
- # f.write(chunk)
- # print(f"视频已成功下载到:{filename}")
- # except Exception as e:
- # print(f"下载过程中出现错误:{e}")
- # # 使用示例
- # video_url = "https://storage.googleapis.com/falserverless/example_outputs/wan-25-i2v-output.mp4" # 请替换为实际视频直链
- # download_video(video_url, "my_video.mp4")
- # import os
- # import fal_client
- # from pathlib import Path
- # from dotenv import load_dotenv
- # env_path = Path("./backend") / ".env"
- # load_dotenv(dotenv_path=env_path)
- # fal_client.fal_key = os.getenv('FAL_KEY2')
- # def on_queue_update(update):
- # if isinstance(update, fal_client.InProgress):
- # for log in update.logs:
- # print(log["message"])
- # url_one = fal_client.upload_file("003.jpg")
- # url_two = fal_client.upload_file("002.jpg")
- # get_cloth_prompt = "Remove the people in the image, but keep all the clothes worn by the people in the image, and display all the extracted clothes on a transparent human-shaped stand."
- # swap_cloth_prompt = "去除这个女人身上的所有衣服,然后把第二张图片中的衣服穿到这个女人身上,保持真实的光影、阴影和布料垂感。在无缝融合新衣物的同时,保持人物肤色、样貌的一致性;保持新衣物颜色、细节、设计和版型的一致性;最终效果应看起来像人物实际上正在穿这件新衣服。这个女人行走在森林中。"
- # for i in range(5):
- # result = fal_client.subscribe(
- # "fal-ai/nano-banana/edit",
- # arguments={
- # "prompt": get_cloth_prompt,
- # "image_urls": [url_one]
- # },
- # with_logs=True,
- # on_queue_update=on_queue_update,
- # )
- # print(f'{i} - ',result["images"][0]["url"])
- from openai import OpenAI
- import os
- import io
- import base64
- from PIL import Image
- from io import BytesIO
- def save_base64_to_image_pillow(base64_string, output_filename):
- """
- 使用Pillow库将Base64字符串保存为图片文件
- Args:
- base64_string (str): Base64编码的图片字符串,可包含或不包含Data URL前缀。
- output_filename (str): 要保存的图片文件名,如 'output.png'。
- """
- # 可选:如果Base64字符串包含Data URL前缀(如"data:image/png;base64,"),需要去除
- if ',' in base64_string:
- base64_string = base64_string.split(',')[1]
- # 1. 将Base64字符串解码为二进制数据
- image_data = base64.b64decode(base64_string)
-
- # 2. 将二进制数据转换为图像对象
- img = Image.open(BytesIO(image_data))
-
- # 3. 保存图像到文件
- img.save(output_filename)
- print(f"图片已保存为: {output_filename}")
- def encode_image(image_path: str) -> str:
- """
- 将图片文件转换为base64编码
-
- Args:
- image_path: 图片文件路径
-
- Returns:
- str: base64编码的图片数据
-
- Raises:
- FileNotFoundError: 图片文件不存在
- IOError: 读取或处理图片失败
- """
- if not os.path.exists(image_path):
- raise FileNotFoundError(f"Image file not found: {image_path}")
-
- with Image.open(image_path) as img:
- buffered = io.BytesIO()
- img.save(buffered, format="JPEG")
- return base64.b64encode(buffered.getvalue()).decode("utf-8")
- client = OpenAI(
- api_key="sk-qnsfJw0vsAitlnrXcOeBYrLbTv9LXfsN1m3jIUfMJagan5IR",
- base_url="https://api.openaius.com/v1"
- )
- user_prompt = "融合两张图片内容"
- image_url1 = encode_image("002.jpg")
- image_url2 = encode_image("003.jpg")
- print(len(image_url1))
- print(len(image_url2))
- response = client.chat.completions.create(
- model="gemini-2.5-flash-image-preview",
- messages=[
- {
- "role": "system",
- "content": "根据用户要求进行图片生成"
- },
- {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": user_prompt
- },
- {
- "type": "image_url",
- "image_url": {
- "url": image_url1
- }
- },
- {
- "type": "image_url",
- "image_url": {
- "url": image_url2
- }
- }
- ]
- }
- ]
- )
- # 写入单行字符串
- text = str(response)
- with open("output.txt", "w", encoding="utf-8") as file:
- file.write(text)
- result = response.choices[0].message.content
- save_base64_to_image_pillow(result, "output.png")
- print(result)
|