-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_movie.py
89 lines (72 loc) · 3.24 KB
/
make_movie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# adapted by [email protected]
import glob
import logging
import os
import random
import numpy
import string
from astropy.io import fits
from astropy.time import Time
from multiprocessing import Pool
from PIL import Image,ImageDraw,ImageFont
fontPath = '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf'
sans15 = ImageFont.truetype ( fontPath, 32 )
sans24 = ImageFont.truetype ( fontPath, 42 )
def generate_temp(k=16):
tmpfits = 'temp_'+''.join(random.choices(string.ascii_uppercase + string.digits, k=k))+'.fits'
return tmpfits
def make_png(ff,i):
tmpfits = generate_temp()
logging.info(' | File '+str(i)+' | Input image '+ff)
logging.info(' | File '+str(i)+' | Temp image '+tmpfits)
os.system('mShrink '+ff+' '+tmpfits+' 2')
input_hdu = fits.open(ff)[0]
hdr = input_hdu.header
map_date = hdr.get('DATE-OBS')
ctrfreq = hdr["CRVAL3"]
bmaj = hdr["BMAJ"]
bmin = hdr["BMIN"]
t_mjd = Time(map_date, format='isot', scale='ut1').mjd
tt = map_date+' | '+str(t_mjd)
# pp = str(i).zfill(4)+'_'+ff.replace('.fits','.png')
pp = 'pic_'+str(i).zfill(4)+'.png'
# syscall = 'mViewer -ct 0 -gray '+ff+' -0.0004 0.0008 -out '+pp
logging.info(' | File '+str(i)+' | PNG '+pp)
syscall = 'mViewer -ct 0 -gray '+tmpfits+' -0.02552 0.1316 -out '+pp
os.system(syscall)
logging.info(' | File '+str(i)+' | Time '+tt)
img = Image.open(pp)
# crop to centre
CROPSIZE = 1750
img = img.crop((img.size[0]//2 - CROPSIZE//2, img.size[1]//2 - CROPSIZE//2, img.size[0]//2 + CROPSIZE//2, img.size[1]//2 + CROPSIZE//2))
# ffmpeg codec does not like odd numbers much like certain friends... trim the last row or column if needed
img = img.crop((0,0,img.size[0] - (img.size[0] % 2), img.size[1] - (img.size[1] % 2)))
xx,yy = img.size
draw = ImageDraw.Draw(img)
TITLE = f"MeerKAT UHF Band [{ctrfreq*1e-6:.1f} MHz]"
draw.text((0.03*xx,0.03*yy),'Karoo skies - Moon',fill=('white'),font=sans24)
draw.text((0.03*xx,0.81*yy),TITLE,fill=('white'),font=sans15)
draw.text((0.03*xx,0.84*yy),'Frame : '+str(i).zfill(len(str(nframes)))+' / '+str(nframes),fill=('white'),font=sans15)
draw.text((0.03*xx,0.87*yy),'Time : '+tt,fill=('white'),font=sans15)
draw.text((0.03*xx,0.90*yy),'Image : '+ff,fill=('white'),font=sans15)
draw.text((0.03*xx,0.93*yy),f'Resolution : {bmaj*3600.:.1f}" x {bmin*3600.:.1f}"',fill=('white'),font=sans15)
draw.text((0.03*xx,0.96*yy),f'Stokes : I',fill=('white'),font=sans15)
img.save(pp)
os.system('rm '+tmpfits)
logging.info(' | File '+str(i)+' | Done')
if __name__ == '__main__':
logfile = 'make_movie.log'
logging.basicConfig(filename=logfile, level=logging.DEBUG, format='%(asctime)s | %(message)s', datefmt='%d/%m/%Y %H:%M:%S ')
fitslist = sorted(glob.glob('*-t*-image.fits'))
ids = numpy.arange(0,len(fitslist))
nframes = len(fitslist)
j = 8
pool = Pool(processes=j)
# pool.map(make_png,fitslist)
pool.starmap(make_png,zip(fitslist,ids))
frame = '2340x2340'
fps = 10
opmovie = fitslist[0].split('-t')[0]+'.mp4'
os.system('ffmpeg -r '+str(fps)+' -f image2 -s '+frame+' -i pic_%0004d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p '+opmovie)