Última atividade 1 month ago

LRIT Full Disk Image converter to JPEG

racerxdl's Avatar Lucas Teske revisou este gist 9 years ago. Ir para a revisão

1 file changed, 15 insertions, 2 deletions

fulldisk2jpg.py

@@ -40,12 +40,14 @@ for i in lritfiles:
40 40 curColumns = 0
41 41 curImageId = 0
42 42 pixelAspect = 1
43 + coff = 0
43 44 for header in headers:
44 45 if header["type"] == 1:
45 46 curLines += header["lines"]
46 47 curColumns = header["columns"]
47 48 elif header["type"] == 2:
48 49 pixelAspect = float(header["cfac"]) / header["lfac"]
50 + coff = header["coff"]
49 51 elif header["type"] == 128: # Segment Identification Header
50 52 imageId = header["imageid"]
51 53 segmentId = header["sequence"]
@@ -58,10 +60,21 @@ for i in lritfiles:
58 60 grouped[imageId]["lines"] += curLines
59 61 grouped[imageId]["columns"] = curColumns
60 62 grouped[imageId]["pixelAspect"] = pixelAspect
63 + grouped[imageId]["startColumn"] = coff
61 64
62 65 for i in grouped:
63 66 print "Processing image %s" %i
64 67 d = grouped[i]
68 +
69 + # Crop Left
70 + sc = d["startColumn"]
71 + hw = min(d["columns"] - sc, sc)
72 + cl = d["startColumn"] - hw
73 +
74 + # Crop Right
75 + cf = cl + 2 * hw
76 +
77 + print " Crop Marks: %s %s" % (cl, cf)
65 78 img = Image.new("L", (d["columns"], d["lines"]))
66 79 pixels = np.array([], dtype=np.uint8)
67 80 for z in d["segments"]:
@@ -71,6 +84,6 @@ for i in grouped:
71 84 img.putdata(pixels)
72 85 newheight = img.size[1] * d["pixelAspect"]
73 86 newheight = int(math.ceil(newheight))
74 -
87 + img = img.crop((cl, 0, cf, img.size[1]))
75 88 img = img.resize((img.size[0], newheight))
76 - img.save("%s.jpg" % i, "JPEG")
89 + img.save("%s-crop.jpg" % i, "JPEG")

racerxdl's Avatar Lucas Teske revisou este gist 9 years ago. Ir para a revisão

1 file changed, 76 insertions

fulldisk2jpg.py(arquivo criado)

@@ -0,0 +1,76 @@
1 + #!/usr/bin/env python
2 +
3 + '''
4 + Needs Pillow, xrit and numpy
5 + pip install xrit Pillow
6 +
7 + Numpy is better to be installed using distro packages.
8 + '''
9 +
10 + import os, math
11 + import numpy as np
12 + from PIL import Image
13 + from xrit import packetmanager
14 +
15 +
16 + folder = "."
17 +
18 + lritfiles = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f)) and ".lrit" in f]
19 +
20 + grouped = {}
21 +
22 + def headersFromFile(filename):
23 + f = open(filename, "rb")
24 + try:
25 + k = packetmanager.readHeader(f)
26 + type, filetypecode, headerlength, datalength = k
27 + except Exception,e:
28 + print(" Header 0 is corrupted for file %s" %filename)
29 + return None
30 + f.seek(0, 0)
31 + data = f.read(headerlength)
32 + headers = packetmanager.getHeaderData(data)
33 + f.close()
34 + return headers
35 +
36 +
37 + for i in lritfiles:
38 + headers = headersFromFile(i)
39 + curLines = 0
40 + curColumns = 0
41 + curImageId = 0
42 + pixelAspect = 1
43 + for header in headers:
44 + if header["type"] == 1:
45 + curLines += header["lines"]
46 + curColumns = header["columns"]
47 + elif header["type"] == 2:
48 + pixelAspect = float(header["cfac"]) / header["lfac"]
49 + elif header["type"] == 128: # Segment Identification Header
50 + imageId = header["imageid"]
51 + segmentId = header["sequence"]
52 + curImageId = imageId
53 + if not imageId in grouped:
54 + grouped[imageId] = { "segments": {}, "lines": 0 }
55 +
56 + grouped[imageId]["segments"][segmentId] = i
57 +
58 + grouped[imageId]["lines"] += curLines
59 + grouped[imageId]["columns"] = curColumns
60 + grouped[imageId]["pixelAspect"] = pixelAspect
61 +
62 + for i in grouped:
63 + print "Processing image %s" %i
64 + d = grouped[i]
65 + img = Image.new("L", (d["columns"], d["lines"]))
66 + pixels = np.array([], dtype=np.uint8)
67 + for z in d["segments"]:
68 + data = packetmanager.loadData(d["segments"][z])
69 + data = np.fromstring(data, dtype=np.uint8)
70 + pixels = np.concatenate((pixels, data))
71 + img.putdata(pixels)
72 + newheight = img.size[1] * d["pixelAspect"]
73 + newheight = int(math.ceil(newheight))
74 +
75 + img = img.resize((img.size[0], newheight))
76 + img.save("%s.jpg" % i, "JPEG")
Próximo Anterior