Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add black transparent background color function #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/heat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions examples/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def example1():
# 开始绘制
hm = HeatMap(data)
hm.clickmap(save_as="hit.png")
hm.heatmap(save_as="heat.png")
hm.heatmap(save_as="heat.png",blackbg=True)


def main():
# example1()
example2()
example1()
# example2()


if __name__ == "__main__":
Expand Down
23 changes: 22 additions & 1 deletion pyheatmap/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import random
import Image
import ImageDraw
import ImageDraw2
from inc import cf

Expand Down Expand Up @@ -183,7 +184,7 @@ def sample(self, max_count=None, rate=None):

return data2

def heatmap(self, save_as=None, base=None, data=None):
def heatmap(self, save_as=None, base=None, data=None, blackbg=False):
u"""绘制热图"""

self.__mkImg()
Expand All @@ -201,6 +202,8 @@ def heatmap(self, save_as=None, base=None, data=None):
self.__heat(heat_data, x, y, n, circle)

self.__paintHeat(heat_data, cf.mkColors())
if blackbg == True:
self.__make_img_gray()

if save_as:
self.save_as = save_as
Expand All @@ -218,6 +221,24 @@ def __save(self):
self.__im.save(save_as)
self.__im = None

def __make_img_gray(self):
"""将图像进行变换,增加灰色背景"""
new = Image.new("RGBA", self.__im.size, (0, 0, 0, 200))
dw = ImageDraw.Draw(new)
alpha = 200
rate = 5
for x in range(self.__im.size[0]):
for y in range(self.__im.size[1]):
color = self.__im.getpixel((x, y))
alpha_old = color[3] / 240.0
alpha_new = alpha / 240.0
al = alpha_old + alpha_new - alpha_old * alpha_new
new_color = [int(i * alpha_old * (1 - alpha_new) / al * rate) for i in color[:3]]
al_current = int(al * 240)
new_color.append(al_current)
dw.point((x, y), tuple(new_color))
self.__im = new
return True

def test():
u"""测试方法"""
Expand Down