Skip to content

Instantly share code, notes, and snippets.

@godber
Last active March 30, 2019 01:13
Show Gist options
  • Save godber/8dfc63af0a0c14277065 to your computer and use it in GitHub Desktop.
Save godber/8dfc63af0a0c14277065 to your computer and use it in GitHub Desktop.
RaspberryPi Image Acquire Encode and transmit as SSTV
#!/usr/bin/env python
"""
This script
* acquires Image
* overlays callsign on Image
* converts image to wav
* plays wav
"""
import io
import time
import subprocess
from os import remove
import picamera
from PIL import Image, ImageFont, ImageDraw
from pysstv.color import MartinM1
def quick_timestamp():
return time.time()
def xmit(image):
print "Encoding image to wav file: %s" % quick_timestamp()
sstv = MartinM1(image, 48000, 16)
print "Writing wav file: %s" % quick_timestamp()
sstv.write_wav("out.wav")
print "Playing wav file: %s" % quick_timestamp()
wav_filename = "out.wav"
subprocess.check_call(["aplay", wav_filename])
remove(wav_filename)
def main():
timestamp = quick_timestamp()
image_name = "images/image-%s.jpg" % timestamp
img = take_image(image_name)
img = overlay_callsign(img.rotate(180), "W7ASU", timestamp)
img.save(image_name)
xmit(img)
print "imgxmit complete %s" % quick_timestamp()
print "runtime: %2.5s (s)" % str(time.time() - timestamp)
def overlay_callsign(image, callsign, timestamp):
"""Given a PIL Image Object will overlay the club callsign on the image
and return the modified image object
"""
print "Overlaying callsign: %s" % quick_timestamp()
font = ImageFont.truetype(
"/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf",
24
)
draw = ImageDraw.Draw(image)
text = "%s : %s" % (callsign, timestamp)
draw.text((20, 220), text, (255, 255, 255), font=font)
return image
def take_image(image_name):
"""Takes image and returns PIL Image object"""
print "Taking image %s" % image_name
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (320, 256)
camera.start_preview()
time.sleep(2)
camera.shutter_speed = camera.exposure_speed
camera.exposure_mode = 'off'
g = camera.awb_gains
camera.awb_mode = 'off'
camera.awb_gains = g
camera.capture(stream, format='jpeg')
# "Rewind" the stream to the beginning so we can read its content
stream.seek(0)
return Image.open(stream)
if __name__ == '__main__':
main()
Taking image images/image-1443996915.62.jpg
Overlaying callsign: 1443996919.12
Encoding image to wav file: 1443996919.25
Writing wav file: 1443996919.25
Playing wav file: 1443996976.77
Playing WAVE 'out.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
imgxmit complete 1443997092.12
runtime: 176.4 (s)
diff --git a/setup.py b/setup.py
index 6e51294..fe4694e 100644
--- a/setup.py
+++ b/setup.py
@@ -11,7 +11,7 @@ setup(
url='https://github.com/dnet/pySSTV',
packages=['pysstv', 'pysstv.tests', 'pysstv.examples'],
keywords='HAM SSTV slow-scan television Scottie Martin Robot',
- install_requires = ['PIL',],
+ install_requires = ['pillow',],
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',

This is what I did to get the attached script, imgxmit.py, to work on raspbian

Make sure raspberry pi camera is enabled:

raspi_config

Installed system level dependencies

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install python-virtualenv virtualenvwrapper
sudo apt-get install libfreetype6-dev
sudo apt-get install python-dev python-setuptools libtiff5-dev libjpeg8-dev zlib1g-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk
sudo apt-get install vim git
git clone https://github.com/dnet/pySSTV.git
mkvirtualenv pysstv
cd pysstv
pip install wheel
pip install -U pip
pip install -r requirements.txt
# edit setup.py as shown in the attached patch (or apply patch)
pip install -e .
mkdir images
./imgxmit.py
picamera==1.10
Pillow==3.0.0
@godber
Copy link
Author

godber commented Mar 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment