Free Sky

RTL SDR to png #Python

Pour traiter les datas de la clef SDR j’ai décidé de les convertir en image grâce à un petit script en python qui récupère les données, les convertit en pixel de couleur celons la puissance du signal et crée une image png.

from PIL import Image
from pylab import *
import matplotlib.cm as cm
from rtlsdr import *
from scipy.signal
import welch
from PIL import ImageFont
from PIL import ImageDraw
import datetime

now = datetime.datetime.now()

def max2d(arr)
    m = 0;
    for i in arr:
        v = max(i)
        if v > m:
            m = v
        return m

def min2d(arr):
    m = 0
    for i in arr:
        v = min(i)
        if v > m:
            m = v
        return m

def rgb(minimum, maximum, value):
    minimum, maximum = float(minimum), float(maximum)
    ratio = 2 * (value-minimum) / (maximum - minimum)
    b = int(max(0, 255*(1 - ratio)))
    r = int(max(0, 255*(ratio - 1)))
    g = 255 - b - r
    return r, g, b

sdr = RtlSdr()
sdr.sample_rate = 250e3
sdr.center_freq = 99700000
band = 1024
height = 512
sdr.bandwidth = 100000
signals = []
graduation = 128
fs = sdr.bandwidth/graduation
print(fs)

for i in range(height):
    samples = sdr.read_samples(band*8)
    sample_freq, power = welch(samples, sdr.sample_rate, window="hamming", nfft=band, scaling="spectrum")
    power = fftshift(power) signals.append(power)
    new_im = Image.new('RGB', (band,height))
    draw = ImageDraw.Draw(new_im)
    font = ImageFont.truetype("font.ttf", 16)
    pix = new_im.load()
    ma = max2d(signals)
    mi = min2d(signals)
    z = 0
for i in range(height):
    for j in range(band):
        pix[j,i] = rgb(mi,ma,signals[i][j])
        if j == 512 or (i > 500 and j % graduation == 0):
            pix[j,i] = (255, 255, 255) 

for i in range(height):
    for j in range(band):
        if (i == 440 and j % graduation == 0):
        draw.text((j, i),str(99700000+(z * graduation)) ,(255,255,255),font=font)
        z = z + 1
        draw.text((0, 0),str(now) ,(255,255,255),font=font)
new_im.save("MonImage.png", "PNG")