pySSTV/pysstv/examples/gimp-plugin.py

116 wiersze
3.4 KiB
Python
Czysty Zwykły widok Historia

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# copy to ~/.gimp-2.8/plug-ins/
# dependencies: GIMP 2.8, python-imaging-tk
2013-11-03 21:29:21 +00:00
from gimpfu import register, main, pdb, PF_BOOL, PF_STRING, PF_RADIO
from tempfile import mkstemp
from PIL import Image, ImageTk
from Tkinter import Tk, Label, Button, Checkbutton, IntVar, Frame, LEFT
2013-11-03 21:29:21 +00:00
from pysstv import __main__ as pysstv_main
from pysstv.examples.pyaudio_sstv import PyAudioSSTV
2013-11-04 15:37:43 +00:00
from pysstv.sstv import SSTV
from itertools import repeat
2013-11-04 14:08:24 +00:00
from threading import Thread
import os
2013-11-03 21:29:21 +00:00
MODULE_MAP = pysstv_main.build_module_map()
2013-11-04 14:08:24 +00:00
class AudioThread(Thread):
2013-11-04 14:29:54 +00:00
def __init__(self, sstv, parent):
2013-11-04 14:08:24 +00:00
Thread.__init__(self)
self.pas = PyAudioSSTV(sstv)
2013-11-04 14:29:54 +00:00
self.parent = parent
2013-11-04 14:08:24 +00:00
def run(self):
self.pas.execute()
2013-11-04 14:29:54 +00:00
self.parent.audio_thread_ended()
2013-11-04 14:08:24 +00:00
def stop(self):
self.pas.sampler = []
2013-11-04 14:29:54 +00:00
self.pas = None
2013-11-04 14:08:24 +00:00
2013-11-04 15:37:43 +00:00
class Sine1750(SSTV):
encode_line = None
def gen_freq_bits(self):
return repeat((1750, 1000))
class Transmitter(object):
def __init__(self, sstv, root):
def encode_line_hooked(line):
print line # TODO show progress on GUI
return self.original_encode_line(line)
self.sstv = sstv
self.original_encode_line = sstv.encode_line
sstv.encode_line = encode_line_hooked
self.root = root
2013-11-04 14:29:54 +00:00
self.tx_enabled = IntVar()
2013-11-04 14:08:24 +00:00
self.audio_thread = None
2013-11-04 14:29:54 +00:00
def start_stop_tx(self):
if self.tx_enabled.get():
self.audio_thread = AudioThread(self.sstv, self)
self.audio_thread.start()
elif self.audio_thread is not None:
2013-11-04 14:08:24 +00:00
self.audio_thread.stop()
2013-11-04 14:29:54 +00:00
def audio_thread_ended(self):
self.tx_enabled.set(0)
def close(self):
self.root.destroy()
2013-11-04 14:08:24 +00:00
2013-11-03 21:29:21 +00:00
def transmit_current_image(image, drawable, mode, vox, fskid):
sstv = MODULE_MAP[mode]
handle, png_fn = mkstemp(suffix='.png', prefix='pysstv-gimp-')
os.fdopen(handle).close()
try:
pdb.gimp_file_save(image, drawable, png_fn, png_fn)
pil_img = Image.open(png_fn)
root = Tk()
s = sstv(pil_img, 44100, 16)
s.vox_enabled = vox
if fskid:
s.add_fskid_text(fskid)
tm = Transmitter(s, root)
2013-11-04 15:37:43 +00:00
tm1750 = Transmitter(Sine1750(None, 44100, 16), root)
tk_img = ImageTk.PhotoImage(pil_img)
img_widget = Label(root, image=tk_img)
img_widget.image = tk_img
img_widget.pack()
buttons = Frame(root)
2013-11-04 22:01:41 +00:00
for text, tram in (('TX', tm), ('1750 Hz', tm1750)):
Checkbutton(buttons, text=text, indicatoron=False, padx=5, pady=5,
variable=tram.tx_enabled, command=tram.start_stop_tx).pack(side=LEFT)
Button(buttons, text="Close", command=tm.close).pack(side=LEFT)
buttons.pack()
root.mainloop()
finally:
os.remove(png_fn)
register(
"pysstv_for_gimp",
"PySSTV for GIMP",
"Transmits the current image using PySSTV",
"Andras Veres-Szentkiralyi",
"Andras Veres-Szentkiralyi",
"November 2013",
"<Image>/PySSTV/Transmit...",
"*",
2013-11-03 21:29:21 +00:00
[
(PF_RADIO, "mode", "SSTV mode", "MartinM1",
tuple((n, n) for n in sorted(MODULE_MAP.iterkeys()))),
(PF_BOOL, "vox", "Include VOX tones", True),
(PF_STRING, "fskid", "FSK ID", ""),
],
[],
transmit_current_image
)
main()