楽天エリアマップを画像に変換

import cv2
import numpy as np
from PIL import Image
from staticmap import StaticMap


def get_map(
    url, lat=33.84167, lng=132.76611, width=2000, height=2000, zoom=12, fn="map.png"
):

    smap = StaticMap(width, height, url_template=url)

    img = smap.render(zoom=zoom, center=[lng, lat])

    img.save(fn)


kokudo = "https://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png"

rakuten = "https://gateway-api.global.rakuten.com/dsd/geoserver/4g4m/mno_coverage_map/gwc/service/gmaps?LAYERS=mno_coverage_map:all_map&FORMAT=image/png&TRANSPARENT=TRUE&x={x}&y={y}&zoom={z}"

get_map(kokudo, width=4000, height=4000)
get_map(rakuten, width=4000, height=4000, fn="area.png")

# エリア透明

src = cv2.imread("area.png")

# 白をマスク
mask = np.all(src[:, :, :] == [255, 255, 255], axis=-1)

# アルファー化
dst = cv2.cvtColor(src, cv2.COLOR_BGR2BGRA)

# 白は透明
dst[mask, 3] = 0

# 白以外は半透明
dst[~mask, 3] = 180

# 保存
cv2.imwrite("dst.png", dst)

im_map = Image.open("map.png")
im_area = Image.open("dst.png")

im_map.putalpha(255)

# im_area.putalpha(160)

im_raku = Image.alpha_composite(im_map, im_area)

im_raku.save("rakuten.png")