A Python-based character image preprocessing tool that transforms character images into a dataset suitable for training through background transparency, boundary cropping, smart cropping, image tagging, image enlargement, etc.
Each function can be executed independently, or they can be used in combination through mixed commands.
- Python 3.10 or higher and its dependencies(check
requirements.txt
) - Git (optional)
- Clone the repository or download zip:
git clone https://github.com/Inupedia/sd-character-image-preprocessing
- Navigate to the project folder to create and activate the Python environment (optional):
cd sd-character-image-preprocessing python3.11 -m venv venv # 3.11 version in this case source venv/bin/activate
- Install the required packages:
pip install -r requirements.txt
- Change the configuration file
module/config_temp.py
toconfig.py
.
Background removal is based on the character detection model. Please choose the model according to your needs (such as isnet-anime corresponds to secondary character).
-
All models are downloaded and saved in the user home folder in the
.u2net
directory.The available models are:
- u2net (download, source): A pre-trained model for general use cases.
- u2netp (download, source): A lightweight version of u2net model.
- u2net_human_seg (download, source): A pre-trained model for human segmentation.
- u2net_cloth_seg (download, source): A pre-trained model for Cloths Parsing from human portrait. Here clothes are parsed into 3 category: Upper body, Lower body and Full body.
- silueta (download, source): Same as u2net but the size is reduced to 43Mb.
- isnet-general-use (download, source): A new pre-trained model for general use cases.
- isnet-anime (download, source): A high-accuracy segmentation for anime character.
- sam (download encoder, download decoder, source): A pre-trained model for any use cases.
- Modify the following configurations in
config.py
, the format is as follows:IMAGE_CONFIG = { # Change to the corresponding model name, such as isnet-anime "REMBG_MODEL": "u2net", }
- Put the images that need to be processed into the
src/input
folder. - run
main.py
:python main.py --remove-bg
Boundary cropping uses an algorithm to find the boundary of the object and remove excess space.
- Modify the following configurations in
config.py
, the format is as follows:IMAGE_CONFIG = { # Modify the target path for cropping images and the path for saving. By default, modify the files under src/output and save them as "original_name_crop.png" in the same path. If you need a different path, please generate the corresponding path first "BOUNDARY_CROP_INPUT_DIR": "./src/rm_bg_output/", "BOUNDARY_CROP_OUTPUT_DIR": "./src/boundary_crop_output/", }
- Run
main.py
:python main.py --boundary-crop
Smart cropping can be used in conjunction with background removal. Please note that when the image resolution is not high, the cropped character will be below 512x512, so it is recommended to enlarge the image after cropping. In the case of multiple people in one picture, multiple images will be automatically cropped based on facial features, but it's not applicable for too dense scenarios.
- Modify the following configurations in
config.py
, the format is as follows:IMAGE_CONFIG = { # Modify the target path for cropping images and the path for saving. By default, modify the files under src/output and save them as "original_name_smartcrop_number.png" in the same path. If you need a different path, please generate the corresponding path first "SMART_CROP_INPUT_DIR": "./src/rm_bg_output/", "SMART_CROP_OUTPUT_DIR": "./src/smart_crop_output/", # Model address, no need to change "HF_REPO_ID": "inupedia/anime-character-image-preprocessing", "HF_MODEL_DIR": "./module/model/", }
- Run
main.py
:python main.py --smart-crop auto # Recommended, scale factor parameter can be adjusted, for example --smart-crop auto 1.5 python main.py --smart-crop auto-fast # Based on lbpcascade_animeface.xml for automatic cropping, fast speed, but may miss some characters
Original Image | Tagger (50% Confidence) |
---|---|
1 | boat, lily pad, flower, multiple girls, 2girls, water, watercraft, lotus, hanfu, sitting, outdoors, black hair, hair flower, hair ornament, chinese clothes, day, holding, long hair, long sleeves, sash, smile, pink flower, looking at another, bangs, hair bun, sidelocks, braid, single hair bun |
- Modify the following configurations in
config.py
, the format is as follows:IMAGE_CONFIG = { "IMAGE_TAGGER_INPUT_DIR": "./src/input/", # Directory of images that need tags "IMAGE_TAGGER_CONFIDENCE": 0.5, # Confidence, the higher the more accurate, but some tags may be missed }
- The model will be automatically downloaded to the corresponding folder. You can manually download wd-v1-4-convnextv2-tagger-v2 model and selected_tags into the
module/image_tagger/model
folder. - Run
main.py
:python main.py --tag
Base on REAL-ESRGAN, a practical algorithm for single image super-resolution with realistic textures. 2
- Modify the following configurations in
config.py
, the format is as follows:IMAGE_CONFIG = { # Change to the corresponding prefix name, such as illust, it will generate illust_1.jpg, illust_2.jpg, etc. "IMAGE_PREFIX": "illust", }
- Put the images that need to be processed into the
src/input
folder. - Run
main.py
:python main.py --rename
This function mainly meets two needs, one is to download all the works of the artist through the artist ID, and the other is to download relevant works through keywords (the number will be downloaded according to the corresponding pages).
-
Please abide by Pixiv's relevant regulations for crawling
-
Modify the following configurations in
config.py
, the format is as follows:NETWORK_CONFIG = { # Proxy settings (no need to modify for Clash, port number needs to be modified for SSR) "PROXY": {"https": "127.0.0.1:7890"}, } USER_CONFIG = { "USER_ID": "Change to your own uid, refer to the URL of the personal profile page https://www.pixiv.net/users/{UID}", "COOKIE": "Change to your own cookie, the acquisition method refers to the following graphic", }
- How to get cookies:
- Log in to pixiv
- Press F12 to open the developer tools
- Click on
Network
- Visit the ranking list and refresh the page
- Find ranking.php and copy the cookie from its Request Headers
- How to get cookies:
-
Crawl its pixiv images according to the artist ID:
python main.py --pixiv-user artist_ID
-
Download according to keywords:
- Modify the following configurations in
config.py
, the format is as follows:IMAGE_CONFIG = { "KEYWORD_ORDER": True, # True: Sort by popularity, False: Sort by newest "KEYWORD_N_PAGES": 5, # 1 page is 60 images, the actual number per page will be more than 60 because some galleries will have multiple images "KEYWORD_MODE": "safe", # safe / r18 / all }
- (PIXIV member feature) Keywords can be searched accurately by combining, such as "50000users AND hutao"
python main.py --pixiv-keyword "keyword"
- Modify the following configurations in
Mixed commands can satisfy multiple tasks to be executed in order. If you want to use multiple commands at the same time, you can use the combination as follows.
python main.py --rename --remove-bg --boundary-crop # Rename first, then remove the background of the image and crop the edge