赞
踩
stabel diffusion upscale是收费的, 不过有100多张的免费额度, 也可以多使用几个邮箱注册, 然后白嫖
pip install stability-sdk
import os import io import warnings from PIL import Image from stability_sdk import client import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation # Our Host URL should not be prepended with "https" nor should it have a trailing slash. os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443' # Sign up for an account at the following link to get an API Key. # https://platform.stability.ai/ # Click on the following link once you have created an account to be taken to your API Key. # https://platform.stability.ai/account/keys # Paste your API Key below. os.environ['STABILITY_KEY'] = 'sk-R9KZW7eCZ5LNF2Qi2h0sv3SMWgx8LiG2RmEg0qhtfxVu69Nx'
这里的STABILITY_KEY需要自己获取, 在key网址获取key
直接复制图片上的key即可
# Set up our connection to the API.
stability_api = client.StabilityInference(
key=os.environ['STABILITY_KEY'], # API Key reference.
upscale_engine="esrgan-v1-x2plus", # The name of the upscaling model we want to use.
# Available Upscaling Engines: esrgan-v1-x2plus
verbose=True, # Print debug messages.
)
图片放大是有限制的
输入最大值是1024x1024个像素, 输出最大是2048x2048个像素
import numpy as np image_path = 'yolov5/train/small_obj/images' # 需要放大图片文件夹路径 image_upscaled_path = './yolov5/train/small_obj/upscaled' # 放大后图片保存的文件夹路径 if not os.path.exists(image_upscaled_path): os.makedirs(image_upscaled_path) img_list = os.listdir(image_path) for img_name in img_list: # Import our local image to use as a reference for our upscaled image. # The 'img' variable below is set to a local file for upscaling, however if you are already running a generation call and have an image artifact available, you can pass that image artifact to the upscale function instead. img = Image.open(os.path.join(image_path, img_name)) ## 当width > height时,将width设置为2048,否则将height设置为2048 if np.array(img).shape[1] > np.array(img).shape[0]: answers = stability_api.upscale( init_image=img, # Pass our image to the API and call the upscaling process. width=2048, # Optional parameter to specify the desired output width. ) else: answers = stability_api.upscale( init_image=img, # Pass our image to the API and call the upscaling process. height=2048, # Optional parameter to specify the desired output height. ) # Set up our warning to print to the console if the adult content classifier is tripped. # If adult content classifier is not tripped, save our image. for resp in answers: for artifact in resp.artifacts: if artifact.finish_reason == generation.FILTER: warnings.warn( "Your request activated the API's safety filters and could not be processed." "Please submit a different image and try again.") if artifact.type == generation.ARTIFACT_IMAGE: big_img = Image.open(io.BytesIO(artifact.binary)) big_img.save(os.path.join(image_upscaled_path, img_name)) # Save our image to a local file. print('{}已完成'.format(img_name))
未放大之前结果 像素值1000x993
放大之后结果 像素值2048x2033
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。