How to Detect Nude Images with Python and Pillow: A Complete Guide

This article walks through building a Python3 program that uses the Pillow library to identify skin regions in images, applies color‑space heuristics to classify pixels, merges connected skin areas, and decides whether an image is pornographic based on configurable rules, complete with code samples and testing results.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Detect Nude Images with Python and Pillow: A Complete Guide

Project Introduction

This project uses Python 3 and the Pillow (PIL fork) library to recognize whether an image contains pornographic content by segmenting skin regions.

Environment Setup

Pillow replaces the outdated PIL and supports Python 3. Install it with:

sudo install -U pip3
sudo install Pillow

Program Principle

The algorithm scans each pixel, classifies it as skin based on RGB, normalized RGB, HSV, and YCbCr thresholds, groups adjacent skin pixels into regions, and applies four non‑pornographic rules:

Fewer than three skin regions.

Skin pixels occupy less than 15% of the image.

The largest skin region is under 45% of total skin area.

More than 60 skin regions.

If none of these rules hold, the image is considered nude.

Implementation Script

The core class Nude loads an image, optionally resizes it, and processes pixels:

import sys
import os
import _io
from collections import namedtuple
from PIL import Image

class Nude:
    def __init__(self, path_or_image):
        if isinstance(path_or_image, Image.Image):
            self.image = path_or_image
        elif isinstance(path_or_image, str):
            self.image = Image.open(path_or_image)
        # ...initialize attributes...
        self.width, self.height = self.image.size
        self.total_pixels = self.width * self.height

Key methods include: resize(maxwidth=1000, maxheight=1000) – proportionally scales large images. parse() – iterates over pixels, creates Skin namedtuples, and builds region maps. _classify_skin(r, g, b) – applies the four color‑space rules (currently returns the YCbCr result). _add_merge(_from, _to) and _merge() – combine connected regions. _analyse_regions() – evaluates the final rules and sets self.result and self.message. showSkinRegions() – generates a black‑and‑white visual where white pixels represent detected skin.

Command‑line support uses argparse to accept image files and optional flags for resizing and visualization.

Test Results

Running python3 nude.py -v 1.jpg on a normal photograph produces a result of False with a message indicating the image is not nude, and a visualization image is saved.

Summary

The project demonstrates practical use of Pillow for image analysis, outlines the skin‑detection heuristics, and provides a foundation for further improvements such as refined skin‑color formulas, additional classification criteria, and performance optimizations via multithreading.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPythonImage Processingpillownudity detectionskin segmentation
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.