我想创建盐和胡椒粉噪音功能。 输入为noise_density,即输出图像中作为噪声的像素数量,应返回的值是噪声图像数据源
linkedListIntersection.js
答案 0 :(得分:0)
此函数返回一个[密度] x [密度]像素的图像,使用numpy生成一个随机数组,使用PIL生成该数组本身的图像。
var upd = sel.selectAll('path').data(district);
upd.enter()
.append('path')
.attr('d', proj.pathFromGeojson)
.attr('pointer-events','none') // add this line
.attr('stroke', 'black')
.attr('fill-opacity', '0.1')
upd.attr('stroke-width', 1 / proj.scale);
例如,现在您可以运行
def salt_pepper(density):
imarray = numpy.random.rand(density,density,3) * 255
return Image.fromarray(imarray.astype('uint8')).convert('L')
要生成500x500px的图像文件。
当然,请确保
salt_pepper(500)
答案 1 :(得分:0)
我想出了一种矢量化解决方案,相信可以改善/简化它。尽管该接口与请求的接口不完全相同,但是该代码非常简单(并且快捷),并且我相信它可以轻松地进行修改。
import numpy as np
from PIL import Image
def salt_and_pepper(image, prob=0.05):
# If the specified `prob` is negative or zero, we don't need to do anything.
if prob <= 0:
return image
arr = np.asarray(image)
original_dtype = arr.dtype
# Derive the number of intensity levels from the array datatype.
intensity_levels = 2 ** (arr[0, 0].nbytes * 8)
min_intensity = 0
max_intensity = intensity_levels - 1
# Generate an array with the same shape as the image's:
# Each entry will have:
# 1 with probability: 1 - prob
# 0 or np.nan (50% each) with probability: prob
random_image_arr = np.random.choice(
[min_intensity, 1, np.nan], p=[prob / 2, 1 - prob, prob / 2], size=arr.shape
)
# This results in an image array with the following properties:
# - With probability 1 - prob: the pixel KEEPS ITS VALUE (it was multiplied by 1)
# - With probability prob/2: the pixel has value zero (it was multiplied by 0)
# - With probability prob/2: the pixel has value np.nan (it was multiplied by np.nan)
# We need to to `arr.astype(np.float)` to make sure np.nan is a valid value.
salt_and_peppered_arr = arr.astype(np.float) * random_image_arr
# Since we want SALT instead of NaN, we replace it.
# We cast the array back to its original dtype so we can pass it to PIL.
salt_and_peppered_arr = np.nan_to_num(
salt_and_peppered_arr, nan=max_intensity
).astype(original_dtype)
return Image.fromarray(salt_and_peppered_arr)
您可以像这样加载黑白版本的Lena:
lena = Image.open("lena.ppm")
bwlena = Image.fromarray(np.asarray(lena).mean(axis=2).astype(np.uint8))
最后,您可以保存几个示例:
salt_and_pepper(bwlena, prob=0.1).save("sp01lena.png", "PNG")
salt_and_pepper(bwlena, prob=0.3).save("sp03lena.png", "PNG")
结果: