FaYE-image
A image toolkit designed to convert the image file from any supported type to another one with only one line code.
current supported file types:
- .png
- .jpg / .jpeg
- .exr
- .gif
- .npy
current supported runtime data types:
- numpy.ndarray
- torch.Tensor
- cv2.Mat
- PIL.Image.Image
- matplotlib.pyplot.Figure
News
- 2024.10.12: FaYE 0.2.0 is released, bringing a totally new version of FaYE-image, with more operations and easier interfaces.
- 2024.10.14: FaYE 0.2.1 is released, fixing the bug of unexpected image data shape management.
- 2024.10.20: FaYE 0.2.2 is released, fixing the bug of potential data type incompatibility during image resizing.
- 2024.11.07: FaYE 0.3.0 is released, introducing FaYE Visualizer, an interactive visualizer for any image formation algorithm.
- 2024.04.19: FaYE 0.4.0 is released, FaYE Visualizer is deprecated, and introducing the novel FyGUI.
Requirement
Necessary packages
- numpy
- imgui[glfw]
- PyOpenGL
Optional packages
- matplotlib
- PIL
- opencv-python
- torch
- OpenEXR
Usage
Installation:
pip install faye-image
To achieve this easy and uniform image data IO operations, you only need to add:
from faye_image import *
at the beginning of your code.
If you want to load a PNG image file into a torch tensor, you can simply call:
tensor = Convert('path/to/image.png', from_type=PNG_FILE, to_type=TORCH)
Or precisely version:
intermediate = From('path/to/image.png', data_type=PNG_FILE)
tensor = To(intermediate, data_type=TORCH)
If you want to save a torch tensor to a PNG image file, you can simply call:
Convert(tensor, from_type=TORCH, to_type=PNG_FILE, save_path='path/to/image.png', save_mode='RGB')
Or precisely version:
intermediate = From(tensor, data_type=TORCH)
To(intermediate, data_type=PNG_FILE, save_path='path/to/image.png', save_mode='RGB')
If you want to convert a numpy image data to a cv::Mat, you can simply call:
mat = Convert(numpy_image, from_type=NUMPY, to_type=CV_MAT)
Or precisely version:
intermediate = From(numpy_image, data_type=NUMPY)
mat = To(intermediate, data_type=CV_MAT)
Extension
If you want to make this module compatible with more image data types of your interest,
you just need to implement a corresponding builder class inherited from the ImageDataBuilder class.
Then, call the RegisterBuilders(builder1, builder2, ...) at the end of your .py file to add the builder to the image factory.
The builder class should implement the following methods:
CanBuild(data) -> bool: Check if the builder can build the data.GetTag() -> str: Get the tag of the builder.BuildIntermediate(data) -> ImageIntermediate: Build the image intermediate from the data. For current version, the image intermediate is a numpy array in[BxCxHxW]infloat32.BuildData(intermediate: ImageIntermediate, **kwargs): Build the data from the image intermediate. Since theBmaybe not 1, the batch dimension should be considered in the implementation. Returning a list of data is also supported.
Usage Example of FyGUI:
-
Define Input Parameters: Use the
@FyINPUTdecorator on a class and define attributes usingFyinput types.from faye_image.GUI import FyINPUT, FyINT, FySTR, FyBOOL, FyCHOICE, FyContext @FyINPUT class MyInputs: seed = FyINT(0, 10000, default=42) width = FyINT(min_val=50, max_val=500, default=200) height = FyINT(min_val=50, max_val=500, default=150) message = FySTR(default="Hello FyGUI!") color = FyCHOICE(['Red', 'Green', 'Blue', 'Yellow'], default='Blue') use_border = FyBOOL(default=True)
-
Define Callback Function: Create a function that takes the parameter object (an instance reflecting
MyInputs) and aFyContextobject. It should return a fy.ImageIntermediate. This can be obtained byfy.Stash(data, datatype).from PIL import Image, ImageDraw, ImageFont import time def generate_image(params: MyInputs, context: FyContext) -> Image.Image: """Callback that generates a simple image based on inputs.""" print(f"Generating image with seed={params.seed}, size=({params.width}x{params.height})") frame = context.get('frame_count', 0) context['last_seed'] = params.seed img = Image.new('RGB', (params.width, params.height), color='white') draw = ImageDraw.Draw(img) text = f"{params.message}\nSeed: {params.seed}\nFrame: {frame}" text_color = params.color.lower() try: font = ImageFont.load_default() draw.text((10, 10), text, fill=text_color, font=font) except ImportError: draw.text((10, 10), text, fill=text_color) if params.use_border: draw.rectangle([(0, 0), (params.width - 1, params.height - 1)], outline=(0,0,0), width=2) time.sleep(0.1) # Simulate work context['frame_count'] = frame + 1 return fy.Stash(img, fy.PIL_IMAGE)
-
Initialize and Run FyGUI: Instantiate
FyGUIwith your input class and run it with your callback function.from faye_image.GUI import FyGUI # Initialize FyGUI, optionally passing initial context gui = FyGUI(MyInputs, window_title="Simple Image Generator", frame_count=0) # Run the GUI, providing the callback function gui.run(generate_image)
This will launch an interactive window where you can modify the parameters defined in MyInputs and see the generated image update in real-time.
Metadata
Release files for faye-image 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| faye_image-0.4.1.tar.gz | 46.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| faye_image-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 96.9 kB
Release files / faye_image-0.4.1.tar.gz
| Download URL | faye_image-0.4.1.tar.gz |
|---|---|
| Size | 46.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
306f53a9c35a5f7742c691f5fd4275088ca63bedb3e3a66eaa7f8d5de5ba478d
|
|
BLAKE2b-256 checksum How to use checksums |
9917c60f2bb3d99b2ce03034edbe4a70a62c73f28a1c029ead189f53597a60ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / faye_image-0.4.1-py3-none-any.whl
| Download URL | faye_image-0.4.1-py3-none-any.whl |
|---|---|
| Size | 50.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd849482a9fcf12812c9104facd96df349c2ac84168710e03f3033fbcd50b5e8
|
|
BLAKE2b-256 checksum How to use checksums |
6a4ce97078d3b3a40e2c335889070a200ed068347ed6e53b606db54591c0a2b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|