python调用豆包大模型给人脸生成卡通图像
·
首先,注册,获取api-key,拿到endpoint-ip就不讲了,有很多教程!
比如这个 :
【豆包大模型】-Python调用豆包大模型API及文本转语音TTS_豆包tts-CSDN博客
然后代码 :
# happy coding
# -*- coding: UTF-8 -*-
'''
@project:pcProject
@auth:y1441206
@file:tt3.py
@date:2025-08-31 17:11
'''
import os
import base64
from tkinter import filedialog
encoded_image = filedialog.askopenfilename(
title="选择人脸图片",
filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp")]
)
from volcenginesdkarkruntime import Ark
api_key = os.environ.get("ARK_API_KEY", "your api key ")# 请替换为您的API Key , 从环境变量设置 , win是set OPENAI_API_KEY=API_KEY = "xxxxxxx"
# 请确保您已将 API Key 存储在环境变量 ARK_API_KEY 中
# 初始化Ark客户端,从环境变量中读取您的API Key
client = Ark(api_key=api_key)
imagesResponse = client.images.generate(
model="doubao-seededit-3-0-i2i-250628", # 选取一个视觉模型
prompt="生成卡通图像",
image="https://yourserver/a.jpg", # 这里的url要用外网公开访问的图片url
seed=123,
guidance_scale=5.5,
size="adaptive",
watermark=True
)
url = imagesResponse.data[0].url
print(url)
# 后续的一些处理,剋不需要
import requests
# img_url为图片链接,
# file_name为文件储存路径及文件名
file_name = 'aaa.jpg'
res=requests.get(url)
with open(file_name ,'wb') as f:
f.write(res.content)
from PIL import Image
image = Image.open("aaa.jpg")
width, height = image.size
cutoff_height = int(height * 0.2)
cropped_image = image.crop((0, 0, width, height - cutoff_height))
# 保存或显示裁剪后的图像
cropped_image.save("ax3.jpg")
cropped_image.show()
更多推荐
所有评论(0)