qwen_edit.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """
  2. 通义千问图片编辑模块
  3. 使用通义千问图片编辑模型进行图片编辑
  4. """
  5. import os
  6. import json
  7. import base64
  8. import mimetypes
  9. import requests
  10. import dashscope
  11. from dashscope import MultiModalConversation
  12. # 配置API地址(中国北京地域)
  13. # 若使用新加坡地域的模型,需将url替换为:https://dashscope-intl.aliyuncs.com/api/v1
  14. dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
  15. # API Key配置
  16. # 获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key
  17. api_key = "sk-04b63960983445f980d85ff185a17876"
  18. def download_image(image_url, save_path='output.png'):
  19. """
  20. 下载图片到本地
  21. Args:
  22. image_url: 图片URL
  23. save_path: 保存路径
  24. """
  25. try:
  26. response = requests.get(image_url, stream=True, timeout=300)
  27. response.raise_for_status()
  28. with open(save_path, 'wb') as f:
  29. for chunk in response.iter_content(chunk_size=8192):
  30. f.write(chunk)
  31. print(f"图像已成功下载到: {save_path}")
  32. except requests.exceptions.RequestException as e:
  33. print(f"图像下载失败: {e}")
  34. def encode_file(file_path):
  35. """
  36. 将图片文件编码为base64格式的data URI
  37. Args:
  38. file_path: 图片文件路径
  39. Returns:
  40. base64编码的data URI字符串
  41. Raises:
  42. ValueError: 如果文件格式不支持
  43. IOError: 如果读取文件失败
  44. """
  45. mime_type, _ = mimetypes.guess_type(file_path)
  46. if not mime_type or not mime_type.startswith("image/"):
  47. raise ValueError("不支持或无法识别的图像格式")
  48. try:
  49. with open(file_path, "rb") as image_file:
  50. encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
  51. return f"data:{mime_type};base64,{encoded_string}"
  52. except IOError as e:
  53. raise IOError(f"读取文件时出错: {file_path}, 错误: {str(e)}")
  54. def qwen_edit(image_path, prompt, save_path):
  55. """
  56. 使用通义千问图片编辑模型编辑单张图片
  57. Args:
  58. image_path: 输入图片路径
  59. prompt: 编辑提示词
  60. save_path: 保存路径
  61. """
  62. image = encode_file(image_path)
  63. messages = [
  64. {
  65. "role": "user",
  66. "content": [
  67. {"image": image},
  68. {"text": prompt}
  69. ]
  70. }
  71. ]
  72. # 调用图片编辑模型
  73. # qwen-image-edit-plus支持输出1-6张图片,此处设置为1张
  74. response = MultiModalConversation.call(
  75. api_key=api_key,
  76. model="qwen-image-edit-plus-2025-10-30",
  77. messages=messages,
  78. stream=False,
  79. n=1,
  80. watermark=False,
  81. negative_prompt="不要随意减少纽扣或者随意增加纽扣",
  82. prompt_extend=False,
  83. # 仅当输出图像数量n=1时支持设置size参数,否则会报错
  84. # size="2048*1024",
  85. )
  86. if response.status_code == 200:
  87. for i, content in enumerate(response.output.choices[0].message.content):
  88. print(f"输出图像{i+1}的URL: {content['image']}")
  89. download_image(content['image'], save_path)
  90. else:
  91. print(f"HTTP返回码:{response.status_code}")
  92. print(f"错误码:{response.code}")
  93. print(f"错误信息:{response.message}")
  94. print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
  95. def qwen_edit_mutil(image_path_list, prompt, save_path):
  96. """
  97. 使用通义千问图片编辑模型编辑多张图片
  98. Args:
  99. image_path_list: 输入图片路径列表
  100. prompt: 编辑提示词
  101. save_path: 保存路径
  102. """
  103. images = [{"image": encode_file(image_path)} for image_path in image_path_list]
  104. messages = [
  105. {
  106. "role": "user",
  107. "content": images + [{"text": prompt}]
  108. }
  109. ]
  110. # 调用图片编辑模型
  111. response = MultiModalConversation.call(
  112. api_key=api_key,
  113. model="qwen-image-edit-plus-2025-10-30",
  114. messages=messages,
  115. stream=False,
  116. n=1,
  117. watermark=False,
  118. negative_prompt="不要随意减少纽扣或者随意增加纽扣",
  119. prompt_extend=False,
  120. )
  121. if response.status_code == 200:
  122. for i, content in enumerate(response.output.choices[0].message.content):
  123. print(f"输出图像{i+1}的URL: {content['image']}")
  124. download_image(content['image'], save_path)
  125. else:
  126. print(f"HTTP返回码:{response.status_code}")
  127. print(f"错误码:{response.code}")
  128. print(f"错误信息:{response.message}")
  129. print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
  130. if __name__ == "__main__":
  131. image_path = [r"H:\data\线稿图\S1261C003.jpg",r"C:\Users\PC\Desktop\企业微信截图_17636312608996.png"]
  132. prompt = "提取出图1里面整条裤子的平铺精修图,保持原比例,保留衣服的细节,不要随意增加纽扣,图2是图1的细节的补充参考图(图2仅供参考),要严格保留纽扣的数量"
  133. qwen_edit_mutil(image_path,prompt,save_path=r"H:\data\线稿图\S1261C003_S1261C003_concatenated.jpg")