This project provides a flexible framework for generating images using the Flux model from Black Forest Labs. It supports both text-to-image and image-to-image generation using either the Schnell or Dev model variants.
- Clone this repository
git clone https://github.com/einar-hansen/flux-python-package.git
- Create and activate a virtual environment:
python3 -m venv flux_env source flux_env/bin/activate
- Install the required dependencies:
pip install git+https://github.com/huggingface/diffusers.git pip install transformers torch pillow pyyaml term-image accelerate protobuf sentencepiece
Run the main script run_flux.py
with the following syntax:
# source flux_env/bin/activate
python run_flux.py [options] <prompt>
--mode {text2img,img2img,upscale}
: Mode of operation (default: text2img)--model {schnell,dev}
: Model to use (default: schnell)-n, --num_images
: Number of images to generate (default in config.yaml)-o, --output_dir
: Output directory for generated images (default in config.yaml)-b, --base_filename
: Base filename for generated images (default: None, uses SHA256 hash)-g, --guidance_scale
: Guidance scale (default in config.yaml)-H, --height
: Height of the generated image (default in config.yaml)-W, --width
: Width of the generated image (default in config.yaml)-s, --num_inference_steps
: Number of inference steps (default in config.yaml)-v, --view-image
: View the image after generation (default: False)-f, --force
: Force mode: generate all images without prompting (default: False)--lora_model
: Path to the LoRA model (default: None)--lora_scale
: Scale for the LoRA model (default in config.yaml)-i, --input_image
: Path to the input image (required for img2img mode)--strength
: Strength for img2img generation (default in config.yaml)-r, --randomness
: Generate random prompt variants for each image--output_format
: The image format['webp', 'png', 'jpg']
for the output image
Generate a single image from a text prompt:
# source flux_env/bin/activate
python run_flux.py --model schnell --mode text2img "A cyberpunk cityscape"
Generate multiple images with random prompt variants:
# source flux_env/bin/activate
python run_flux.py --model schnell --mode text2img --num_images 3 --randomness "a developer that sits in the office working on a apple mac, very concentrated, can partially see the code on the screen, the office is professional and has a few green plants, scandinavian style."
This appends f.example ", during golden hour time of day", ", in the style of surrealism" or ", in a fantasy setting". You can control what kind of styles you randomly want to apply in the config.yaml
file.
Transform an existing image based on a prompt:
# source flux_env/bin/activate
python run_flux.py --model dev --mode img2img --strength 0.70 --num_inference_steps 50 --input_image images/cityscape.png "turn the landscape into a snowy, white winter wonderland"
Click on the links to learn more about how to use the strength and guidance_scale arguments.
Generate a larger/upscaled image. This only works on the dev model.
# source flux_env/bin/activate
python run_flux.py --model dev --mode upscale --input_image images/portrait-rounded-xs.png "improve image the quality"
Default settings can be adjusted in the config.yaml
file. There are separate configurations for Schnell and Dev models, as well as options for prompt variants.
The script generates a log file named generation_log.csv
in the same directory as the script. This CSV file uses semicolons (;) as delimiters and contains information about each generated image.
- The initial model loading may take some time, but subsequent image generations will be faster.
- Adjust the
num_inference_steps
parameter to balance between generation speed and image quality. - Using a GPU can significantly speed up the image generation process.
Feel free to submit issues or pull requests if you have suggestions for improvements or encounter any problems.
This project is open-source and available under the MIT License.