forked from spipm/Depix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genpixed.py
69 lines (45 loc) · 1.35 KB
/
genpixed.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
from depixlib.LoadedImage import *
from depixlib.Rectangle import *
from depixlib.functions import *
import argparse
import logging
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--image', help = 'Path to image to pixelize', required=True)
parser.add_argument('-o', '--outputimage', help = 'Path to output image', nargs='?', default='output.png')
args = parser.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)
# Generated:
# 676c81
# Gimp:
# 878a9e
# diff: 2104861
# Generated:
# 889475
# Gimp:
# a7b194
# diff: 2039071
# ?