Upload 13 files
Browse files- .gitattributes +3 -0
- README.md +7 -7
- gitattributes +35 -0
- gitignore +164 -0
- img/1.final_image.jpg +0 -0
- img/1.png +3 -0
- img/2.final_image.jpg +0 -0
- img/2.png +3 -0
- img/3.final_image.jpg +0 -0
- img/3.png +3 -0
- pyproject.toml +3 -0
- requirements.txt +16 -0
- src/app.py +2 -0
- src/utils.py +192 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
img/1.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
img/2.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
img/3.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
-
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
license:
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Change background
|
| 3 |
+
emoji: 👁
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.1.0
|
| 8 |
+
app_file: src/app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
gitignore
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.gradio
|
| 2 |
+
|
| 3 |
+
# Byte-compiled / optimized / DLL files
|
| 4 |
+
__pycache__/
|
| 5 |
+
*.py[cod]
|
| 6 |
+
*$py.class
|
| 7 |
+
|
| 8 |
+
# C extensions
|
| 9 |
+
*.so
|
| 10 |
+
|
| 11 |
+
# Distribution / packaging
|
| 12 |
+
.Python
|
| 13 |
+
build/
|
| 14 |
+
develop-eggs/
|
| 15 |
+
dist/
|
| 16 |
+
downloads/
|
| 17 |
+
eggs/
|
| 18 |
+
.eggs/
|
| 19 |
+
lib/
|
| 20 |
+
lib64/
|
| 21 |
+
parts/
|
| 22 |
+
sdist/
|
| 23 |
+
var/
|
| 24 |
+
wheels/
|
| 25 |
+
share/python-wheels/
|
| 26 |
+
*.egg-info/
|
| 27 |
+
.installed.cfg
|
| 28 |
+
*.egg
|
| 29 |
+
MANIFEST
|
| 30 |
+
|
| 31 |
+
# PyInstaller
|
| 32 |
+
# Usually these files are written by a python script from a template
|
| 33 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 34 |
+
*.manifest
|
| 35 |
+
*.spec
|
| 36 |
+
|
| 37 |
+
# Installer logs
|
| 38 |
+
pip-log.txt
|
| 39 |
+
pip-delete-this-directory.txt
|
| 40 |
+
|
| 41 |
+
# Unit test / coverage reports
|
| 42 |
+
htmlcov/
|
| 43 |
+
.tox/
|
| 44 |
+
.nox/
|
| 45 |
+
.coverage
|
| 46 |
+
.coverage.*
|
| 47 |
+
.cache
|
| 48 |
+
nosetests.xml
|
| 49 |
+
coverage.xml
|
| 50 |
+
*.cover
|
| 51 |
+
*.py,cover
|
| 52 |
+
.hypothesis/
|
| 53 |
+
.pytest_cache/
|
| 54 |
+
cover/
|
| 55 |
+
|
| 56 |
+
# Translations
|
| 57 |
+
*.mo
|
| 58 |
+
*.pot
|
| 59 |
+
|
| 60 |
+
# Django stuff:
|
| 61 |
+
*.log
|
| 62 |
+
local_settings.py
|
| 63 |
+
db.sqlite3
|
| 64 |
+
db.sqlite3-journal
|
| 65 |
+
|
| 66 |
+
# Flask stuff:
|
| 67 |
+
instance/
|
| 68 |
+
.webassets-cache
|
| 69 |
+
|
| 70 |
+
# Scrapy stuff:
|
| 71 |
+
.scrapy
|
| 72 |
+
|
| 73 |
+
# Sphinx documentation
|
| 74 |
+
docs/_build/
|
| 75 |
+
|
| 76 |
+
# PyBuilder
|
| 77 |
+
.pybuilder/
|
| 78 |
+
target/
|
| 79 |
+
|
| 80 |
+
# Jupyter Notebook
|
| 81 |
+
.ipynb_checkpoints
|
| 82 |
+
|
| 83 |
+
# IPython
|
| 84 |
+
profile_default/
|
| 85 |
+
ipython_config.py
|
| 86 |
+
|
| 87 |
+
# pyenv
|
| 88 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 89 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 90 |
+
# .python-version
|
| 91 |
+
|
| 92 |
+
# pipenv
|
| 93 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 94 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 95 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 96 |
+
# install all needed dependencies.
|
| 97 |
+
#Pipfile.lock
|
| 98 |
+
|
| 99 |
+
# poetry
|
| 100 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 101 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 102 |
+
# commonly ignored for libraries.
|
| 103 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 104 |
+
#poetry.lock
|
| 105 |
+
|
| 106 |
+
# pdm
|
| 107 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 108 |
+
#pdm.lock
|
| 109 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 110 |
+
# in version control.
|
| 111 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
| 112 |
+
.pdm.toml
|
| 113 |
+
.pdm-python
|
| 114 |
+
.pdm-build/
|
| 115 |
+
|
| 116 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 117 |
+
__pypackages__/
|
| 118 |
+
|
| 119 |
+
# Celery stuff
|
| 120 |
+
celerybeat-schedule
|
| 121 |
+
celerybeat.pid
|
| 122 |
+
|
| 123 |
+
# SageMath parsed files
|
| 124 |
+
*.sage.py
|
| 125 |
+
|
| 126 |
+
# Environments
|
| 127 |
+
.env
|
| 128 |
+
.venv
|
| 129 |
+
env/
|
| 130 |
+
venv/
|
| 131 |
+
ENV/
|
| 132 |
+
env.bak/
|
| 133 |
+
venv.bak/
|
| 134 |
+
|
| 135 |
+
# Spyder project settings
|
| 136 |
+
.spyderproject
|
| 137 |
+
.spyproject
|
| 138 |
+
|
| 139 |
+
# Rope project settings
|
| 140 |
+
.ropeproject
|
| 141 |
+
|
| 142 |
+
# mkdocs documentation
|
| 143 |
+
/site
|
| 144 |
+
|
| 145 |
+
# mypy
|
| 146 |
+
.mypy_cache/
|
| 147 |
+
.dmypy.json
|
| 148 |
+
dmypy.json
|
| 149 |
+
|
| 150 |
+
# Pyre type checker
|
| 151 |
+
.pyre/
|
| 152 |
+
|
| 153 |
+
# pytype static type analyzer
|
| 154 |
+
.pytype/
|
| 155 |
+
|
| 156 |
+
# Cython debug symbols
|
| 157 |
+
cython_debug/
|
| 158 |
+
|
| 159 |
+
# PyCharm
|
| 160 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 161 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 162 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 163 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 164 |
+
#.idea/
|
img/1.final_image.jpg
ADDED
|
img/1.png
ADDED
|
Git LFS Details
|
img/2.final_image.jpg
ADDED
|
img/2.png
ADDED
|
Git LFS Details
|
img/3.final_image.jpg
ADDED
|
img/3.png
ADDED
|
Git LFS Details
|
pyproject.toml
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
[tool.ruff]
|
| 3 |
+
line-length = 120
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/finegrain-ai/refiners@06204731093d8055e65b21b4da2ce586737d6ea4
|
| 2 |
+
pillow-heif>=0.18.0
|
| 3 |
+
opencv-python>=4.8.0
|
| 4 |
+
numpy>=1.24.0
|
| 5 |
+
torch>=2.0.0
|
| 6 |
+
Pillow>=10.0.0
|
| 7 |
+
gradio>=3.50.0
|
| 8 |
+
safetensors>=0.4.0
|
| 9 |
+
transformers>=4.35.0
|
| 10 |
+
diffusers>=0.24.0
|
| 11 |
+
torchvision
|
| 12 |
+
mediapipe
|
| 13 |
+
transforms
|
| 14 |
+
refiners
|
| 15 |
+
huggingface_hub
|
| 16 |
+
openai
|
src/app.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
exec(os.environ.get('APP'))
|
src/utils.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils.py
|
| 2 |
+
from enum import Enum, auto
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 7 |
+
import cv2
|
| 8 |
+
import numpy as np
|
| 9 |
+
from refiners.fluxion.utils import load_from_safetensors, tensor_to_image
|
| 10 |
+
from refiners.foundationals.clip import CLIPTextEncoderL
|
| 11 |
+
from refiners.foundationals.latent_diffusion import SD1UNet
|
| 12 |
+
from refiners.foundationals.latent_diffusion.stable_diffusion_1 import SD1Autoencoder
|
| 13 |
+
from refiners.foundationals.latent_diffusion.stable_diffusion_1.ic_light import ICLight
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def load_ic_light(device: torch.device, dtype: torch.dtype) -> ICLight:
|
| 17 |
+
return ICLight(
|
| 18 |
+
patch_weights=load_from_safetensors(
|
| 19 |
+
path=hf_hub_download(
|
| 20 |
+
repo_id="refiners/sd15.ic_light.fc",
|
| 21 |
+
filename="model.safetensors",
|
| 22 |
+
revision="ea10b4403e97c786a98afdcbdf0e0fec794ea542",
|
| 23 |
+
),
|
| 24 |
+
),
|
| 25 |
+
unet=SD1UNet(in_channels=4, device=device, dtype=dtype).load_from_safetensors(
|
| 26 |
+
tensors_path=hf_hub_download(
|
| 27 |
+
repo_id="refiners/sd15.realistic_vision.v5_1.unet",
|
| 28 |
+
filename="model.safetensors",
|
| 29 |
+
revision="94f74be7adfd27bee330ea1071481c0254c29989",
|
| 30 |
+
)
|
| 31 |
+
),
|
| 32 |
+
clip_text_encoder=CLIPTextEncoderL(device=device, dtype=dtype).load_from_safetensors(
|
| 33 |
+
tensors_path=hf_hub_download(
|
| 34 |
+
repo_id="refiners/sd15.realistic_vision.v5_1.text_encoder",
|
| 35 |
+
filename="model.safetensors",
|
| 36 |
+
revision="7f6fa1e870c8f197d34488e14b89e63fb8d7fd6e",
|
| 37 |
+
)
|
| 38 |
+
),
|
| 39 |
+
lda=SD1Autoencoder(device=device, dtype=dtype).load_from_safetensors(
|
| 40 |
+
tensors_path=hf_hub_download(
|
| 41 |
+
repo_id="refiners/sd15.realistic_vision.v5_1.autoencoder",
|
| 42 |
+
filename="model.safetensors",
|
| 43 |
+
revision="99f089787a6e1a852a0992da1e286a19fcbbaa50",
|
| 44 |
+
)
|
| 45 |
+
),
|
| 46 |
+
device=device,
|
| 47 |
+
dtype=dtype,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def resize_modulo_8(
|
| 52 |
+
image: Image.Image,
|
| 53 |
+
size: int = 768,
|
| 54 |
+
resample: Image.Resampling | None = None,
|
| 55 |
+
on_short: bool = True,
|
| 56 |
+
) -> Image.Image:
|
| 57 |
+
"""이미지 크기를 8의 배수로 조정"""
|
| 58 |
+
assert size % 8 == 0, "Size must be a multiple of 8 because this is the latent compression size."
|
| 59 |
+
side_size = min(image.size) if on_short else max(image.size)
|
| 60 |
+
scale = size / (side_size * 8)
|
| 61 |
+
new_size = (int(image.width * scale) * 8, int(image.height * scale) * 8)
|
| 62 |
+
return image.resize(new_size, resample=resample or Image.Resampling.LANCZOS)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def adjust_image(
|
| 66 |
+
image: Image.Image,
|
| 67 |
+
brightness=0.0,
|
| 68 |
+
contrast=0.0,
|
| 69 |
+
temperature=0.0,
|
| 70 |
+
saturation=0.0,
|
| 71 |
+
tint=0.0,
|
| 72 |
+
blur_intensity=0,
|
| 73 |
+
exposure=0.0,
|
| 74 |
+
vibrance=0.0,
|
| 75 |
+
color_mixer_blues=0.0,
|
| 76 |
+
) -> Image.Image:
|
| 77 |
+
"""이미지 조정 함수"""
|
| 78 |
+
image = image.convert('RGB')
|
| 79 |
+
|
| 80 |
+
# 노출 조정 (Exposure)
|
| 81 |
+
if exposure != 0.0:
|
| 82 |
+
# Exposure ranges from -5 to 5, where 0 is neutral
|
| 83 |
+
exposure_factor = 1 + (exposure / 5.0)
|
| 84 |
+
exposure_factor = max(exposure_factor, 0.01) # Prevent zero or negative
|
| 85 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 86 |
+
image = enhancer.enhance(exposure_factor)
|
| 87 |
+
|
| 88 |
+
# 밝기 조정
|
| 89 |
+
if brightness != 0.0:
|
| 90 |
+
# Brightness ranges from -5 to 5, mapped to brightness factor
|
| 91 |
+
brightness_factor = 1 + (brightness / 5.0)
|
| 92 |
+
brightness_factor = max(brightness_factor, 0.01) # Prevent zero or negative
|
| 93 |
+
enhancer = ImageEnhance.Brightness(image)
|
| 94 |
+
image = enhancer.enhance(brightness_factor)
|
| 95 |
+
|
| 96 |
+
# 대비 조정
|
| 97 |
+
if contrast != 0.0:
|
| 98 |
+
# Contrast ranges from -100 to 100, mapped to contrast factor
|
| 99 |
+
contrast_factor = 1 + (contrast / 100.0)
|
| 100 |
+
contrast_factor = max(contrast_factor, 0.01) # Prevent zero or negative
|
| 101 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 102 |
+
image = enhancer.enhance(contrast_factor)
|
| 103 |
+
|
| 104 |
+
# 채도 조정 (Vibrance)
|
| 105 |
+
if vibrance != 0.0:
|
| 106 |
+
# Vibrance simulates adjusting the saturation; positive increases saturation, negative decreases
|
| 107 |
+
vibrance_factor = 1 + (vibrance / 100.0)
|
| 108 |
+
vibrance_factor = max(vibrance_factor, 0.0) # Prevent negative saturation
|
| 109 |
+
enhancer = ImageEnhance.Color(image)
|
| 110 |
+
image = enhancer.enhance(vibrance_factor)
|
| 111 |
+
|
| 112 |
+
# 채도 조정 (Saturation)
|
| 113 |
+
if saturation != 0.0:
|
| 114 |
+
# Saturation ranges from -100 to 100, mapped to saturation factor
|
| 115 |
+
saturation_factor = 1 + (saturation / 100.0)
|
| 116 |
+
saturation_factor = max(saturation_factor, 0.0) # Prevent negative saturation
|
| 117 |
+
enhancer = ImageEnhance.Color(image)
|
| 118 |
+
image = enhancer.enhance(saturation_factor)
|
| 119 |
+
|
| 120 |
+
# 색온도 조정
|
| 121 |
+
if temperature != 0.0:
|
| 122 |
+
# To prevent division by zero, adjust temperature calculation
|
| 123 |
+
temp_factor = 1 + (temperature / 100.0)
|
| 124 |
+
temp_factor = max(temp_factor, 0.01) # Prevent zero or negative
|
| 125 |
+
|
| 126 |
+
r, g, b = image.split()
|
| 127 |
+
r = r.point(lambda i: i * temp_factor)
|
| 128 |
+
b = b.point(lambda i: i / temp_factor)
|
| 129 |
+
image = Image.merge('RGB', (r, g, b))
|
| 130 |
+
|
| 131 |
+
# 색조 조정 (Tint)
|
| 132 |
+
if tint != 0.0:
|
| 133 |
+
image_np = np.array(image)
|
| 134 |
+
image_hsv = cv2.cvtColor(image_np, cv2.COLOR_RGB2HSV).astype(np.float32)
|
| 135 |
+
image_hsv[:, :, 0] = (image_hsv[:, :, 0] + tint) % 180
|
| 136 |
+
image_hsv[:, :, 0] = np.clip(image_hsv[:, :, 0], 0, 179)
|
| 137 |
+
image_rgb = cv2.cvtColor(image_hsv.astype(np.uint8), cv2.COLOR_HSV2RGB)
|
| 138 |
+
image = Image.fromarray(image_rgb)
|
| 139 |
+
|
| 140 |
+
# 블러 적용
|
| 141 |
+
if blur_intensity > 0:
|
| 142 |
+
image = image.filter(ImageFilter.GaussianBlur(radius=blur_intensity))
|
| 143 |
+
|
| 144 |
+
# Color Mixer (Blues)
|
| 145 |
+
if color_mixer_blues != 0.0:
|
| 146 |
+
image_np = np.array(image).astype(np.float32)
|
| 147 |
+
# Adjust the blue channel
|
| 148 |
+
image_np[:, :, 2] = np.clip(image_np[:, :, 2] + (color_mixer_blues / 100.0) * 255, 0, 255)
|
| 149 |
+
image = Image.fromarray(image_np.astype(np.uint8))
|
| 150 |
+
|
| 151 |
+
return image
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
class LightingPreference(str, Enum):
|
| 155 |
+
LEFT = auto()
|
| 156 |
+
RIGHT = auto()
|
| 157 |
+
TOP = auto()
|
| 158 |
+
BOTTOM = auto()
|
| 159 |
+
NONE = auto()
|
| 160 |
+
|
| 161 |
+
def get_init_image(self, width: int, height: int, interval: tuple[float, float] = (0.0, 1.0)) -> Image.Image | None:
|
| 162 |
+
"""조명 선호도에 따른 그라데이션 이미지 생성"""
|
| 163 |
+
start, end = interval
|
| 164 |
+
match self:
|
| 165 |
+
case LightingPreference.LEFT:
|
| 166 |
+
tensor = torch.linspace(end, start, width).repeat(1, 1, height, 1)
|
| 167 |
+
case LightingPreference.RIGHT:
|
| 168 |
+
tensor = torch.linspace(start, end, width).repeat(1, 1, height, 1)
|
| 169 |
+
case LightingPreference.TOP:
|
| 170 |
+
tensor = torch.linspace(end, start, height).repeat(1, 1, width, 1).transpose(2, 3)
|
| 171 |
+
case LightingPreference.BOTTOM:
|
| 172 |
+
tensor = torch.linspace(start, end, height).repeat(1, 1, width, 1).transpose(2, 3)
|
| 173 |
+
case LightingPreference.NONE:
|
| 174 |
+
return None
|
| 175 |
+
|
| 176 |
+
return tensor_to_image(tensor).convert("RGB")
|
| 177 |
+
|
| 178 |
+
@classmethod
|
| 179 |
+
def from_str(cls, value: str):
|
| 180 |
+
match value.lower():
|
| 181 |
+
case "left":
|
| 182 |
+
return LightingPreference.LEFT
|
| 183 |
+
case "right":
|
| 184 |
+
return LightingPreference.RIGHT
|
| 185 |
+
case "top":
|
| 186 |
+
return LightingPreference.TOP
|
| 187 |
+
case "bottom":
|
| 188 |
+
return LightingPreference.BOTTOM
|
| 189 |
+
case "none":
|
| 190 |
+
return LightingPreference.NONE
|
| 191 |
+
case _:
|
| 192 |
+
raise ValueError(f"Invalid lighting preference: {value}")
|