The error message indicates that you cannot apply the ImageEnhance.Sharpness filter to a palette image. The image you are trying to enhance seems to be in the palette mode.

To fix this issue, you can convert the image to RGB mode before applying the enhancement filters. Here's the updated code:

import requests
from PIL import Image, ImageEnhance
from io import BytesIO

# 百度地图API密钥
ak = "qf1GuEVTWoUawbNABUHAYXxFY8voeq8l"
# 区域范围
bounds = "39.915,116.404,39.975,116.414"

# 请求百度地图API获取实时路况图
url = f"http://api.map.baidu.com/staticimage/v2?ak={ak}&width=400&height=300&bounds={bounds}&traffic=1"
response = requests.get(url)

# 读取图片并增强清晰度和对比度
image = Image.open(BytesIO(response.content)).convert("RGB")
enhancer = ImageEnhance.Sharpness(image)
image = enhancer.enhance(2)  # 2倍清晰度
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(1.5)  # 1.5倍对比度

# 保存图片到指定路径
save_path = "C:/Users/jh/Desktop/data/images/traffic_map.png"
image.save(save_path)

By converting the image to RGB mode, you should be able to apply the enhancement filters without any issues.

import requests from PIL import ImageEnhance from io import BytesIO # 百度地图API密钥 ak = qf1GuEVTWoUawbNABUHAYXxFY8voeq8l # 区域范围 bounds = 3991511640439975116414 # 请求百度地图API获取实时路况图 url = fhttpapimapbaid

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

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