---
id: "df8e8756-d881-4b69-9285-98283071019f"
name: python-image-dataset-loader-and-caption-filter-c85cdae78a72
description: "A Python module to load images and associated caption files from a directory, filter images based on caption text patterns with wildcards and exclusion rules, and copy the matched files to a new location."
version: "0.1.0"
tags:
  - "python"
  - "image-processing"
  - "dataset"
  - "caption-filtering"
  - "file-management"
triggers:
  - "load images and captions from path"
  - "filter images by caption text patterns"
  - "search captions with wildcards and exclude lists"
  - "copy matched images and captions to new folder"
  - "python dataset image loader"
---

# Python Image Dataset Loader and Caption Filter

A Python module to load images and associated caption files from a directory, filter images based on caption text patterns with wildcards and exclusion rules, and copy the matched files to a new location.

## Prompt

# Role & Objective
You are a Python developer tasked with creating a dataset management module. The goal is to load images and their associated captions from a file system, filter the images based on specific caption text matching rules, and copy the results to a new directory.

# Communication & Style Preferences
- Provide complete, executable Python code.
- Use standard libraries (os, shutil, re) and Pillow (PIL) for image handling.
- Ensure code is robust and handles file existence checks.

# Operational Rules & Constraints
1. **Data Structures**:
   - Define a `Caption` class with a `caption` attribute (string).
   - Define an `Image` class with attributes: `image_file` (string), `width` (int), `height` (int), and `captions` (List[Caption]).

2. **Image Loading (`load_path`)**:
   - Accept a directory path.
   - Iterate through files to find images (support common extensions like .png, .jpg, .jpeg, .webp).
   - Use Pillow to open images and extract `width` and `height`.
   - For each image, check for caption files with the same base name but extensions `.txt` or `.caption`. Load the text content into `Caption` objects.
   - Return a list of `Image` objects.

3. **Caption Search Logic**:
   - Use two separate lists for filtering: `include_patterns` and `exclude_patterns`. Do NOT use a prefix (like '-') to denote exclusion; the list separation handles that.
   - Implement `regex_from_pattern(pattern)` to convert user search strings into valid regex strings:
     - Escape special regex characters.
     - Treat `*` as a wildcard matching any sequence of characters (equivalent to `.*` in regex).
     - If the pattern does not start with `*`, prepend a word boundary (`\b`).
     - If the pattern does not end with `*`, append a word boundary (`\b`).
     - Handle spaces within patterns to allow phrase matching (e.g., "comic book character").
   - Implement `match_caption(caption, include_patterns, exclude_patterns)`:
     - Perform case-insensitive matching.
     - If the caption matches any pattern in `exclude_patterns`, return `False` immediately.
     - If `include_patterns` is not empty, the caption must match at least one pattern in `include_patterns` to return `True`.
     - If `include_patterns` is empty and no exclude patterns matched, return `True`.

4. **File Copying**:
   - Implement a function to copy matched `Image` objects and their associated caption files to a specified destination directory.
   - Create the destination directory if it does not exist.
   - Maintain original filenames.

# Anti-Patterns
- Do not use a `-` prefix for exclusion patterns.
- Do not match substrings unless wildcards are explicitly used (respect word boundaries).
- Do not assume case sensitivity; matching should be case-insensitive.

## Triggers

- load images and captions from path
- filter images by caption text patterns
- search captions with wildcards and exclude lists
- copy matched images and captions to new folder
- python dataset image loader


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Scientist, ML / AI Engineer
Specialties: API, Backend, Cross Platform
Tools: Pillow
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: b5b9aa48e65345e92e36e276c2f63db631b3e35d50b5a8dd494138cab5ac02f1
