File size: 2,691 Bytes
9bcf22a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
import requests
from PIL import Image
from io import BytesIO

# Define style tokens or modifiers
styles = {
    "None": " ",
    "Hyper-Realistic": " hyper-realistic",
    "Realistic": " realistic",
    "Flux": " flux",
    "Abstract": " abstract",
    "Cartoon": " cartoon",
    "Ghibli": " ghibli style",
    "Anime": " anime style",
    "Watercolor": " watercolor painting",
    "Pixel Art": " pixel art",
    "Sketch": " sketch",
    "Digital Art": " digital art",
    "Oil Painting": " oil painting",
    "Pastel": " pastel painting",
    "Photorealistic": " photorealistic",
    "Sci-Fi": " sci-fi",
    "Fantasy": " fantasy",
}

def generate_image(prompt, style):
    # Append the style token to the prompt
    style_token = styles.get(style, "")
    full_prompt = prompt + style_token
    
    # Set image parameters
    width = 1024
    height = 1024
    seed = 42
    model = "flux"  # You can adjust the model if needed

    # Construct the URL for pollinations.ai
    image_url = f"https://pollinations.ai/p/{full_prompt}?width={width}&height={height}&seed={seed}&model={model}"
    
    # Download the image using requests
    response = requests.get(image_url)
    if response.status_code == 200:
        image = Image.open(BytesIO(response.content))
        return image
    else:
        return None

def crop_watermark(pil_img):
    """
    Crop the image from the bottom to remove the watermark.
    This function assumes the generated image is 1024x1024,
    and crops it to a height of 960 pixels (removing the bottom 64 pixels).
    """
    width, height = pil_img.size  # Should be 1024 x 1024
    # Crop from the top left (0,0) to (width, 960)
    cropped_img = pil_img.crop((0, 0, width, 960))
    return cropped_img

# Gradio interface function
def interface(prompt, style):
    image = generate_image(prompt, style)
    if image is None:
        return "Error generating image."
    # Crop out the bottom portion with the watermark
    image_cropped = crop_watermark(image)
    return image_cropped

# Define style options for the radio buttons
style_options = list(styles.keys())

# Build Gradio interface using Blocks
with gr.Blocks() as demo:
    gr.Markdown("## Canvasio: AI Image Generator")
    with gr.Row():
        prompt_input = gr.Textbox(label="Enter your prompt", placeholder="A beautiful landscape")
    with gr.Row():
        style_choice = gr.Radio(choices=style_options, label="Select image style", value="Flux")
    generate_btn = gr.Button("Generate Image")
    output_image = gr.Image(label="Generated Image")
    
    generate_btn.click(fn=interface, inputs=[prompt_input, style_choice], outputs=output_image)

demo.launch()