import os
from osgeo import ogr, gdal, osr
from PIL import Image, ImageDraw, ImageFont
def transform_point(point, inv_geo_transform):
"""将地理坐标转换为像素坐标"""
pixel_x = int(inv_geo_transform[0] + inv_geo_transform[1] * point[0] + inv_geo_transform[2] * point[1])
pixel_y = int(inv_geo_transform[3] + inv_geo_transform[4] * point[0] + inv_geo_transform[5] * point[1])
return pixel_x, pixel_y
def draw_geometry(geom, draw, inv_geo_transform):
"""根据几何类型绘制相应的线"""
geom_type = geom.GetGeometryName()
if geom_type == 'LINESTRING' or geom_type == 'LINEARRING':
points = geom.GetPoints()
pixel_points = [transform_point(point, inv_geo_transform) for point in points]
draw.line(pixel_points, fill=(255, 0, 0), width=5) # 红色线条,宽度5像素
elif geom_type == 'MULTILINESTRING':
# MULTILINESTRING 是多个 LINESTRING 的组合
for i in range(geom.GetGeometryCount()):
sub_geom = geom.GetGeometryRef(i)
draw_geometry(sub_geom, draw, inv_geo_transform)
def draw_shapefile_on_image(shp_file, jpg_image, reference_ds):
"""将线矢量文件的几何形状绘制到图像上"""
# 获取参考栅格的地理变换
geo_transform = reference_ds.GetGeoTransform()
inv_geo_transform = gdal.InvGeoTransform(geo_transform)
# 打开Shapefile
shapefile = ogr.Open(shp_file)
layer = shapefile.GetLayer()
# 创建一个ImageDraw对象
draw = ImageDraw.Draw(jpg_image)
# 遍历所有几何对象并将它们绘制到图像上
for feature in layer:
geom = feature.GetGeometryRef()
draw_geometry(geom, draw, inv_geo_transform)
def process_image(tiffile, shpfile, outpath):
# 打开TIF文件
tif_image = Image.open(tiffile)
# 提取日期和时间部分
filename = os.path.basename(tiffile) # 获取文件名
timestamp = filename[0:16] # 假设文件名前面部分为日期时间,如09_22_16_50
timestamp = timestamp.split('_')
timestamp = "{}-{}-{} {}:{}".format(timestamp[0], timestamp[1], timestamp[2], timestamp[3], timestamp[4])
# 转换为JPEG
jpg_image = tif_image.convert('RGB')
# 在JPEG上打印文本
draw = ImageDraw.Draw(jpg_image)
font = ImageFont.truetype('cmr10.ttf', size=200) # 选择合适的字体和大小
text_position = (2400, 200) # 文本位置,可以根据需要调整
draw.text(text_position, timestamp, fill=(255, 255, 255), font=font) # 白色文本
# 使用GDAL打开TIF文件作为参考栅格
reference_ds = gdal.Open(tiffile)
# 将Shapefile边界绘制到图像上
draw_shapefile_on_image(shpfile, jpg_image, reference_ds)
# 保存JPEG
jpg_image.save(outpath, 'JPEG')
print(f"图像已保存为 {outpath}")
if __name__ == '__main__':
# 文件路径
tiffile = "/home/ubuntu/code/h8/2024-10-20/06-00/2024_10_20_06_00_clip.tif"
shpfile = "/home/ubuntu/code/himawari8/shp/边界线_省级_九段线.shp"
outpath = "/home/ubuntu/code/h8/2024-10-20/06-00/2024_10_20_06_00_clip.jpg"
process_image(tiffile, shpfile, outpath)
实现流程
读取 TIFF 栅格数据:通过
Pillow
打开 TIFF 图像并转换为 RGB 格式,以便可以进一步操作。读取 Shapefile 矢量数据:使用
OGR
读取 Shapefile 文件,提取线矢量数据的几何形状。将矢量边界绘制到栅格图像上:通过
ImageDraw
对象在栅格图像上绘制线条。添加时间戳信息:根据 TIFF 文件名提取时间信息,并将其绘制到图像上。
保存为 JPEG:将叠加结果保存为 JPEG 格式。
北京时间8点
视频如下:
为什么今天的视频有早上的6点到九点半,原因是我的服务器的存储空间满了,我又没写自动清除之前不要的数据的功能,导致9点半之后的数据没有自动下载。
下一步计划是
1自动清除不要的数据。
2将视频自动上传到b站、抖音。
3将图片进行24小时实时直播。
今天先写到这里了。
往期文章回顾