Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add image file source #4

Merged
merged 1 commit into from Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
add image file source
  • Loading branch information
ckuethe committed Aug 27, 2015
commit 6f3356c9816402961e75082f0ab9aa5b5b0eb338
3 changes: 2 additions & 1 deletion grc/CMakeLists.txt
Expand Up @@ -18,5 +18,6 @@
# Boston, MA 02110-1301, USA.

install(FILES
paint_paint_bc.xml DESTINATION share/gnuradio/grc/blocks
paint_paint_bc.xml
paint_image_source.xml DESTINATION share/gnuradio/grc/blocks
)
63 changes: 63 additions & 0 deletions grc/paint_image_source.xml
@@ -0,0 +1,63 @@
<?xml version="1.0"?>
<block>
<name>Image Source</name>
<key>paint_image_source</key>
<category>Paint</category>
<import>import paint</import>
<make>paint.image_source($image_file, $image_flip, $image_invert, $autocontrast)</make>
<param>
<name>Image File</name>
<key>image_file</key>
<value></value>
<type>file_open</type>
</param>
<param>
<name>Flip image?</name>
<key>image_flip</key>
<value>False</value>
<type>enum</type>
<option>
<name>No</name>
<key>0</key>
</option>
<option>
<name>Yes</name>
<key>1</key>
</option>
</param>

<param>
<name>Invert brightness?</name>
<key>image_invert</key>
<value>False</value>
<type>enum</type>
<option>
<name>No</name>
<key>0</key>
</option>
<option>
<name>Yes</name>
<key>1</key>
</option>
</param>

<param>
<name>"Enhance" contrast?</name>
<key>autocontrast</key>
<value>False</value>
<type>enum</type>
<option>
<name>No</name>
<key>0</key>
</option>
<option>
<name>Yes</name>
<key>1</key>
</option>
</param>

<source>
<name>out</name>
<type>byte</type>
</source>
</block>
2 changes: 1 addition & 1 deletion python/CMakeLists.txt
Expand Up @@ -31,7 +31,7 @@ endif()
GR_PYTHON_INSTALL(
FILES
__init__.py
DESTINATION ${GR_PYTHON_DIR}/paint
image_source.py DESTINATION ${GR_PYTHON_DIR}/paint
)

########################################################################
Expand Down
1 change: 1 addition & 0 deletions python/__init__.py
Expand Up @@ -31,4 +31,5 @@
pass

# import any pure python here
from image_source import image_source
#
78 changes: 78 additions & 0 deletions python/image_source.py
@@ -0,0 +1,78 @@
#!/usr/bin/env python
# vim: tabstop=4:softtabstop=4:shiftwidth=4:noexpandtab:
# -*- coding: utf-8 -*-
#
# Copyright 2015 Chris Kuethe <chris.kuethe@gmail.com>
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

import numpy
from gnuradio import gr
from PIL import Image
from PIL import ImageOps
import pmt

class image_source(gr.sync_block):
"""
Given an image file readable by Python-Imaging, this block produces
monochrome lines suitable for input to spectrum_paint
"""
def __init__(self, image_file, image_flip=False, image_invert=False, autocontrast=False):
gr.sync_block.__init__(self,
name="image_source",
in_sig=None,
out_sig=[numpy.uint8])

im = Image.open(image_file)
im = ImageOps.grayscale(im)

if autocontrast:
# may or may not improve the look of the transmitted spectrum
im = ImageOps.autocontrast(im)

if image_invert:
# may or may not improve the look of the transmitted spectrum
im = ImageOps.invert(im)

if image_flip:
# set to true for waterfalls that scroll from the top
im = ImageOps.flip(im)

(self.image_width, self.image_height) = im.size
max_width = 2048.0
if self.image_width > max_width:
scaling = max_width / self.image_width
newsize = (int(self.image_width * scaling), int(self.image_height * scaling))
(self.image_width, self.image_height) = newsize
im = im.resize(newsize)
self.set_output_multiple(self.image_width)

self.image_data = list(im.getdata())
self.image_len = len(self.image_data)
print "paint.image_source: %d bytes, %dpx width" % (self.image_len, self.image_width)
self.line_num = 0

def work(self, input_items, output_items):
out = output_items[0]
self.add_item_tag(0, self.nitems_written(0), pmt.intern("image_width"), pmt.from_long(self.image_width))
self.add_item_tag(0, self.nitems_written(0), pmt.intern("line_num"), pmt.from_long(self.line_num))
out[:self.image_width] = self.image_data[self.image_width*self.line_num: self.image_width*(1+self.line_num)]

self.line_num += 1
if self.line_num >= self.image_height:
self.line_num = 0
return self.image_width