62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
|
|
||
|
# -------------------------------------------------------------------
|
||
|
# ORTHOGRAPHIC
|
||
|
# Your personal aerial satellite. Always on. At any altitude.*
|
||
|
# Developed by MarStrMind
|
||
|
# License: Open Software License 3.0
|
||
|
# Up to date version always on marstr.online
|
||
|
# -------------------------------------------------------------------
|
||
|
# colorizer.py
|
||
|
# A class providing functions to adjust any image to a specific
|
||
|
# background color. Similar to GIMP's colorize function.
|
||
|
# -------------------------------------------------------------------
|
||
|
|
||
|
|
||
|
from PIL import Image
|
||
|
|
||
|
class mstr_colorizer:
|
||
|
def __init__(self, baseimg):
|
||
|
self._baseimg = baseimg
|
||
|
self._basepix = self._baseimg.load()
|
||
|
self._desaturate()
|
||
|
|
||
|
def _desaturate(self):
|
||
|
grs = Image.new("RGBA", (self._baseimg.width, self._baseimg.height))
|
||
|
pix = grs.load()
|
||
|
for y in range(0, self._baseimg.height):
|
||
|
for x in range(0, self._baseimg.width):
|
||
|
cl = self._basepix[x,y]
|
||
|
sum = cl[0]+cl[1]+cl[2]
|
||
|
gr = int(sum/3)
|
||
|
c = (gr,gr,gr,cl[3])
|
||
|
pix[x,y] = c
|
||
|
|
||
|
self._grs = grs
|
||
|
|
||
|
|
||
|
# img is the background, which can be larger than the image to colorize
|
||
|
# xpos and ypos is where the image should appear
|
||
|
def _colorizeToImage(self, img, xpos, ypos):
|
||
|
colorized = Image.new("RGBA", (self._grs.width, self._grs.height))
|
||
|
clrpix = colorized.load()
|
||
|
imgpix = img.load()
|
||
|
grs = self._grs.load()
|
||
|
|
||
|
for y in range(0, self._grs.height):
|
||
|
for x in range(0, self._grs.width):
|
||
|
if xpos+x >= 0 and xpos+x < img.width and ypos+y >= 0 and ypos+y < img.height:
|
||
|
bgpx = imgpix[xpos+x, ypos+y]
|
||
|
grpx = grs[x,y]
|
||
|
gr = grpx[0]
|
||
|
av = (gr/255) * 1.2
|
||
|
|
||
|
r = bgpx[0] + int(av * bgpx[0])
|
||
|
g = bgpx[1] + int(av * bgpx[1])
|
||
|
b = bgpx[2] + int(av * bgpx[2])
|
||
|
|
||
|
colrz = (r,g,b,grpx[3])
|
||
|
clrpix[x,y] = colrz
|
||
|
|
||
|
return colorized
|
||
|
|