深度学习图像操作 PIL opencv

这篇博客介绍了如何利用Python的图像处理库创建不同类型的背景图片。首先,展示了如何创建一张空白背景图,然后通过添加高斯噪声来制造随机效果。接着,详细阐述了生成具有晶体纹路的复杂背景的方法。最后,演示了如何从指定目录中选取一张图片作为背景,并进行不失真的调整和裁剪。这些技术对于图像合成和视觉效果的创建非常有用。
摘要由CSDN通过智能技术生成

创建空白图

def plain_white(height, width):
    return Image.new("L", (width, height), 255).convert("RGBA")

背景加高斯噪声。

def gaussian_noise(height, width):
 
    # 创建一个白色图
    image = np.ones((height, width)) * 255
 
    # 添加高斯噪声
    cv2.randn(image, 235, 10)
 
    # 从opencv转PIL.Image ,带有透明度
    return Image.fromarray(image).convert("RGBA")

创建晶体纹路背景图


def quasicrystal(height, width):
    image = Image.new("L", (width, height))
    pixels = image.load()
    frequency = rnd.random() * 30 + 20  # frequency
    phase = rnd.random() * 2 * math.pi  # phase
    rotation_count = rnd.randint(10, 20)  # of rotations
    for kw in range(width):
        y = float(kw) / (width - 1) * 4 * math.pi - 2 * math.pi
        for kh in range(height):
            x = float(kh) / (height - 1) * 4 * math.pi - 2 * math.pi
            z = 0.0
            for i in range(rotation_count):
                r = math.hypot(x, y)
                a = math.atan2(y, x) + i * math.pi * 2.0 / rotation_count
                z += math.cos(r * math.sin(a) * frequency + phase)
            c = int(255 - round(255 * z / rotation_count))
            pixels[kw, kh] = c  # grayscale
    return image.convert("RGBA")

通过图片创建背景

def image(height, width, image_dir):
 
    images = os.listdir(image_dir)
    if len(images) > 0:
        pic = Image.open(
            os.path.join(image_dir, images[rnd.randint(0, len(images) - 1)])
        )
        # 不失真resize
        if pic.size[0] < width:
            pic = pic.resize(
                [width, int(pic.size[1] * (width / pic.size[0]))], Image.ANTIALIAS
            )
        if pic.size[1] < height:
            pic = pic.resize(
                [int(pic.size[0] * (height / pic.size[1])), height], Image.ANTIALIAS
            )
        if pic.size[0] == width:
            x = 0
        else:
            x = rnd.randint(0, pic.size[0] - width)
        if pic.size[1] == height:
            y = 0
        else:
            y = rnd.randint(0, pic.size[1] - height)
        return pic.crop((x, y, x + width, y + height))
    else:
        raise Exception("No images where found in the images folder!")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江小皮不皮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值