
How to Blur NSFW Content in Images with Python
Some platforms need to blur NSFW content instead of removing it — dating apps, art communities, and news sites blur flagged images and let users opt in. Here's how to detect and blur in Python. What You'll Build A Python script that: Sends an image to the NSFW detection API Checks if any label exceeds your confidence threshold Applies a Gaussian blur if flagged Saves the blurred version Prerequisites pip install requests Pillow Complete Script import requests from PIL import Image , ImageFilter from io import BytesIO from pathlib import Path NSFW_API_URL = " https://nsfw-detect3.p.rapidapi.com/nsfw-detect " HEADERS = { " x-rapidapi-host " : " nsfw-detect3.p.rapidapi.com " , " x-rapidapi-key " : " YOUR_API_KEY " , " Content-Type " : " application/x-www-form-urlencoded " , } BLUR_CATEGORIES = { " Explicit Nudity " , " Suggestive " , " Violence " , " Visually Disturbing " } CONFIDENCE_THRESHOLD = 75 BLUR_RADIUS = 40 def moderate_and_blur ( image_url : str , output_dir : str = " . " ) -> d
Continue reading on Dev.to Tutorial
Opens in a new tab
