Skip to content

Commit

Permalink
Colors update
Browse files Browse the repository at this point in the history
- Linux path fix for an output image file;
- New color modes: "rainbow", "red", "blue";
- "heat_map" and "heat_map_toxic" color modes is now can be used with more shorter "heat" and "heat_toxic" names;
- Docstrings;
- Other minor fixes.
  • Loading branch information
hermanTenuki committed Nov 27, 2020
2 parents 4cd4d53 + 9c84b0f commit e1378db
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 23 deletions.
Binary file modified .github/demo_zip.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,14 @@
Change Log
=========================

3 (27.11.2020)
-------------------------
- Linux path fix for an output image file;
- New color modes: "rainbow", "red", "blue";
- "heat_map" and "heat_map_toxic" color modes is now can be used with more shorter "heat" and "heat_toxic" names;
- Docstrings.
- Other minor fixes.

2.1 (25.11.2020)
-------------------------
- Small linux fix.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -24,19 +24,20 @@ Then go to python terminal and write ```import imageZIP```.

Then you can use 2 available functions:

- ```imageZIP.zip(path: str, scale: int = 1, color_mode: str = 'heat_map')```;
- ```imageZIP.unzip(path: str, scale: int = 1, color_mode: str = 'heat_map')```.
- ```imageZIP.zip(path: str, scale: int = 1, color_mode: str = 'heat')```;
- ```imageZIP.unzip(path: str, scale: int = 1, color_mode: str = 'heat')```.

Where:
- ```path``` - path (```str```) to a single file or whole directory to "zip", or image file to "unzip";
- ```scale```* - scale (```int```) of input or output image file. Default: ```1```;
- ```color_mode```* - color mode (```str```) of input or output image file. Default: ```"heat_map"```.
- ```color_mode```* - color mode (```str```) of input or output image file. Default: ```"heat"```.

> *not necessary attributes, which can be omitted.
#### Notes:

- For ```zip``` and ```unzip``` you have to provide same ```scale``` and ```color_mode``` settings;
- Available ```color_mode```s are: ```bw```, ```heat```, ```heat_toxic```, ```rainbow```, ```red```, ```blue```;
- ```scale``` is have to be ```int >= 1```;
- ```path``` can be absolute or relative;
- Available ```color_mode```s are: ```bw```, ```heat_map```, ```heat_map_toxic```.
- If a good rectangle image can't be created, output image will be a square with some empty pixels at the end.
9 changes: 9 additions & 0 deletions imageZIP/__init__.py
@@ -1 +1,10 @@
# -*- coding: utf-8 -*-
"""
Archive (encrypt) files and directories to image file
"""
from .imageZIP import zip, unzip
__version__ = '3'
__author__ = 'Herman Schechkin (hermanTenuki)'
__copyright__ = 'Copyright (c) 2020 Herman Schechkin'
__license__ = 'MIT'
__email__ = 'itseasy322@gmail.com'
92 changes: 76 additions & 16 deletions imageZIP/imageZIP.py
Expand Up @@ -2,7 +2,6 @@
import io
import os
import math
from pathlib import Path
from sys import platform

