File size: 4,311 Bytes
52cbb9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16dea1d
52cbb9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16dea1d
 
 
 
 
 
 
52cbb9c
 
 
 
 
 
16dea1d
 
52cbb9c
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from typing import List
import io
from PIL import Image, ImageOps
import numpy as np
import compColors
import dominantColors
import recolorReinhardAlgo
import recolorOTAlgo
import recolorTransferAlgo
import recolorLumaConverterAlgo
import recolorPaletteBasedTransfer

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
@app.post("/dominantColor/")
async def dominant_color(file: UploadFile = File(...), num_colors: int = Form(...)):
    """
    Receive an image file and an integer and return the dominant color(s).
    """
    print('num_colors: ', num_colors)
    file_content = await file.read()
    image_bytes = io.BytesIO(file_content)
    im = Image.open(image_bytes)
    dominantColorsRGB = dominantColors.findDominantColors(image_bytes, num_colors, False)
    dominantColorsHex = [dominantColors.rgb_to_hex(color) for color in dominantColorsRGB]
    return {"dominantColors": dominantColorsHex}

@app.post("/ColorPalettes/")
async def color_palettes(colors: str = Form(...)):
    """
    Receive an array of strings representing colors and return a color palette based on these colors.
    """
    #maybe this isn't necessary. converting the string to an array of strings
    colors = [color.strip() for color in colors.split(',')]
    #generate the first pallete, which is the complementary colors of the given colors
    complementaryColors = []
    for color in colors:
        complementaryColors.append(compColors.complementary_colors(color))

    #generate the second palette using the adjacent colors algorithm:
    adjacentColors = []
    for color in colors:
        _adjcolors = compColors.adjacent_colors(color)
        for _color in _adjcolors:
            if _color not in adjacentColors:
                adjacentColors.append(_color)
    #generate the third palette using the gradient colors algorithm:
    gradientColors = []
    for i in range(len(colors)-1):
        gradientColors.append(compColors.gradient_colors(colors[i], colors[i+1]))

    #Fixing size of palletes to 5 colors:
    complementaryColors = [complementaryColors[i:i + 5] for i in range(0, len(complementaryColors), 5)]
    adjacentColors = [adjacentColors[i:i + 5] for i in range(0, len(adjacentColors), 5)]
    colors = [colors[i:i + 5] for i in range(0, len(colors), 5)]
    
    return {"inputColor": colors, "complementaryColors": complementaryColors, "adjacentColors": adjacentColors, "gradientColors": gradientColors}

@app.post("/recolor/")
async def recolor(file: UploadFile = File(...), colors: str = Form(...),  model: str = Form(...)):
    """
    Receive an image file and an array of strings representing colors of a selected pallete and recolor an image.
    """
    method = model
    invertColors = False
    colors = [color.strip() for color in colors.split(',')]
    file_content = await file.read()
    image_bytes = io.BytesIO(file_content)
    image = Image.open(image_bytes)
    if invertColors:
        image = ImageOps.invert(image)
    image_np = np.array(image)

    if method == "CCA":
        #Characteristic Color Analysis
        recolorReinhardAlgo.recolor(image_np, colors)
    elif method == "OTA":
        #Optimal Transport Algorithm transfer
        recolorOTAlgo.recolor(image_np, colors)
    elif method =="KMEANS":
        #K-means clustering transfer
        recolorTransferAlgo.recolor(image_np, colors)
    elif method == "LUMA":
        #Luma converter transfer
        recolorLumaConverterAlgo.remap_image_colors(image_np, colors)
    elif method == "palette":
        #palette transfer
        recolorPaletteBasedTransfer.recolor(image_np, colors)
    img_file = open("./result.jpg", "rb")
    return StreamingResponse(img_file, media_type="image/jpeg")
@app.get("/test/")
async def test():
    """
    Test endpoint to check if the server is running.
    """
    return {"message": "Server is running"}





if __name__ == "__main__":
    import uvicorn
    print("Server is running")
    uvicorn.run(app, host="0.0.0.0", port=7860)




#how to run:
#source env/bin/activate     
#uvicorn server:app --reload