# 90行代码让微信地球转起来，太酷了！（python实现）

88 篇文章 95 订阅

## 4.基本原理

https://blog.csdn.net/xiaorang/article/details/106692489

## 5.GIF压缩

https://www.iloveimg.com/zh-cn/compress-image/compress-gif

## 6.完整代码

from PIL import Image, ImageDraw
import math
import numpy as np
import imageio

def calcSphereXY2XYZ(px, py, maxHeight, longOffset):
v0x= np.array(px)
v0y= np.array(py)
v03= np.subtract(v0x, maxHeight)
v04= np.subtract(v0y, maxHeight)
v1x= np.true_divide(v03, maxHeight)
v1y= np.true_divide(v04, maxHeight)
# print(max(v1x), min(v1x))
v07= np.power(v1x,2)
v08= np.power(v1y,2)
v0a= np.subtract(1,v09)
v1z= np.power(v0a,1/2)                                  # z
# print('z:', max(v1z), min(v1z))
v1lat= np.multiply(v1y, math.pi/2)                      # lat
v0lon= np.arctan2(v1z, -v1x)
v2lon= np.fmod(v1lon, math.pi*2)                       # long
return v2lon, v1lat

def calcShpereLatLong2XY(vlon, vlat, width, height):
v3x0=np.multiply(vlon, width/2/math.pi)
v3y0=np.multiply(vlat, height/math.pi)
v3x2=v3x0.astype(np.integer)
v3y2=v3y1.astype(np.integer)
return v3x2, v3y2

def getPic(a):
# imgBack= Image.open('地球3.jpg')
imgBack= Image.open('世界地球日地图_8K_2.jpg')
imgCloud= Image.open('世界地球云地图_8K.jpg')
width= imgBack.size[0]
height= imgBack.size[1]
imgBack= imgBack.convert('RGBA')
arrayBack= np.array(imgBack)
arrayCloud= np.array(imgCloud)
circleSize= 508
img2= Image.new('RGBA', (circleSize,circleSize))
img= Image.new('RGBA', (circleSize,circleSize), 'black')
w= img.size[0]
h= img.size[1]
pxList=[]
pyList=[]
for i in range(w):
for j in range(h):
r= math.sqrt((i-w/2)**2+(j-h/2)**2)
if r<circleSize/2:
pxList.append(i)
pyList.append(j)

nplon, nplat= calcSphereXY2XYZ(pxList, pyList, h/2, a)
nplon2, nplat2= calcSphereXY2XYZ(pxList, pyList, h/2, a/2)
# nplon, nplat= rotSphere(nplon, nplat, )
npx, npy= calcShpereLatLong2XY(nplon, nplat, width-1, height)
npx2, npy2= calcShpereLatLong2XY(nplon2, nplat2, width-1, height)
color= arrayBack[npy, npx]
color2= arrayCloud[npy2, npx2]
for i in range(len(pxList)):
x= pxList[i]
y= pyList[i]
cc=color[i]
# print(cc)
cc= tuple(cc)
img.putpixel((x,y), cc)
c2= color2[i]
c0= int(c2[0]*1.6)
if c0>255:
c0=255
c_alpha= int(c2[0]*0.9)
c2= (c0,c0,c0,c_alpha)
img2.putpixel((x,y), c2)
r,g,b,a= img2.split()
return img

if __name__=='__main__':
frames=[]
img1= Image.new('RGB', (750,1334))
img2= Image.open(str1)
for i in range(0, 720, 12):
a= -i*math.pi/ 180
img= getPic(a)
img1.paste(img,(122,424))
r,g,b,alpha=img2.split()
str1= 'temp%03d.png'%i
print(str1)
img1.save(str1)
frames.append(im)
# img.show()
imageio.mimsave('earth.gif', frames, 'GIF', duration=0.20)



04-03

02-01 2236
09-08 2万+
07-27 3884
02-18 1621
01-13 5189
12-14 4717
01-19 2万+

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助