-
Notifications
You must be signed in to change notification settings - Fork 0
/
pimage.py
59 lines (56 loc) · 2.38 KB
/
pimage.py
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
from os import path
from sys import argv
from PIL import Image
from PIL.Image import Resampling
from termcolor import colored
import functions as fun
import settings as sel
if fun.isNotebook():
(fPath, fName) = (
'/home/chipdelmal/Documents/Sync/LegoOptimizer/',
'marioTanooki.png'
)
else:
(fPath, fName) = (argv[1], argv[2])
###############################################################################
# Get user selections from file
###############################################################################
(SIZE, PALETTE) = (sel.USER_SEL['size'], sel.USER_SEL['palette'])
###############################################################################
# Load image
###############################################################################
pth = path.join(fPath, fName)
img = Image.open(pth).convert('RGB')
###############################################################################
# Quantize
# 0: median cut, 1: maximum coverage, 2: fast octree
###############################################################################
if isinstance(PALETTE, int):
# Provided palette is a number of target quantization colors --------------
imgQnt = fun.quantizeImage(img, int(PALETTE), method=0)
print(colored(f'+ Quantizing image to {PALETTE} colors', 'red'))
elif isinstance(PALETTE, tuple) or isinstance(PALETTE, list):
# Provided palette is a list or tuple of colors with no block qty ---------
cpal = fun.paletteReshape(PALETTE)
imgQnt = fun.quantizeImage(
img, colorPalette=cpal[1], method=0
)
print(colored(f'+ Quantizing image to palette with no block QTY', 'red'))
elif isinstance(PALETTE, dict):
# Provided palette is a dictionary of colors with block qty ---------------
pal = tuple(PALETTE.keys())
cpal = fun.paletteReshape(pal)
imgQnt = fun.quantizeImage(
img, colorPalette=cpal[1], method=0
)
print(colored(f'+ Quantizing image to palette with block QTY', 'red'))
else:
print(colored("Error in the color palette!", "red"))
###############################################################################
# Downscale
# NEAREST, BILINEAR, BICUBIC, LANCZOS, NEAREST
###############################################################################
pthDWN = path.join(fPath, fName.split('.png')[0]+'_DWN.png')
imgDwn = imgQnt.resize(SIZE, resample=Resampling.LANCZOS)
imgDwn.save(pthDWN)
imgDwn.close()