AshWu03 commited on
Commit ·
5c3aba6
1
Parent(s): 21b679c
removing lab1
Browse files
lab1.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
'''
|
| 2 |
-
1. Accept image via Gradio
|
| 3 |
-
* image will come out an a numpy array
|
| 4 |
-
Done!
|
| 5 |
-
2. resize image
|
| 6 |
-
* Solved with PIL
|
| 7 |
-
3. split image into blocks
|
| 8 |
-
* Solved with PIL
|
| 9 |
-
4. find color of the square
|
| 10 |
-
* potentially solved with PIL
|
| 11 |
-
6. Find image with similar color profile
|
| 12 |
-
*need database
|
| 13 |
-
7. Place image in the new output image
|
| 14 |
-
* can be done with PIL
|
| 15 |
-
'''
|
| 16 |
-
|
| 17 |
-
import gradio as gr
|
| 18 |
-
import numpy as np
|
| 19 |
-
from PIL import Image
|
| 20 |
-
from SSIM_PIL import compare_ssim
|
| 21 |
-
|
| 22 |
-
# Define a function that applies a sepia filter to an image.
|
| 23 |
-
def mosiac(input_img):
|
| 24 |
-
pil_img = Image.fromarray(input_img)
|
| 25 |
-
|
| 26 |
-
width, height = pil_img.size
|
| 27 |
-
|
| 28 |
-
#resizing the image
|
| 29 |
-
if width != height:
|
| 30 |
-
if width < height:
|
| 31 |
-
#make height the same as width
|
| 32 |
-
box = (0, 0, width, width)
|
| 33 |
-
elif height < width:
|
| 34 |
-
#make width the same as height
|
| 35 |
-
box = (0, 0, height, height)
|
| 36 |
-
pil_img = pil_img.crop(box)
|
| 37 |
-
|
| 38 |
-
width, height = pil_img.size
|
| 39 |
-
dims = np.pow(np.floor(np.sqrt(np.sqrt(width))), 4)
|
| 40 |
-
box = (0, 0, dims, dims)
|
| 41 |
-
cropped_original = pil_img.crop(box)
|
| 42 |
-
pil_img = cropped_original.copy()
|
| 43 |
-
|
| 44 |
-
quant_img = pil_img.quantize(colors = 100)
|
| 45 |
-
|
| 46 |
-
#putting a grid on the image
|
| 47 |
-
|
| 48 |
-
#gridsize
|
| 49 |
-
grid_size = 16
|
| 50 |
-
|
| 51 |
-
#size of each image
|
| 52 |
-
img_size = int(width/grid_size)
|
| 53 |
-
|
| 54 |
-
for column in range(0, grid_size):
|
| 55 |
-
for row in range(0, grid_size):
|
| 56 |
-
box = (int((row - 1) * img_size), int((column - 1) * img_size), int(row * img_size), int(column * img_size))
|
| 57 |
-
tile = quant_img.crop(box)
|
| 58 |
-
tile = tile.quantize(colors=3, method=2)
|
| 59 |
-
tile_rgb = tile.getpalette()[:9]
|
| 60 |
-
first_flag = True
|
| 61 |
-
with open("imageRGBs.txt", "r") as database:
|
| 62 |
-
for line in database:
|
| 63 |
-
rgb, img_num = line.split(';')
|
| 64 |
-
rgb = rgb.split(' ')
|
| 65 |
-
rgb = list(map(int, rgb))
|
| 66 |
-
|
| 67 |
-
difference = np.abs((tile_rgb[0] - rgb[0]) + (tile_rgb[1] - rgb[1]) + (tile_rgb[2] - rgb[2]))
|
| 68 |
-
difference = difference + np.abs((tile_rgb[3] - rgb[3]) + (tile_rgb[4] - rgb[4]) + (tile_rgb[5] - rgb[5])) * .5
|
| 69 |
-
difference = difference + np.abs((tile_rgb[6] - rgb[6]) + (tile_rgb[7] - rgb[7]) + (tile_rgb[8] - rgb[8])) * .333
|
| 70 |
-
|
| 71 |
-
if first_flag:
|
| 72 |
-
min_diff = difference
|
| 73 |
-
min_img_num = img_num.replace("\n", "")
|
| 74 |
-
first_flag = False
|
| 75 |
-
else:
|
| 76 |
-
if (min_diff > difference):
|
| 77 |
-
min_diff = difference
|
| 78 |
-
min_img_num = img_num.replace("\n", "")
|
| 79 |
-
replace_image = Image.open("images\\" + min_img_num)
|
| 80 |
-
replace_image = replace_image.resize((img_size, img_size))
|
| 81 |
-
pil_img.paste(replace_image, box)
|
| 82 |
-
|
| 83 |
-
ssim_score = compare_ssim(pil_img, cropped_original)
|
| 84 |
-
print(ssim_score)
|
| 85 |
-
|
| 86 |
-
return pil_img
|
| 87 |
-
|
| 88 |
-
# Create a Gradio interface that takes an image input, runs it through the sepia function, and returns the filtered image.
|
| 89 |
-
demo = gr.Interface(mosiac, gr.Image(), "image")
|
| 90 |
-
|
| 91 |
-
# Launch the interface.
|
| 92 |
-
if __name__ == "__main__":
|
| 93 |
-
demo.launch(show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|