Image.MAX_IMAGE_PIXELS = None
Expand All @@ -14,13 +13,15 @@ def _calculate_color(num, color_mode: str):
Calculate color for input byte, represented as 0<=int<=255 (or special float code)
"""

color_max = 255

if type(num) == float:
if num == 0.:
return 255, 250, 255
elif num == 1.:
return 5, 0, 5

if color_mode == 'heat_map_toxic':
if color_mode in ['heat_toxic', 'heat_map_toxic']:
# original num is always be 0 <= num <= 255 (it's actually 256 different bytes)
# by multiplying num, we are getting wider range of colors
num *= 4
Expand All @@ -31,29 +32,63 @@ def _calculate_color(num, color_mode: str):
# Don't forget that 0 counts, so full range from color_min to color_max is 256, not 255
color_min = 0

elif color_mode == 'heat_map':
elif color_mode in ['heat', 'heat_map']:
num *= 3
palette1 = 192
color_min = 20
elif color_mode == 'bw':
return num, num, num
elif color_mode == 'rainbow':
num *= 5
palette1 = 183 # imperfect, actually 182.85
color_min = 20
elif color_mode == 'red':
return color_max, color_max-num, color_max-num
elif color_mode == 'blue':
return color_max-num, color_max-num, color_max
else:
raise AttributeError(f'"{color_mode}" color_mode does not exist.')

color_max = color_min + palette1 - 1
palette2 = palette1 * 2
palette3 = palette2 + palette1
if num < palette1:
return color_min, color_min + num, color_max
elif num < palette2:
num -= palette1
return color_min, color_max, color_max - num
elif num < palette3:
num -= palette2
return color_min + num, color_max, color_min

if color_mode == 'rainbow':
palette4 = palette3 + palette1
palette5 = palette4 + palette1
palette6 = palette5 + palette1
if num < palette1:
return color_max, color_max-num, color_max-num
if num < palette2:
num -= palette1
return color_max, color_min+num, color_min
elif num < palette3:
num -= palette2
return color_max-num, color_max, color_min
elif num < palette4:
num -= palette3
return color_min, color_max, color_min+num
elif num < palette5:
num -= palette4
return color_min, color_max-num, color_max
elif num < palette6:
num -= palette5
return color_min + num, color_min, color_max
else:
num -= palette6
return color_max-num, color_min, color_max-num
else:
num -= palette3
return color_max, color_max - num, color_min
if num < palette1:
return color_min, color_min + num, color_max
elif num < palette2:
num -= palette1
return color_min, color_max, color_max - num
elif num < palette3:
num -= palette2
return color_min + num, color_max, color_min
else:
num -= palette3
return color_max, color_max - num, color_min


def _calculate_sizes(multiplier, perimeter):
Expand Down Expand Up @@ -169,10 +204,14 @@ def draw_bytes_as_image(bts, color_mode, scale, **kwargs):

def img_save(img, path, **kwargs):
"""
Save final img
Save final image
"""

path = os.path.normpath(path)

if not ON_WINDOWS:
path = path.replace(os.sep, '/').replace('\\', '/')

img.save(path + '_zip.png')


Expand Down Expand Up @@ -233,16 +272,37 @@ def encrypt_hub(path, **kwargs):

def zip(path: str,
scale: int = 1,
color_mode: str = 'heat_map'):
color_mode: str = 'heat'):
"""
Encrypt (zip) chosen file or whole directory into a single image file.
:param path: Path to a single file or whole directory to "zip". Can be absolute or relative.
:param scale: Scale of output image file. Default: 1;
:param color_mode: Color mode of output image file.
Available color_mode's are: "bw", "heat", "heat_toxic", "rainbow", "red", "blue".
Default: "heat".
"""

color_mode = color_mode.lower()

encrypt_hub(path=path, scale=scale, color_mode=color_mode)


def unzip(path: str,
scale: int = 1,
color_mode: str = 'heat_map'):
color_mode: str = 'heat'):
"""
Decrypt (unzip) files from an image file.
:param path: Path to an image file to "unzip". Can be absolute or relative.
:param scale: Scale of input image file. Default: 1;
:param color_mode: Color mode of input image file.
Available color_mode's are: "bw", "heat", "heat_toxic", "rainbow", "red", "blue".
Default: "heat".
"""
if os.path.isdir(path):
file_chosen = False
else:
file_chosen = True

color_mode = color_mode.lower()

decrypt_image(path=path, scale=scale, color_mode=color_mode, file_chosen=file_chosen)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -4,7 +4,7 @@
long_description = file.read()

setup(name='imageZIP',
version='2.1',
version='3',
description='Archive (encrypt) files and directories to image file',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
10 changes: 8 additions & 2 deletions tests.py
Expand Up @@ -88,6 +88,7 @@ def test_folder(self):
with open(path_image_file, 'rb') as file:
image_file_new = file.read()
self.assertEqual(image_file_new, image_file)
self.assertFalse(os.path.exists('test folder\\0') and os.path.isdir('test folder\\0'))

os.remove(zip_path)

Expand Down Expand Up @@ -154,16 +155,21 @@ def test_scale(self):
self._test_with_settings(scale=0)
with self.assertRaises(TypeError):
self._test_with_settings(scale=1.2)
self._test_with_settings(scale=8)
self._test_with_settings(scale=4)

def test_colors(self):
with self.assertRaises(AttributeError):
self._test_with_settings(color_mode='random123123')
self._test_with_settings(color_mode='bw')
self._test_with_settings(color_mode='rainbow')
self._test_with_settings(color_mode='RaInBow') # Test char cases
self._test_with_settings(color_mode='red')
self._test_with_settings(color_mode='blue')
self._test_with_settings(color_mode='heat')
self._test_with_settings(color_mode='heat_toxic')
self._test_with_settings(color_mode='heat_map')
self._test_with_settings(color_mode='heat_map_toxic')


if __name__ == '__main__':
# imageZIP.unzip('tests/test folder_zip.png')
unittest.main()

0 comments on commit e1378db

Please sign in to comment.