- Notifications
You must be signed in to change notification settings - Fork0
Universal image reader designed for seamless reading of images into multiple formats, including PIL Images, Torch Tensors, and NumPy arrays.
License
aredden/functis
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
functis
is a universal image reader designed for seamless reading of images into multiple formats, including PIL Images, torch Tensors via torchvision.io, and NumPy arrays via OpenCV. It offers flexibility in device placement, image format, color modes, data types, and output types.
- Read Modes: Supports multiple reading backends including
torch
,np
,pil
, andnvjpeg
.note: torch is torchvision.io, np uses cv2, and nvjpeg is a private library I have yet to expose publically.
- Device Placement: Place your data on any CUDA device or CPU.
- Image Format: Choose between
hwc
orchw
formats. - Color Modes: Multiple color options including
rgb
,bgr
,rgba
,bgra
, andgrayscale
. - Data Types: Supports
uint8
,float32
, andfloat16
fortorch
,nvjpeg
andnp
backends. - Output Types: Get the result in
pil
,np
, ortorch
formats.
git clone https://github.com/aredden/functiscd functispython -m pip install.
fromfunctisimportImageReaderimreader=ImageReader().with_reader("torch").with_device("cuda:0").with_format("hwc")image=imreader.read("./your_image.jpg")
forimageinimreader.read_dir("./your_image_directory"):# process or view your image ...# For recursive reading...forimageinimreader.read_dir("./your_image_directory",recursive=True):# process or view your image ...# Also will optionally include paths...forpath,imageinimreader.read_dir("./your_image_directory",return_paths=True):# process or view your image ...
Can also return paths, similar to read_dir.
forimageinimreader.read_list(list_of_your_images):# process or view your image ...
Relatively new feature (as of oct 11 2023)
withopen("./your_image.jpg","rb")asf:image=imreader.read(f.read())
Inspiration for this idea is that I wanted to have a way to efficienctly read images of all types from a web api, here is a simple fastapi example:
fromtypingimportAnnotatedfromfastapiimportFastAPI,Bodyapp=FastAPI()inference_client= ...@app.post("/inference",tags=["inference"])asyncdefinference(image_bytes:Annotated[bytes,Body(media_type="application/octet-stream")]):globalinference_clientimage=imreader.read(image_bytes)withtorch.no_grad():results_json=inference_client.infer(image)returnresults_json
- Read Modes:
torch
,np
,pil
,nvjpeg
# torch nvjpeg reader is not public yet. - Device:
cuda:<index>
,cuda
,cpu
- Format:
hwc
,chw
- Color Mode:
rgb
,bgr
,rgba
,bgra
,grayscale
- Data Type:
uint8
,float32
,float16
(only for torch and np) - Image Type:
pil
,np
,torch
We welcome contributions! Please see ourcontributing guidelines for more details.