forked from spipm/Depix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_gen_pixelated.py
107 lines (80 loc) · 2.37 KB
/
tool_gen_pixelated.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import argparse
import logging
import os
import shutil
from depix import DepixHelpFormatter
from LoadedImage import LoadedImage
def check_file(s: str) -> str:
if os.path.isfile(s):
return s
else:
raise argparse.ArgumentTypeError("%s is not a file." % repr(s))
def parse_args() -> argparse.Namespace:
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(
description="This command generates pixelized image from a given image.",
formatter_class=(
lambda prog: DepixHelpFormatter(
prog,
**{
"width": shutil.get_terminal_size(fallback=(120, 50)).columns,
"max_help_position": 40,
},
)
),
)
parser.add_argument(
"-i",
"--image",
help="path to image to pixelize",
required=True,
type=check_file,
metavar="PATH",
)
parser.add_argument(
"-o",
"--outputimage",
help="path to output image",
default="output.png",
metavar="PATH",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
imagePath = args.image
image = LoadedImage(imagePath)
outputImage = image.getCopyOfLoadedPILImage()
blockSize = 5
blockPixelCount = blockSize * blockSize
for x in range(0, image.width, blockSize):
for y in range(0, image.height, blockSize):
r = g = b = 0
maxX = min(x + blockSize, image.width)
maxY = min(y + blockSize, image.height)
for xx in range(x, maxX):
for yy in range(y, maxY):
currentPixel = image.imageData[xx][yy]
r += currentPixel[0]
g += currentPixel[1]
b += currentPixel[2]
averageR = int(r / blockPixelCount)
averageG = int(g / blockPixelCount)
averageB = int(b / blockPixelCount)
averageColor = (averageR, averageG, averageB)
for xx in range(x, maxX):
for yy in range(y, maxY):
outputImage.putpixel((xx, yy), averageColor)
outputImage.save(args.outputimage)
if __name__ == "__main__":
main()
# Generated:
# 676c81
# Gimp:
# 878a9e
# diff: 2104861
# Generated:
# 889475
# Gimp:
# a7b194
# diff: 2039071
# ?