1
0
Fork 0

Added kivy_memory_image.py example for entry: How to load an image from memory in Kivy

This commit is contained in:
Daniele Tricoli 2013-11-06 19:52:07 +01:00
parent 4c0facd77a
commit 1fc4ee25fc
1 changed files with 106 additions and 0 deletions

106
blog/kivy_memory_image.py Normal file
View File

@ -0,0 +1,106 @@
#!/usr/bin/env python
# encoding: utf-8
# kivy_memory_image.py
# How to load an image from memory in Kivy
# http://mornie.org/blog/2013/11/06/how-load-image-memory-kivy/
# Copyright (c) 2013, Daniele Tricoli <eriol@mornie.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the <organization>.
# 4. Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import StringIO
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import numpy as np
from kivy.app import App
from kivy.core.image.img_pygame import ImageLoaderPygame
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
def cardioid(start, stop, step):
"""A rotated cardioid."""
theta = np.arange(start, stop, step)
r = 1 - np.sin(theta)
return theta, r
def polar_plot(theta, r, rmax):
"""Draw a polat plot.
:returns: matplotlib.Figure
"""
fig = Figure(facecolor='white')
ax = fig.add_subplot(111, polar=True, frameon=False)
ax.grid(False)
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.plot(theta, r, color='g', linewidth=2)
ax.set_rmax(rmax)
return fig
def fig2png(fig):
"""Convert a matplotlib.Figure to PNG image.
:returns: PNG image bytes
"""
data = StringIO.StringIO()
canvas = FigureCanvasAgg(fig)
canvas.print_png(data)
return data.getvalue()
class MemoryImage(Image):
"""Display an image already loaded in memory."""
memory_data = ObjectProperty(None)
def __init__(self, memory_data, **kwargs):
super(MemoryImage, self).__init__(**kwargs)
self.memory_data = memory_data
def on_memory_data(self, *args):
"""Load image from memory."""
data = StringIO.StringIO(self.memory_data)
with self.canvas:
self.texture = ImageLoaderPygame(data).texture
class TestApp(App):
def build(self):
return MemoryImage(self.options['image'])
if __name__ == '__main__':
theta, r = cardioid(0, 8.0, 0.01)
image = fig2png(polar_plot(theta, r, 2.5))
TestApp(image=image).run()