app-kerrct-39 / models.py
Gertie01's picture
Deploy Gradio app with multiple files
e363c32 verified
from PIL import Image, ImageDraw, ImageFont
import random
import io
# Note: These are MOCK implementations for 'gemini-2' and 'gpt image-1' image remixing.
# Direct multi-image remixing APIs for these specific model names that take three arbitrary
# input images are not publicly available or are not standard usage.
# These functions simulate the desired behavior using the Pillow library for demonstration.
def _get_font(size: int = 24) -> ImageFont.FreeTypeFont:
"""Helper to get a font that is likely available on most systems or a default."""
try:
# Try a common system font
return ImageFont.truetype("arial.ttf", size)
except IOError:
# Fallback to default if Arial is not found
return ImageFont.load_default(size)
def gemini_remix_image(image1: Image.Image | None, image2: Image.Image | None, image3: Image.Image | None, prompt: str) -> Image.Image:
"""
MOCK: Simulates image remixing with a "Gemini-2" style.
Combines up to three input images and adds prompt text dynamically.
"""
canvas_width, canvas_height = 800, 600
output_image = Image.new("RGB", (canvas_width, canvas_height), color=(240, 240, 255)) # Light blueish background
draw = ImageDraw.Draw(output_image)
images = [img for img in [image1, image2, image3] if img is not None]
if not images and not prompt:
# Return a small blank image if no inputs are provided
return Image.new("RGB", (1, 1), color=(255, 255, 255))
# Define positions for images
positions = [
(canvas_width // 4 - 50, canvas_height // 4 - 50), # Top-left quadrant
(canvas_width // 2 + 50, canvas_height // 4 - 50), # Top-right quadrant
(canvas_width // 4, canvas_height // 2 + 50) # Bottom-center, overlapping
]
img_target_size = (canvas_width // 3, canvas_height // 3)
for i, img in enumerate(images):
# Apply a subtle tint and transparency for overlap effect
tint_color = (random.randint(180,220), random.randint(180,220), random.randint(180,220))
img_with_tint = Image.composite(Image.new(img.mode, img.size, tint_color), img, Image.new("L", img.size, 128))
resized_img = img_with_tint.resize(img_target_size)
# Create a mask for semi-transparency
mask = Image.new("L", resized_img.size, int(255 * 0.8)) # 80% opaque
# Paste with alpha composite to handle potential transparency
try:
output_image.paste(resized_img, positions[i % len(positions)], resized_img)
except ValueError: # Fallback for images without alpha channel
output_image.paste(resized_img, positions[i % len(positions)])
# Add prompt text with artistic styling
font_size = 30
font = _get_font(font_size)
text_color = (70, 70, 70) # Darker grey
if prompt:
wrapped_prompt = ""
current_line = ""
words = prompt.split(" ")
for word in words:
if draw.textbbox((0, 0), current_line + " " + word, font=font)[2] < canvas_width * 0.9:
current_line += " " + word
else:
wrapped_prompt += current_line.strip() + "\n"
current_line = word
wrapped_prompt += current_line.strip()
text_bbox = draw.textbbox((0, 0), wrapped_prompt, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
text_x = (canvas_width - text_width) // 2
text_y = canvas_height - text_height - 20
draw.multiline_text((text_x, text_y), wrapped_prompt, font=font, fill=text_color, align="center")
else:
draw.text((canvas_width // 2 - 80, canvas_height - 40), "Gemini-2 AI Magic ✨", font=_get_font(20), fill=(150, 150, 150))
return output_image
def gpt_remix_image(image1: Image.Image | None, image2: Image.Image | None, image3: Image.Image | None, prompt: str) -> Image.Image:
"""
MOCK: Simulates image remixing with a "GPT Image-1" (DALL-E) style.
Combines up to three input images with a different arrangement and adds prompt text.
"""
canvas_width, canvas_height = 800, 600
output_image = Image.new("RGB", (canvas_width, canvas_height), color=(220, 230, 245)) # Soft blue background
draw = ImageDraw.Draw(output_image)
images = [img for img in [image1, image2, image3] if img is not None]
if not images and not prompt:
return Image.new("RGB", (1, 1), color=(255, 255, 255))
# Different layout: Center large image, smaller images in corners
img_large_size = (canvas_width // 2, canvas_height // 2)
img_small_size = (canvas_width // 4, canvas_height // 4)
if len(images) > 0:
main_img = images[0].resize(img_large_size)
output_image.paste(main_img, (canvas_width // 2 - img_large_size[0] // 2, canvas_height // 2 - img_large_size[1] // 2))
if len(images) > 1:
top_left_img = images[1].resize(img_small_size)
output_image.paste(top_left_img, (20, 20))
if len(images) > 2:
bottom_right_img = images[2].resize(img_small_size)
output_image.paste(bottom_right_img, (canvas_width - img_small_size[0] - 20, canvas_height - img_small_size[1] - 20))
# Add artistic borders and effects
for i, img in enumerate(images):
if i < len(images): # Apply to images that were placed
pos = None
size = None
if i == 0 and len(images) > 0: # Main image
pos = (canvas_width // 2 - img_large_size[0] // 2, canvas_height // 2 - img_large_size[1] // 2)
size = img_large_size
elif i == 1 and len(images) > 1: # Top-left
pos = (20, 20)
size = img_small_size
elif i == 2 and len(images) > 2: # Bottom-right
pos = (canvas_width - img_small_size[0] - 20, canvas_height - img_small_size[1] - 20)
size = img_small_size
if pos and size:
# Draw a subtle border
border_color = (random.randint(100, 150), random.randint(100, 150), random.randint(100, 150))
draw.rectangle((pos[0]-2, pos[1]-2, pos[0]+size[0]+2, pos[1]+size[1]+2), outline=border_color, width=3)
# Add prompt text with a distinct style
font_size = 32
font = _get_font(font_size)
text_color = (25, 25, 25) # Very dark grey
if prompt:
text_bbox = draw.textbbox((0, 0), prompt, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_x = (canvas_width - text_width) // 2
draw.text((text_x, 10), prompt, font=font, fill=text_color, stroke_width=1, stroke_fill=(150,150,150))
else:
draw.text((canvas_width // 2 - 90, 10), "GPT Image-1 Creativity", font=_get_font(22), fill=(100, 100, 100))
return output_image