栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python使用cv2读bytes写bytes流不缓存网络传输加噪声(高斯/椒盐/泊松/斑点)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python使用cv2读bytes写bytes流不缓存网络传输加噪声(高斯/椒盐/泊松/斑点)

from PIL import Image, ImageDraw
import random
import io
import cv2
import numpy as np


# 椒盐噪声
def tupian_add_noise_jiaoyan(a):
    image = np.array(bytearray(a), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    # np.ndarray转IMAGE
    # 设置添加椒盐噪声的数目比例
    s_vs_p = 0.5
    # 设置添加噪声图像像素的数目
    amount = 0.04
    noise_img = np.copy(image)
    # 添加salt噪声
    num_salt = np.ceil(amount * image.size * s_vs_p)
    # 设置添加噪声的坐标位置
    coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
    noise_img[coords] = 255
    # 添加pepper噪声
    num_pepper = np.ceil(amount * image.size * (1. - s_vs_p))
    # 设置添加噪声的坐标位置
    coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
    noise_img[coords] = 0
    img_encode = cv2.imencode('.jpg', noise_img)[1]
    data_encode = np.array(img_encode,dtype="uint8")
    return bytes(data_encode)
# 高斯噪声
def tupian_add_noise_gaosi(a):
    image = np.array(bytearray(a), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # 设置高斯分布的均值和方差
    mean = 0
    # 设置高斯分布的标准差
    sigma = 25
    # 根据均值和标准差生成符合高斯分布的噪声
    gauss = np.random.normal(mean, sigma, (image.shape[0], image.shape[1], cv2.IMREAD_COLOR))
    # 给图片添加高斯噪声
    noisy_img = image + gauss
    # 设置图片添加高斯噪声之后的像素值的范围
    noise_img = np.clip(noisy_img, a_min=0, a_max=255)
    # 保存图片
    img_encode = cv2.imencode('.jpg', noise_img)[1]
    data_encode = np.array(img_encode,dtype="uint8")
    return bytes(data_encode)
# 给图片添加泊松噪声
def tupian_add_noise_baosong(a):
    image = np.array(bytearray(a), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # 计算图像像素的分布范围
    vals = len(np.unique(image))
    vals = 2 ** np.ceil(np.log2(vals))
    # 给图片添加泊松噪声
    noise_img = np.random.poisson(image * vals) / float(vals)
    # 保存图片

    img_encode = cv2.imencode('.jpg', noise_img)[1]
    data_encode = np.array(img_encode,dtype="uint8")
    return bytes(data_encode)
# 给图片添加speckle噪声
def tupian_add_noise_speckle(a):
    image = np.array(bytearray(a), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # 随机生成一个服从分布的噪声
    gauss = np.random.randn(image.shape[0], image.shape[1], cv2.IMREAD_COLOR)
    # 给图片添加speckle噪声
    noise_img = image + image * gauss
    # 归一化图像的像素值
    noise_img = np.clip(noise_img, a_min=0, a_max=255)
    img_encode = cv2.imencode('.jpg', noise_img)[1]
    data_encode = np.array(img_encode,dtype="uint8")
    return bytes(data_encode)

函数输入为bytes,输入也是bytes,可用于网络传输,或者写入文件,不用本地缓存

比如:

response = requests.get('http://www.xxx.com/hsdkfjhsfkj.jpg')
a = tupian_add_noise_jiaoyan(response.content)
with open('xxx.jpg','wb')as f:
    f.write(a)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960448.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号