From c994e2bb2d8a100ebfa8bebab546f2c0273e793e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Veres-Szentkir=C3=A1lyi?= Date: Thu, 23 May 2013 16:11:53 +0200 Subject: [PATCH] moved implementation classes to separate modules --- color.py | 65 +++++++++++++++++++++++++++++++++ grayscale.py | 42 +++++++++++++++++++++ sstv.py | 101 +-------------------------------------------------- 3 files changed, 108 insertions(+), 100 deletions(-) create mode 100644 color.py create mode 100644 grayscale.py diff --git a/color.py b/color.py new file mode 100644 index 0000000..59578d3 --- /dev/null +++ b/color.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python + +from __future__ import division +from sstv import FREQ_BLACK, FREQ_RANGE, FREQ_SYNC +from grayscale import GrayscaleSSTV + +class ColorSSTV(GrayscaleSSTV): + RED, GREEN, BLUE = range(3) + + def encode_line(self, line): + cs = self.COLOR_SEQ + msec_pixel = self.SCAN / self.WIDTH + image = self.image + for index in cs: + for item in self.before_channel(index): + yield item + for col in xrange(self.WIDTH): + pixel = image.getpixel((col, line)) + value = pixel[index] + freq_pixel = FREQ_BLACK + FREQ_RANGE * value / 255 + yield freq_pixel, msec_pixel + + def before_channel(self, index): + return [] + + +class MartinM1(ColorSSTV): + COLOR_SEQ = (ColorSSTV.GREEN, ColorSSTV.BLUE, ColorSSTV.RED) + VIS_CODE = 0x2c + WIDTH = 320 + HEIGHT = 256 + SYNC = 4.862 + SCAN = 146.432 + INTER_CH_GAP = 0.572 + + def before_channel(self, index): + if index != ColorSSTV.GREEN: + yield FREQ_BLACK, self.INTER_CH_GAP + + +class MartinM2(MartinM1): + VIS_CODE = 0x28 + WIDTH = 160 + SCAN = 73.216 + + +class ScottieS1(MartinM1): + VIS_CODE = 0x3c + SYNC = 9 + SCAN = 138.24 + INTER_CH_GAP = 1.5 + + def horizontal_sync(self): + return [] + + def before_channel(self, index): + if index != ColorSSTV.RED: + yield FREQ_SYNC, self.SYNC + yield FREQ_BLACK, self.INTER_CH_GAP + + +class ScottieS2(ScottieS1): + VIS_CODE = 0x38 + SCAN = 88.064 + WIDTH = 160 diff --git a/grayscale.py b/grayscale.py new file mode 100644 index 0000000..3ca0aa4 --- /dev/null +++ b/grayscale.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +from __future__ import division +from sstv import FREQ_BLACK, FREQ_RANGE, FREQ_SYNC, SSTV + +class GrayscaleSSTV(SSTV): + def gen_freq_bits(self): + for item in SSTV.gen_freq_bits(self): + yield item + for line in xrange(self.HEIGHT): + for item in self.horizontal_sync(): + yield item + for item in self.encode_line(line): + yield item + + def horizontal_sync(self): + yield FREQ_SYNC, self.SYNC + + def encode_line(self, line): + msec_pixel = self.SCAN / self.WIDTH + image = self.image + for col in xrange(self.WIDTH): + pixel = image.getpixel((col, line)) + value = sum(pixel) / len(pixel) + freq_pixel = FREQ_BLACK + FREQ_RANGE * value / 255 + yield freq_pixel, msec_pixel + + +class Robot8BW(GrayscaleSSTV): + VIS_CODE = 0x02 + WIDTH = 160 + HEIGHT = 120 + SYNC = 10 + SCAN = 56 + + +class Robot24BW(GrayscaleSSTV): + VIS_CODE = 0x0A + WIDTH = 320 + HEIGHT = 240 + SYNC = 12 + SCAN = 93 diff --git a/sstv.py b/sstv.py index d6a96b1..1d3c491 100644 --- a/sstv.py +++ b/sstv.py @@ -84,108 +84,9 @@ class SSTV(object): yield FREQ_SYNC, MSEC_VIS_BIT # stop bit -class GrayscaleSSTV(SSTV): - def gen_freq_bits(self): - for item in SSTV.gen_freq_bits(self): - yield item - for line in xrange(self.HEIGHT): - for item in self.horizontal_sync(): - yield item - for item in self.encode_line(line): - yield item - - def horizontal_sync(self): - yield FREQ_SYNC, self.SYNC - - def encode_line(self, line): - msec_pixel = self.SCAN / self.WIDTH - image = self.image - for col in xrange(self.WIDTH): - pixel = image.getpixel((col, line)) - value = sum(pixel) / len(pixel) - freq_pixel = FREQ_BLACK + FREQ_RANGE * value / 255 - yield freq_pixel, msec_pixel - - -class ColorSSTV(GrayscaleSSTV): - RED, GREEN, BLUE = range(3) - - def encode_line(self, line): - cs = self.COLOR_SEQ - msec_pixel = self.SCAN / self.WIDTH - image = self.image - for index in cs: - for item in self.before_channel(index): - yield item - for col in xrange(self.WIDTH): - pixel = image.getpixel((col, line)) - value = pixel[index] - freq_pixel = FREQ_BLACK + FREQ_RANGE * value / 255 - yield freq_pixel, msec_pixel - - def before_channel(self, index): - return [] - - -class Robot8BW(GrayscaleSSTV): - VIS_CODE = 0x02 - WIDTH = 160 - HEIGHT = 120 - SYNC = 10 - SCAN = 56 - - -class Robot24BW(GrayscaleSSTV): - VIS_CODE = 0x0A - WIDTH = 320 - HEIGHT = 240 - SYNC = 12 - SCAN = 93 - - -class MartinM1(ColorSSTV): - COLOR_SEQ = (ColorSSTV.GREEN, ColorSSTV.BLUE, ColorSSTV.RED) - VIS_CODE = 0x2c - WIDTH = 320 - HEIGHT = 256 - SYNC = 4.862 - SCAN = 146.432 - INTER_CH_GAP = 0.572 - - def before_channel(self, index): - if index != ColorSSTV.GREEN: - yield FREQ_BLACK, self.INTER_CH_GAP - - -class MartinM2(MartinM1): - VIS_CODE = 0x28 - WIDTH = 160 - SCAN = 73.216 - - -class ScottieS1(MartinM1): - VIS_CODE = 0x3c - SYNC = 9 - SCAN = 138.24 - INTER_CH_GAP = 1.5 - - def horizontal_sync(self): - return [] - - def before_channel(self, index): - if index != ColorSSTV.RED: - yield FREQ_SYNC, self.SYNC - yield FREQ_BLACK, self.INTER_CH_GAP - - -class ScottieS2(ScottieS1): - VIS_CODE = 0x38 - SCAN = 88.064 - WIDTH = 160 - - if __name__ == '__main__': from PIL import Image + from grayscale import Robot8BW image = Image.open('160x120bw.png') s = Robot8BW(image, 48000, 16) s.write_wav('test.wav')