Spaces:
Sleeping
Sleeping
init
Browse files- .gitignore +160 -0
- app.py +19 -0
- base_images/pexels-alexas-fotos-11171325.jpg +0 -0
- base_images/pexels-chris-f-2583717.jpg +0 -0
- base_images/pexels-denys-gromov-6769660.jpg +0 -0
- base_images/pexels-klub-boks-6548253.jpg +0 -0
- base_images/pexels-tayla-walsh-2825904.jpg +0 -0
- monkey.py +91 -0
.gitignore
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# pdm
|
| 105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 106 |
+
#pdm.lock
|
| 107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 108 |
+
# in version control.
|
| 109 |
+
# https://pdm.fming.dev/#use-with-ide
|
| 110 |
+
.pdm.toml
|
| 111 |
+
|
| 112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 113 |
+
__pypackages__/
|
| 114 |
+
|
| 115 |
+
# Celery stuff
|
| 116 |
+
celerybeat-schedule
|
| 117 |
+
celerybeat.pid
|
| 118 |
+
|
| 119 |
+
# SageMath parsed files
|
| 120 |
+
*.sage.py
|
| 121 |
+
|
| 122 |
+
# Environments
|
| 123 |
+
.env
|
| 124 |
+
.venv
|
| 125 |
+
env/
|
| 126 |
+
venv/
|
| 127 |
+
ENV/
|
| 128 |
+
env.bak/
|
| 129 |
+
venv.bak/
|
| 130 |
+
|
| 131 |
+
# Spyder project settings
|
| 132 |
+
.spyderproject
|
| 133 |
+
.spyproject
|
| 134 |
+
|
| 135 |
+
# Rope project settings
|
| 136 |
+
.ropeproject
|
| 137 |
+
|
| 138 |
+
# mkdocs documentation
|
| 139 |
+
/site
|
| 140 |
+
|
| 141 |
+
# mypy
|
| 142 |
+
.mypy_cache/
|
| 143 |
+
.dmypy.json
|
| 144 |
+
dmypy.json
|
| 145 |
+
|
| 146 |
+
# Pyre type checker
|
| 147 |
+
.pyre/
|
| 148 |
+
|
| 149 |
+
# pytype static type analyzer
|
| 150 |
+
.pytype/
|
| 151 |
+
|
| 152 |
+
# Cython debug symbols
|
| 153 |
+
cython_debug/
|
| 154 |
+
|
| 155 |
+
# PyCharm
|
| 156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
+
#.idea/
|
app.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import monkey
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
def encode_decode(data, input_image):
|
| 6 |
+
if len(data) > 0:
|
| 7 |
+
fp =tempfile.NamedTemporaryFile(suffix = '.jpg').name
|
| 8 |
+
img = monkey.encode(data)
|
| 9 |
+
img.save(fp)
|
| 10 |
+
return img, fp, None
|
| 11 |
+
elif input_image is not None:
|
| 12 |
+
print(input_image)
|
| 13 |
+
return None, None, monkey.decode(input_image)
|
| 14 |
+
else:
|
| 15 |
+
raise Exception("please provide data OR input_image")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
iface = gr.Interface(fn=encode_decode, inputs=["text", "pil"], outputs=["image", "file", "text"])
|
| 19 |
+
iface.launch()
|
base_images/pexels-alexas-fotos-11171325.jpg
ADDED
|
base_images/pexels-chris-f-2583717.jpg
ADDED
|
base_images/pexels-denys-gromov-6769660.jpg
ADDED
|
base_images/pexels-klub-boks-6548253.jpg
ADDED
|
base_images/pexels-tayla-walsh-2825904.jpg
ADDED
|
monkey.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from email.mime import base
|
| 2 |
+
import glob
|
| 3 |
+
import random
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def random_base_img():
|
| 7 |
+
img_fn = random.choice(glob.glob("./base_images/*.jpg"))
|
| 8 |
+
return Image.open(img_fn, "r")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def modify_pixel(pixel, data):
|
| 12 |
+
data_lst = [format(ord(d), '08b') for d in data]
|
| 13 |
+
data_len = len(data_lst)
|
| 14 |
+
imdata = iter(pixel)
|
| 15 |
+
|
| 16 |
+
for i in range(data_len):
|
| 17 |
+
pix = [value for value in imdata.__next__()[:3] +
|
| 18 |
+
imdata.__next__()[:3] +
|
| 19 |
+
imdata.__next__()[:3]]
|
| 20 |
+
|
| 21 |
+
# Pixel value should be made
|
| 22 |
+
# odd for 1 and even for 0
|
| 23 |
+
for j in range(0, 8):
|
| 24 |
+
if (data_lst[i][j] == '0' and pix[j]% 2 != 0):
|
| 25 |
+
pix[j] -= 1
|
| 26 |
+
|
| 27 |
+
elif (data_lst[i][j] == '1' and pix[j] % 2 == 0):
|
| 28 |
+
if(pix[j] != 0):
|
| 29 |
+
pix[j] -= 1
|
| 30 |
+
else:
|
| 31 |
+
pix[j] += 1
|
| 32 |
+
|
| 33 |
+
# Eighth pixel of every set tells
|
| 34 |
+
# whether to stop ot read further.
|
| 35 |
+
# 0 means keep reading; 1 means thec
|
| 36 |
+
# message is over.
|
| 37 |
+
if (i == data_len - 1):
|
| 38 |
+
if (pix[-1] % 2 == 0):
|
| 39 |
+
if(pix[-1] != 0):
|
| 40 |
+
pix[-1] -= 1
|
| 41 |
+
else:
|
| 42 |
+
pix[-1] += 1
|
| 43 |
+
|
| 44 |
+
else:
|
| 45 |
+
if (pix[-1] % 2 != 0):
|
| 46 |
+
pix[-1] -= 1
|
| 47 |
+
|
| 48 |
+
pix = tuple(pix)
|
| 49 |
+
yield pix[0:3]
|
| 50 |
+
yield pix[3:6]
|
| 51 |
+
yield pix[6:9]
|
| 52 |
+
|
| 53 |
+
def encode(data):
|
| 54 |
+
print("encoding: ", data)
|
| 55 |
+
base_img = random_base_img()
|
| 56 |
+
w = base_img.size[0]
|
| 57 |
+
(x, y) = (0, 0)
|
| 58 |
+
|
| 59 |
+
for pixel in modify_pixel(base_img.getdata(), data):
|
| 60 |
+
base_img.putpixel((x, y), pixel)
|
| 61 |
+
if (x == w - 1):
|
| 62 |
+
x = 0
|
| 63 |
+
y += 1
|
| 64 |
+
else:
|
| 65 |
+
x += 1
|
| 66 |
+
|
| 67 |
+
print("decoded:", decode(base_img.copy()))
|
| 68 |
+
return base_img.copy()
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def decode(img):
|
| 72 |
+
data = ''
|
| 73 |
+
imgdata = iter(img.getdata())
|
| 74 |
+
|
| 75 |
+
while (True):
|
| 76 |
+
pixels = [value for value in imgdata.__next__()[:3] +
|
| 77 |
+
imgdata.__next__()[:3] +
|
| 78 |
+
imgdata.__next__()[:3]]
|
| 79 |
+
|
| 80 |
+
# string of binary data
|
| 81 |
+
binstr = ''
|
| 82 |
+
|
| 83 |
+
for i in pixels[:8]:
|
| 84 |
+
if (i % 2 == 0):
|
| 85 |
+
binstr += '0'
|
| 86 |
+
else:
|
| 87 |
+
binstr += '1'
|
| 88 |
+
|
| 89 |
+
data += chr(int(binstr, 2))
|
| 90 |
+
if (pixels[-1] % 2 != 0):
|
| 91 |
+
return data
|