deepfakes/faceswap · error · PermissionError

PermissionError while reading: {filename}

Error message

PermissionError while reading: {filename}

What it means

Raised while reading the first 8 bytes of a file that faceswap's PNG dimension parser is probing. The file opened successfully but reading it failed with an OS PermissionError, which faceswap re-raises with the offending filename. This happens inside lib/image.py's PNG header reader (read_dim_batch / dimensions detection path).

Source

Thrown at lib/image.py:276

    >>> width = metadata["width]
    >>> height = metadata["height"]
    >>> faceswap_info = metadata["itxt"]
    """
    retval = {}
    if os.path.splitext(filename)[-1].lower() != ".png":
        # Get the dimensions directly from the image for non-png
        logger.trace(  # type:ignore[attr-defined]
            "Non png found. Loading file for dimensions: '%s'",
            filename)
        img = cv2.imread(filename)
        assert img is not None
        retval["height"], retval["width"] = img.shape[:2]
        return retval
    with open(filename, "rb") as in_file:
        try:
            chunk = in_file.read(8)
        except PermissionError as exc:
            raise PermissionError(f"PermissionError while reading: {filename}") from exc

        if chunk != b"\x89PNG\r\n\x1a\n":
            raise ValueError(f"Invalid header found in png: {filename}")

        while True:
            chunk = in_file.read(8)
            length, field = struct.unpack(">I4s", chunk)
            logger.trace(  # type:ignore[attr-defined]
                "Read chunk: (chunk: %s, length: %s, field: %s",
                chunk, length, field)
            if not chunk or field == b"IDAT":
                break
            if field == b"IHDR":
                # Get dimensions
                chunk = in_file.read(8)
                retval["width"], retval["height"] = struct.unpack(">II", chunk)
                length -= 8
            elif field == b"iTXt":

View on GitHub (pinned to f530cb7508)

Solutions

  1. Fix the file permissions: chmod u+r '<filename>' or take ownership of the dataset folder (chown -R).
  2. Exclude or delete the unreadable file from the input folder and rerun.
  3. Run the faceswap process as a user with read access to the dataset location.
  4. On Windows, close applications (or antivirus) holding an exclusive lock on the file.

Example fix

# before (dataset contains unreadable file)
$ python faceswap.py extract -i /data/src -o /data/faces
# PermissionError while reading: /data/src/locked.png

# after
$ sudo chown -R $(whoami) /data && chmod -R u+rw /data
$ python faceswap.py extract -i /data/src -o /data/faces
Defensive patterns

Strategy: validation

Validate before calling

import os

readable = os.access(filename, os.R_OK)
if not readable:
    print(f'skip unreadable file: {filename}')

Try / catch

try:
    dims = read_image_meta(filename)
except PermissionError as err:
    logger.warning('skipping unreadable %s: %s', filename, err)
    continue

Prevention

When it happens

Trigger: Calling the image dimension API on a file whose read permission is denied for the current user (chmod 000, owned by another user, file locked by another process on Windows). Occurs during batch scans of folders containing unreadable files.

Common situations: Running extraction/training over a dataset copied from another machine with restrictive permissions, files created by a root/service user, security software locking files, or read-only mounts.

Related errors


AI-assisted analysis of deepfakes/faceswap@f530cb7508 (2026-08-15). Data as JSON: /api/errors/97987fab47a4e9d3. Report an issue: GitHub.