Kivy: get_pixel_color (fbo, svg, scatter)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Kivy: get_pixel_color (fbo, svg, scatter)



I have an application with an SVG image that I can scale and move using scatter.



My task is to write a function, get_widget_pixel_color (), that is, I want to get the color of the picture in a certain place (pixel), for example under the mouse.



I started, but got confused with the coordinate formula, so this function produces incorrect values.


import sys
from glob import glob
from os.path import join, dirname
from kivy.uix.scatter import Scatter
from kivy.app import App
from kivy.graphics.svg import Svg

from kivy.graphics import (
Canvas, Translate, Fbo, ClearColor, ClearBuffers, Scale)
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image

Builder.load_string("""
<SvgWidget>:
id:s
do_rotation: False

<FloatLayout>:
id:f
canvas.before:
Color:
rgb: (1, 1, 1)
Rectangle:
pos: self.pos
size: self.size
""")


class SvgWidget(Scatter):

def __init__(self, filename, **kwargs):
super(SvgWidget, self).__init__(**kwargs)
with self.canvas:
svg = Svg(filename)
self.size = svg.width, svg.height





def get_widget_pixel_color(self,x_pos,y_pos):

self.size = (Window.size[0]-self.x, Window.size[1]-self.y)


if self.parent is not None:
canvas_parent_index = self.parent.canvas.indexof(self.canvas)
if canvas_parent_index > -1:
self.parent.canvas.remove(self.canvas)

fbo = Fbo(size=self.size, with_stencilbuffer=True)

with fbo:
ClearColor(0, 0, 0, 0)
ClearBuffers()
Scale(1, -1, 1)
Translate(-self.x, -self.y - self.height, 0)

fbo.add(self.canvas)
fbo.draw()

# self.im.size= self.im.texture_size
# self.im.pos= self.pos
# self.im.pos= (0,0)

x_pos = int(x_pos)
y_pos = int(y_pos)

# print x_pos-self.x
# x_off = x_pos-self.x


color = fbo.get_pixel_color(x_pos-self.x, y_pos)

# print color


fbo.remove(self.canvas)

if self.parent is not None and canvas_parent_index > -1:
self.parent.canvas.insert(canvas_parent_index, self.canvas)

return color





def my_collide_point(self, x_pos, y_pos):


if self.get_widget_pixel_color(x_pos,y_pos)[-1] > 0:
return True
return False



def on_touch_down(self,touch):

print self.my_collide_point(*touch.pos)
return super(SvgWidget, self).on_touch_down(touch)


class SvgApp(App):

def build(self):
self.root = FloatLayout()

filenames = sys.argv[1:]
if not filenames:
filenames = glob(join(dirname(__file__), '*.svg'))

for filename in filenames:
svg = SvgWidget(filename, size_hint=(None, None))
# svg = SvgWidget(filename)
self.root.add_widget(svg)
svg.scale = 5.
svg.center = Window.center


if __name__ == '__main__':
SvgApp().run()



for SVG you can use any SVG file, for example



circle.svg


<svg height="100" width="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>



The idea is as follows:



I want it to work with moved (translated) and scaled svg image inside the scatter.



How to make the function correctly find the position of the picture?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered