import requests from PIL import ImageEnhance

def get_traffic_image(): ' 替換為自己的密鑰 ' ak = 'qf1GuEVTWoUawbNABUHAYXxFY8voeq8l' ' 設置區域的經緯度範圍 ' bounds = '39.822489,116.266998,39.993557,116.49565' ' 設置地圖尺寸 ' size = '4000,4000' ' 設置地圖縮放級別 ' zoom = 11 ' 設置圖像質量 ' quality = 8

' 生成URL '
url = f'http://api.map.baidu.com/staticimage/v2?ak={ak}&zoom={zoom}&bounds={bounds}&traffic=1&quality={quality}'

' 發送HTTP請求 '
response = requests.get(url)

if response.status_code == 200:
    ' 保存圖片 '
    with open(r'C:\Users\jh\Desktop\data\images\traffic_image.png', 'wb') as f:
        f.write(response.content)

    ' 開啟圖片並增強對比度 '
    image = Image.open(r'C:\Users\jh\Desktop\data\images\traffic_image.png')
    enhancer = ImageEnhance.Contrast(image)
    enhanced_image = enhancer.enhance(2)  ' 增強對比度 '
    enhanced_image.save(r'C:\Users\jh\Desktop\data\images\enhanced_traffic_image.png')

    print('實時路況圖片保存成功')
else:
    print('請求失敗')

if name == 'main': get_traffic_image()

Python 获取百度地图实时路况图片并增强对比度

原文地址: https://www.cveoy.top/t/topic/lYzJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录