Spaces:
Runtime error
Runtime error
Commit ·
47bd721
1
Parent(s): 12c32a1
Delete utils.py
Browse files
utils.py
DELETED
|
@@ -1,75 +0,0 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
|
| 3 |
-
# https://pillow.readthedocs.io/en/stable/reference/Image.html#dither-modes
|
| 4 |
-
DITHER_METHODS = {
|
| 5 |
-
"None": Image.Dither.NONE,
|
| 6 |
-
"Floyd-Steinberg": Image.Dither.FLOYDSTEINBERG
|
| 7 |
-
}
|
| 8 |
-
|
| 9 |
-
#https://pillow.readthedocs.io/en/stable/reference/Image.html#quantization-methods
|
| 10 |
-
QUANTIZATION_METHODS = {
|
| 11 |
-
"Median cut": Image.Quantize.MEDIANCUT,
|
| 12 |
-
"Maximum coverage": Image.Quantize.MAXCOVERAGE,
|
| 13 |
-
"Fast octree": Image.Quantize.FASTOCTREE,
|
| 14 |
-
"libimagequant": Image.Quantize.LIBIMAGEQUANT
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def downscale_image(image: Image, scale: int) -> Image:
|
| 19 |
-
width, height = image.size
|
| 20 |
-
downscaled_image = image.resize((int(width / scale), int(height / scale)), Image.NEAREST)
|
| 21 |
-
return downscaled_image
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def resize_image(image: Image, size) -> Image:
|
| 25 |
-
width, height = size
|
| 26 |
-
resized_image = image.resize((width, height), Image.NEAREST)
|
| 27 |
-
return resized_image
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def limit_colors(
|
| 31 |
-
image,
|
| 32 |
-
limit: int=16,
|
| 33 |
-
palette=None,
|
| 34 |
-
palette_colors: int=256,
|
| 35 |
-
quantize: Image.Quantize=Image.Quantize.MEDIANCUT,
|
| 36 |
-
dither: Image.Dither=Image.Dither.NONE,
|
| 37 |
-
use_k_means: bool=False
|
| 38 |
-
):
|
| 39 |
-
if use_k_means:
|
| 40 |
-
k_means_value = limit
|
| 41 |
-
else:
|
| 42 |
-
k_means_value = 0
|
| 43 |
-
|
| 44 |
-
if palette:
|
| 45 |
-
palette_image = palette
|
| 46 |
-
ppalette = palette.getcolors()
|
| 47 |
-
if ppalette:
|
| 48 |
-
color_palette = palette.quantize(colors=len(list(set(ppalette))))
|
| 49 |
-
else:
|
| 50 |
-
colors = len(palette_image.getcolors()) if palette_image.getcolors() else palette_colors
|
| 51 |
-
color_palette = palette_image.quantize(colors, kmeans=colors)
|
| 52 |
-
else:
|
| 53 |
-
# we need to get palette from image, because
|
| 54 |
-
# dither in quantize doesn't work without it
|
| 55 |
-
# https://pillow.readthedocs.io/en/stable/_modules/PIL/Image.html#Image.quantize
|
| 56 |
-
color_palette = image.quantize(colors=limit, kmeans=k_means_value, method=quantize, dither=Image.Dither.NONE)
|
| 57 |
-
|
| 58 |
-
new_image = image.quantize(palette=color_palette, dither=dither)
|
| 59 |
-
|
| 60 |
-
return new_image
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def convert_to_grayscale(image):
|
| 64 |
-
new_image = image.convert("L")
|
| 65 |
-
return new_image.convert("RGB")
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def convert_to_black_and_white(image: Image, threshold: int=128, is_inversed: bool=False):
|
| 69 |
-
if is_inversed:
|
| 70 |
-
apply_threshold = lambda x : 255 if x < threshold else 0
|
| 71 |
-
else:
|
| 72 |
-
apply_threshold = lambda x : 255 if x > threshold else 0
|
| 73 |
-
|
| 74 |
-
black_and_white_image = image.convert('L', dither=Image.Dither.NONE).point(apply_threshold, mode='1')
|
| 75 |
-
return black_and_white_image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|