如何在JupyterLab中控制pywwt并实现程序化截图?
PyWWT相机控制与截图实现方案
一、定位目标天区
基于坐标定位
使用center_on_coordinates函数直接指定目标天区的赤经、赤纬,搭配视场角参数完成定位:
# 示例:定位到赤经10h00m00s、赤纬45°00'00",视场角2度 wwt.center_on_coordinates("10h00m00s", "45°00'00'", fov=2)
参数说明:
- 赤经支持小时分秒(如
"10h00m00s")或十进制度数格式 - 赤纬支持度分秒(如
"45°00'00'")或十进制度数格式 fov为视场角,单位是度,数值越小放大倍数越高
基于WCS定位(更精准)
如果你的星空图片带有WCS(世界坐标系)信息,直接用center_on_wcs加载WCS对象,可自动匹配天区:
from astropy.wcs import WCS from astropy.io import fits # 从FITS格式的星空图片中提取WCS with fits.open("your_star_image.fits") as hdul: target_wcs = WCS(hdul[0].header) # 定位到目标天区 wwt.center_on_wcs(target_wcs)
二、开启星座标记
通过constellations属性控制星座显示,还可自定义样式:
# 开启星座线条与名称显示 wwt.constellations = True # 设置星座线条颜色(支持十六进制或RGB值) wwt.constellation_color = "#ffff00" # 开启星座边界填充(可选) wwt.constellation_boundaries = True
三、获取截图/当前图像
直接保存到文件
使用save_screenshot方法一键保存当前视图到本地文件:
# 保存为PNG格式图片 wwt.save_screenshot("constellation_view.png")
获取PIL图像对象(用于后续处理)
使用render方法获取当前帧的PIL图像对象,可用于进一步分析或修改:
from PIL import Image # 获取图像对象 current_img = wwt.render() # 直接显示图像 current_img.show() # 保存到文件 current_img.save("processed_view.png")
完整示例代码
from pywwt.jupyter import WWTJupyterWidget from astropy.wcs import WCS from astropy.io import fits # 初始化JupyterLab中的WWT组件 wwt = WWTJupyterWidget() display(wwt) # 1. 从星空图片提取WCS并定位天区 with fits.open("star_field.fits") as hdul: target_wcs = WCS(hdul[0].header) wwt.center_on_wcs(target_wcs) # 2. 配置星座显示 wwt.constellations = True wwt.constellation_color = "#ffff00" wwt.constellation_labels = True # 显示星座名称 # 3. 保存截图 wwt.save_screenshot("target_constellation.png")
内容的提问来源于stack exchange,提问作者侯凌群
相关产品推荐
相关产品推荐

