Skip to content

Instantly share code, notes, and snippets.

@Poiuytrezay1
Created November 28, 2023 20:20
Show Gist options
  • Select an option

  • Save Poiuytrezay1/de3af1406b7ff3a6a2db032cd6daa3dd to your computer and use it in GitHub Desktop.

Select an option

Save Poiuytrezay1/de3af1406b7ff3a6a2db032cd6daa3dd to your computer and use it in GitHub Desktop.
Remove adversarial noise from images
import numpy as np
import cv2
import os
import argparse
from pathlib import Path
from cv2.ximgproc import guidedFilter
def main(args):
input = Path(args.input_file_or_dir)
output = input
if input.is_dir():
image_files = [
file
for file in input.iterdir()
if file.suffix in [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".avif"]
]
else:
image_files = [input]
for image_file in image_files:
img = cv2.imread(str(image_file.resolve())).astype(np.float32)
y = img.copy()
for _ in range(64):
y = cv2.bilateralFilter(y, 5, 8, 8)
for _ in range(4):
y = guidedFilter(img, y, 4, 16)
cv2.imwrite(str(image_file.resolve()), y.clip(0, 255).astype(np.uint8))
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument(
"--input_file_or_dir", help="Input file or directory to load the images from"
)
args = argparser.parse_args()
main(args)
@TomLucidor
Copy link

Does this work on the newer version of Glaze and Nightshade? Would like to see if chaiNNer can also handle this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment