File size: 3,427 Bytes
21b679c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdedf4
 
21b679c
efdedf4
 
65a9912
efdedf4
 
 
21b679c
 
 
 
 
 
 
 
 
b12ade4
 
 
 
 
65a9912
b12ade4
21b679c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b12ade4
21b679c
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''

1. Accept image via Gradio 

    * image will come out an a numpy array

    Done!

2. resize image 

    * Solved with PIL 

3. split image into blocks 

    * Solved with PIL

4. find color of the square 

    * potentially solved with PIL 

6. Find image with similar color profile

    *need database 

7. Place image in the new output image

    * can be done with PIL

'''

import gradio as gr
import numpy as np
from PIL import Image
from SSIM_PIL import compare_ssim

# Define a function that applies a sepia filter to an image.
def mosiac(input_img):
    pil_img = Image.fromarray(input_img)

    width, height = pil_img.size

    #resizing the image
    if width != height: 
        if width < height:
            #make the dimensions the width
            dims = np.pow(np.floor(np.sqrt(np.sqrt(width))), 4)
        elif height < width: 
            #make dimensions the height
            dims = np.pow(np.floor(np.sqrt(np.sqrt(height))), 4)

    dims = int(dims)
    box = (dims, dims)
    cropped_original = pil_img.resize(box)
    pil_img = cropped_original.copy()

    quant_img = pil_img.quantize(colors = 100)
    
    #putting a grid on the image

    #gridsize
    grid_size = 16

    quant_img = pil_img.quantize(colors = 100)
    
    #putting a grid on the image

    #gridsize
    grid_size = 150

    #size of each image
    img_size = int(width/grid_size)
    
    for column in range(0, grid_size):
        for row in range(0, grid_size):
            box = (int((row - 1) * img_size), int((column - 1) * img_size), int(row * img_size), int(column * img_size))
            tile = quant_img.crop(box)
            tile = tile.quantize(colors=3, method=2)
            tile_rgb = tile.getpalette()[:9]
            first_flag = True
            with open("imageRGBs.txt", "r") as database:
                for line in database: 
                    rgb, img_num = line.split(';')
                    rgb = rgb.split(' ')
                    rgb = list(map(int, rgb))
                    
                    difference = np.abs((tile_rgb[0] - rgb[0]) + (tile_rgb[1] - rgb[1]) + (tile_rgb[2] - rgb[2]))
                    difference = difference + np.abs((tile_rgb[3] - rgb[3]) + (tile_rgb[4] - rgb[4]) + (tile_rgb[5] - rgb[5])) * .5
                    difference = difference + np.abs((tile_rgb[6] - rgb[6]) + (tile_rgb[7] - rgb[7]) + (tile_rgb[8] - rgb[8])) * .333
                    
                    if first_flag:
                        min_diff = difference
                        min_img_num = img_num.replace("\n", "")
                        first_flag = False
                    else: 
                        if (min_diff > difference): 
                            min_diff = difference 
                            min_img_num = img_num.replace("\n", "")
            replace_image = Image.open("images/" + min_img_num)
            replace_image = replace_image.resize((img_size, img_size))
            pil_img.paste(replace_image, box)
    
    ssim_score = compare_ssim(pil_img, cropped_original)
    print(ssim_score)

    return pil_img

# Create a Gradio interface that takes an image input, runs it through the sepia function, and returns the filtered image.
demo = gr.Interface(mosiac, gr.Image(), "image")

# Launch the interface.
if __name__ == "__main__":
    demo.launch(show_error=True)