File size: 3,079 Bytes
94cb743 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import sys
import argparse
from PIL import Image, ImageStat, ImageEnhance
def adjust_background_brightness(bg_image, mode):
"""
Adjusts the brightness of the background image based on the specified mode.
Args:
- bg_image (Image): The PIL Image object of the background.
- mode (str): The mode for background adjustment ('lighter', 'darker').
Returns:
- Image: The adjusted PIL Image object.
"""
enhancer = ImageEnhance.Brightness(bg_image)
if mode == 'lighter':
return enhancer.enhance(1.5) # Increase brightness
elif mode == 'darker':
return enhancer.enhance(0.5) # Decrease brightness
return bg_image # Return unmodified if mode is not recognized
def generate_complementary_background(image_path, guidance):
"""
Generates a complementary background image based on guidance.
Args:
- image_path (str): Path to the input image.
- guidance (str): Guidance for background generation ('lighter', 'darker', 'color').
Returns:
- Image: A PIL Image object of the generated background.
"""
with Image.open(image_path) as img:
# Assuming a simplistic approach to generate a solid color background
background = Image.new(
'RGB', img.size, (255, 255, 255) if guidance == 'lighter' else (0, 0, 0))
# Adjust the background based on guidance
if guidance in ['lighter', 'darker']:
background = adjust_background_brightness(background, guidance)
# For color or other guidance, additional logic can be implemented
return background
def blend_images(foreground_path, output_path, guidance):
"""
Blends an input image with a dynamically generated background image based on guidance.
Args:
- foreground_path (str): Path to the input image with a transparent background.
- output_path (str): Path where the blended image will be saved.
- guidance (str): Guidance for background generation.
"""
background = generate_complementary_background(foreground_path, guidance)
with Image.open(foreground_path).convert("RGBA") as foreground:
background = background.convert("RGBA")
blended_image = Image.alpha_composite(background, foreground)
blended_image.convert("RGB").save(output_path, "PNG")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Blends an input image with a dynamically generated background image based on guidance.")
parser.add_argument(
"foreground", help="Path to the input image with a transparent background.")
parser.add_argument("output", nargs='?', default="out.png",
help="Path where the blended image will be saved. Defaults to 'out.png'.")
parser.add_argument("--guidance", choices=['lighter', 'darker', 'color'], default='darker',
help="Guidance for the type of background to generate ('lighter', 'darker', 'color'). Defaults to 'darker'.")
args = parser.parse_args()
blend_images(args.foreground, args.output, args.guidance) |