当前位置:   article > 正文

简易的使用stable diffusion进行放大图片_stable diffusion如何放大图片

stable diffusion如何放大图片

这个其实是ipynb文件(因为不会使用ipynb, 只能这样写), 每一个python框都是一个需要运行的code

stabel diffusion upscale是收费的, 不过有100多张的免费额度, 也可以多使用几个邮箱注册, 然后白嫖

pip install stability-sdk
  • 1
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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

这里的STABILITY_KEY需要自己获取, 在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.
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

图片放大是有限制的

在这里插入图片描述

输入最大值是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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 结果展示

未放大之前结果 像素值1000x993

在这里插入图片描述

放大之后结果 像素值2048x2033

在这里插入图片描述

参考链接

stable diffusion官方代码获取处

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/644625
推荐阅读
相关标签
  

闽ICP备14008679号