This project is a Python-based Moon phase image generator that leverages NASA's SPICE toolkit (via SpiceyPy) to accurately compute the Moon’s ephemeris data, including its phase, illumination, and position relative to both the Earth and the Sun. The generated images include a white Moon with a realistic shadow overlay representing the terminator, all composed together to form the final Moon phase image.
- Accurate Ephemeris Calculations: Uses SPICE kernels to compute the Moon’s phase angle, illuminated fraction, distance, and more.
- Realistic Illumination: Computes a pixel-by-pixel shadow overlay based on the Moon’s local geometry.
- Image Compositing: Combines a clean white Moon image with a shadow layer for a visually appealing final result.
- Enhanced Logging: Clear, human-friendly logging messages with emojis to guide you through each step of the process.
Before running the script, make sure you have the following installed:
- Python 3
- Required Python Packages:
numpymatplotlibspiceypyastropyPillow
You can install the required packages using pip:
pip install numpy matplotlib spiceypy astropy pillowThis project requires a SPICE meta-kernel file named moon.tm in the same directory as the script. This meta-kernel should reference the necessary SPICE kernels (e.g., leapseconds, planetary ephemerides) to allow the computations.
-
Prepare the Environment:
- Ensure you have Python 3 installed.
- Install the required Python packages.
- Place the
moon.tmfile in the project directory.
-
Customize (Optional):
- Observation Time: Modify the
time_strvariable in themain()function to set your desired UTC observation time. - Observer Location: Change the observer location (e.g.,
new_zealandornl_location) in the script if needed. - Resolution: Adjust the
resolutionvariable to change the image output resolution.
- Observation Time: Modify the
-
Run the Script:
Make the script executable and run it:
chmod +x your_script.py ./your_script.py
Or run it directly with Python:
python your_script.py
-
Output:
The script will create anoutputdirectory containing:moon.png: A white Moon image (a white circle with a black border) on a transparent background.shadow.png: A shadow overlay image computed from the Moon’s illumination.final.png: The final composite image with the Moon and its terminator (shadow overlay).
The script utilizes Python's logging module with a custom, emoji-enhanced format to provide clear and fun output messages as it processes the data. This makes it easy to follow along with each step—from loading the SPICE kernels to generating and compositing the images.
-
Observation Time:
Change thetime_strin themain()function.
Example:time_str = '2026-05-31T18:30:00'
-
Observer Location:
Modify thelocationvariable in themain()function. The example uses New Zealand’s coordinates.
Example:location = new_zealand
-
Image Resolution & Moon Radius:
Adjust theresolutionandR(Moon radius in arbitrary units) variables as desired for higher quality or differently scaled images.
This project is open source. Feel free to modify, extend, and distribute it as needed.
- NASA SPICE Toolkit: For providing precise planetary and spacecraft ephemeris data.
- SpiceyPy: The Python wrapper for the SPICE toolkit.
- Astropy: For time and coordinate transformations.
- Matplotlib & Pillow: For image creation and processing.
Enjoy generating stunning Moon phase images and may your projects always shine as bright as the Moon! 🌕🚀