我们可以用 Python 和 matplotlib
、numpy
以及 Pillow
(可选)库来绘制一幅梦幻星空。这个“梦幻”风格包括:
- 深邃的背景色
- 随机分布的星点,亮度有高有低
- 柔光发散的星星
- 流星或光晕效果(可选)
✅ 示例效果预览(文字描述):
- 背景是深蓝/紫色渐变
- 星星大小不一、颜色有偏白、浅蓝、淡紫
- 流星拖尾如丝
- 可带轻微模糊和光晕模拟浪漫感
🧑💻 Python 实现代码如下:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.colors import LinearSegmentedColormap
# 设置画布大小
fig, ax = plt.subplots(figsize=(10, 6))
ax.set_facecolor('black')
plt.axis('off') # 去除坐标轴
# 自定义梦幻星空背景渐变(黑到深蓝到紫)
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
custom_cmap = LinearSegmentedColormap.from_list(
"dream_sky",
["#000000", "#0d1b2a", "#1b263b", "#415a77", "#6b39b4"],
N=256,
)
plt.imshow(gradient, aspect='auto', cmap=custom_cmap, extent=[0, 1, 0, 1])
# 生成星星
num_stars = 800
x = np.random.rand(num_stars)
y = np.random.rand(num_stars)
size = np.random.rand(num_stars) * 15 # 星星大小
color_choices = ['white', '#cceeff', '#c2b9ff', '#ffd6ff']
colors = np.random.choice(color_choices, num_stars)
for i in range(num_stars):
ax.add_patch(Circle((x[i], y[i]), size[i]/500, color=colors[i], alpha=np.clip(size[i]/15, 0.3, 1)))
# 添加流星(可选)
def draw_meteor(start, length=0.1, angle_deg=45, color='white'):
angle = np.radians(angle_deg)
x0, y0 = start
dx = np.cos(angle) * length
dy = np.sin(angle) * length
ax.plot([x0, x0 + dx], [y0, y0 + dy], lw=1.5, color=color, alpha=0.6)
for _ in range(3):
draw_meteor((np.random.rand(), np.random.rand()), length=0.15, angle_deg=np.random.uniform(20, 70))
plt.tight_layout()
plt.show()
📦 依赖库安装
如果你还没有安装依赖,请运行:
pip install matplotlib numpy
🧚 拓展玩法建议
- 使用
PIL
加入星云纹理或月亮图像; - 使用
noise
或perlin-noise
生成星云背景; - 用
pygame
制作动态星空动画; - 使用
blur
模拟柔光星星或银河。
发表回复