diff --git a/examples/heat.png b/examples/heat.png index 6550c14..016d2fe 100644 Binary files a/examples/heat.png and b/examples/heat.png differ diff --git a/examples/test.py b/examples/test.py index 9772765..2998393 100644 --- a/examples/test.py +++ b/examples/test.py @@ -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__": diff --git a/pyheatmap/heatmap.py b/pyheatmap/heatmap.py index a2277ea..615caa4 100644 --- a/pyheatmap/heatmap.py +++ b/pyheatmap/heatmap.py @@ -14,6 +14,7 @@ import os import random import Image +import ImageDraw import ImageDraw2 from inc import cf @@ -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() @@ -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 @@ -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"""测试方法"""