Prepare images for AI model ingestion
Project description
img_ai_prep
More intelligently crop photos for ingestion to ML models. Supports two major workflows that will be useful to produce higher quality images:
resize_center_crop
- Resize the image with the shortest side matching the desired end size
- Crop only the longer side to achieve final square image, preserving maximum content towards the center
face_center_crop
- Resize the image with the shortest side matching the desired end size
- Crop towards detected faces in the image, preserving maximum human content
In addition to these enhanced cropping modes, the code also supports cropping towards faces without resizing,
center cropping without resizing,
and exposes the underlying crop and resize functions for use in your own workflows.
There is also support for easily passing custom models for face detection, and passing custom parameters to the models. Passing the model by argument also improves performance if you are working in a loop, as it does not need to read the model from disk every time if passed by argument.
Usage
| Original Image | noresize_center_crop | resize_center_crop | face_center_crop | noresize_face_center |
|---|---|---|---|---|
Simply install with pip and get to processing your images!
pip install img_ai_prep
from PIL import Image
from img_ai_prep import img_ai_prep
im = Image.open("input.jpg")
newimg = img_ai_prep(im,
final_size=1024,
crop_mode="resize_center_crop")
print(newimg.size)
newimg.save("output.jpg")
Advanced example script passing a custom model and parameters:
import cv2
from PIL import Image
from img_ai_prep import img_ai_prep
if __name__ == "__main__":
im = Image.open("my_img.jpg")
model = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_frontalface_alt.xml")
newimg = img_ai_prep(im,
final_size=1024,
crop_mode="face_center_crop",
model=model,
min_neighbors=50,
min_size=(100,100))
print(newimg.size)
newimg.save("my_output.jpg")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file img_ai_prep-0.0.1.tar.gz.
File metadata
- Download URL: img_ai_prep-0.0.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3f5e9748e2df04120bacc2c584cc0ccbd8a00597d54cefdb5a58e0ee0152e6f
|
|
| MD5 |
b05c47b77a1451ddb33bfccac622b3cd
|
|
| BLAKE2b-256 |
b1cb2441fb8c1e3df9b3240f1758f6142b5a47845d1ab568bd3e9edd36e266db
|
File details
Details for the file img_ai_prep-0.0.1-py3-none-any.whl.
File metadata
- Download URL: img_ai_prep-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e16bb42d6157e017c12c09f7c5e8136851927b1c883722f3d639463384f721bb
|
|
| MD5 |
afb68c25554e3c238977c1ee8f046478
|
|
| BLAKE2b-256 |
ed10d1a1d42987a33976ed4f64c6aecdfe04e5b3064d38a824e3203ace380247
|