Created
November 8, 2015 12:39
-
-
Save IJEMIN/4e0b46ec2b63e7c6fc53 to your computer and use it in GitHub Desktop.
Pure Python Box Blur Source.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Box Blur Algorithm from Gerdus van Zyl <[email protected]> | |
# it's a slow.it's not good to realtime stuff. Still it works without any other python library based on C. | |
# Need pypng(pure tiny python image library) for extractiing pixel array of png file. | |
# Portable. Just put png.py from pypng to your project with this file. | |
# This source gonna save your file with name "FINAL.PNG" Reccomend modifying for your project. | |
import png | |
import array | |
import copy | |
import itertools | |
class PixelInfo(): | |
def __init__(self,file): | |
self.file= file | |
self.image = png.Reader(file) | |
self.array = array.array('B') | |
for i in list(self.image.asRGBA()[2]): | |
self.array.extend(i) | |
def _boxBlur(self): | |
N = 5 | |
xsum = 0 | |
xsumar = [] | |
pixels = self.array | |
opixels = copy.copy(pixels) | |
stride = self.image.width * 4 | |
clr = array.array('B', [255, 255, 255, 255]) | |
w = self.image.width | |
h = self.image.height | |
pixel = 0 | |
for rgb in [0, 1, 2]: | |
for x in range(0, w): | |
# print i," of ",w | |
for y in range(0, h): | |
hN = N / 2 | |
offset = (y - hN) * stride + (x - hN) * 4 | |
if offset < 0: offset = 0 | |
#pixel = pixels[offset + rgb] | |
pixel = pixels[offset + rgb] | |
xsumar.append(pixel) | |
if len(xsumar) > N: | |
xsumar = xsumar[-N:] | |
xsum = 0 | |
for v in xsumar: | |
xsum += v | |
xsum /= len(xsumar) | |
pixels[offset + rgb] = xsum | |
# x | |
for rgb in [0, 1, 2]: | |
for y in range(0, h): | |
# print i," of ",w | |
for x in range(0, w): | |
hN = N / 2 | |
offset = (y - hN) * stride + (x - hN) * 4 | |
if offset < 0: offset = 0 | |
pixel = pixels[offset + rgb] | |
xsumar.append(pixel) | |
if len(xsumar) > N: | |
xsumar = xsumar[-N:] | |
xsum = 0 | |
for v in xsumar: | |
xsum += v | |
xsum /= len(xsumar) | |
pixels[offset + rgb] = xsum | |
def _getBlurredImage(pixelInfo): | |
_boxBlur(pixelInfo) | |
tmp_list = list(pixelInfo.array) | |
extracted_pixel_list = [tmp_list[i:i+4] for i in range(0,len(tmp_list),4)] | |
EPL = list() | |
for i in range (0,pixelInfo.image.height): | |
tmplist = [] | |
for k in range(0,pixelInfo.image.width): | |
tmplist.append(extracted_pixel_list[k + i*pixelInfo.image.width]) | |
EPL.append(tmplist) | |
png.from_array(EPL,mode='RGBA',info={'height' : pixelInfo.image.height,'width' : pixelInfo.image.width}).save("FINAL.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment