mirror of
https://github.com/jantic/DeOldify.git
synced 2026-08-28 17:45:27 +08:00
dff82b6d2e
All the files marked as executable were bugging me, so I ran this: find . -executable -type f -print0 | xargs -0 grep -L '#!' | xargs chmod -x
30 lines
796 B
Python
30 lines
796 B
Python
import random
|
|
|
|
from fastai.vision.image import TfmPixel
|
|
|
|
# Contributed by Rani Horev. Thank you!
|
|
def _noisify(
|
|
x, pct_pixels_min: float = 0.001, pct_pixels_max: float = 0.4, noise_range: int = 30
|
|
):
|
|
if noise_range > 255 or noise_range < 0:
|
|
raise Exception("noise_range must be between 0 and 255, inclusively.")
|
|
|
|
h, w = x.shape[1:]
|
|
img_size = h * w
|
|
mult = 10000.0
|
|
pct_pixels = (
|
|
random.randrange(int(pct_pixels_min * mult), int(pct_pixels_max * mult)) / mult
|
|
)
|
|
noise_count = int(img_size * pct_pixels)
|
|
|
|
for ii in range(noise_count):
|
|
yy = random.randrange(h)
|
|
xx = random.randrange(w)
|
|
noise = random.randrange(-noise_range, noise_range) / 255.0
|
|
x[:, yy, xx].add_(noise)
|
|
|
|
return x
|
|
|
|
|
|
noisify = TfmPixel(_noisify)
|