Technical Notes: Improvements to a Python Script for Creating Heatmaps from PNG Files
I've been using a very convenient Python script to create heatmaps from dot distribution images (transparent background PNG files). However, I encountered some issues, so I've improved it. The resulting Python script is now more robust.
ドット分布画像(背景透明pngファイル)からヒートマップを作成するPythonスクリプトをとても便利に使っています。しかし、不都合が生じたので改良しました。より堅牢なPythonスクリプトとなりました。
この記事は2026.06.03記事「技術メモ pngファイルから直接ヒートマップを作成するPythonスクリプト」の続きになります。
1 遭遇した不都合
遭遇した不都合
これまで順調にpngファイルからPythonスクリプトで直接ヒートマップを作成してきました。しかし、上記のような不都合に遭遇しました。
調べたところ、画像の透明部分にRGBの色が残っている部分が存在しているために生じた現象であることがわかりました。Pythonスクリプトは透明か否かではなく、色がある部分=点群データとして扱っているので生じた不都合です。
2 不都合の解決策
この不都合の解決策として、次の2を執りました。
1 画像透明部から色を完全に除去する新たなPythonスクリプト作成。
2 色のある透明部を間違って拾わないヒートマップ作成Pythonスクリプトの作成。
それぞれのPythonスクリプトを作成しましたが、2の新たな改良ヒートマップ作成Pythonスクリプトを使う方が作業の手間がはぶけます。ヒートマップ作成Pythonスクリプトがより堅牢となりました。
3 改良Pythonスクリプト (透明部に色が残っていても正常に作動する改良版)
from pathlib import Path
import cv2
import numpy as np
from PIL import Image
from matplotlib import cm
# =========================
# 設定
# =========================
input_path = r"G:/test/aaa.png"
output_path = r"G:/test/aaa_heatmap.png"
# 使用する単色グラデーション
# 例: "Blues", "Reds", "Greens", "Purples", "Oranges", "Greys"
color_map_name = "Reds"
# グラデーションの区分数
# 例: 5, 8, 10, 16, 32
gradient_steps = 10
# 密度計算のぼかし半径
# 大きいほど広くなめらかな密集度になる
blur_radius = 20
# 背景を透明にするか
transparent_background = False
# 点群抽出に使うアルファ閾値
# alpha > alpha_threshold のピクセルだけを点として扱う
# 0: わずかでも不透明なら点
# 10〜30: ほぼ透明なノイズを除外
alpha_threshold = 20
# ヒートマップの透明出力時、薄い密度部分を透明にする閾値
# 通常は 0.0 のままでOK
heat_alpha_threshold = 0.0
# =========================
# 処理
# =========================
input_file = Path(input_path)
if not input_file.exists():
raise FileNotFoundError(f"画像が読み込めません: {input_path}")
# RGBAで読み込み、RGB値には依存しない
img = Image.open(input_file).convert("RGBA")
arr = np.array(img)
# アルファチャンネルだけで点群を抽出
alpha = arr[:, :, 3]
dot_mask = alpha > alpha_threshold
point_pixels = int(np.count_nonzero(dot_mask))
if point_pixels == 0:
raise ValueError(
"アルファ値から点を検出できませんでした。"
f" alpha_threshold={alpha_threshold} を下げてください。"
)
print(f"画像サイズ: {img.size[0]} x {img.size[1]}")
print(f"alpha min/max: {int(alpha.min())} / {int(alpha.max())}")
print(f"点として使うピクセル数: {point_pixels}")
# 密度画像を作成
density = dot_mask.astype(np.float32)
# ガウシアンぼかしで密集度を計算
density = cv2.GaussianBlur(
density,
ksize=(0, 0),
sigmaX=blur_radius,
sigmaY=blur_radius,
)
# 0〜1に正規化
max_density = float(density.max())
if max_density > 0:
density = density / max_density
# グラデーションを段階化
density_step = np.floor(density * gradient_steps) / gradient_steps
density_step = np.clip(density_step, 0, 1)
# カラーマップ適用
cmap = cm.get_cmap(color_map_name)
heat_rgba = cmap(density_step)
# 0〜255へ変換
heat_img = (heat_rgba[:, :, :3] * 255).astype(np.uint8)
# 密度ゼロ部分の処理
if transparent_background:
out_alpha = (density > heat_alpha_threshold).astype(np.uint8) * 255
output = np.dstack([heat_img, out_alpha])
Image.fromarray(output, mode="RGBA").save(output_path)
else:
# 背景は白
heat_img[density <= heat_alpha_threshold] = [255, 255, 255]
Image.fromarray(heat_img, mode="RGB").save(output_path)
print(f"ヒートマップを書き出しました: {output_path}")

0 件のコメント:
コメントを投稿