How do i mask an image in kivy using python?

亡梦爱人 提交于 2020-01-06 21:41:10

问题


I'm trying to mask an image with a specific mask, but i can't seem to find a way.

I've read the documentation and found this https://kivy.org/docs/api-kivy.graphics.stencil_instructions.html# which looks promising but the example is only in kivy language (which i don't use in my project)

I've also found this https://groups.google.com/forum/#!topic/kivy-users/-XXe3F2JoSc but first example is's a bit confusing and second one uses StencilView which seems to be a different thing

So an universal example would be appretiated

Part of code:

    #some code...

    picture_mask = Image(source = "Assets/picture_frame_mask.png", size_hint = (0, 0),
                                       size = (200, 200),
                                       pos = (50,100))
    #Now what ?

    player_picture = Image(source = "Assets/player.png", size_hint = (0, 0),
                          allow_stretch = True, keep_ratio = False,
                          size = (200, 200),
                          pos = (50,100))
    self.add_widget(player_picture)

    #rest of code...

回答1:


Tito already answered it in mailing list, but I'll describe it a little more. To have an image inside canvas you can use Rectangle(), source exactly. But here comes a problem with a proper size, so either write the full size of an image, or something like size=[640/5.0,480/5.0] or maybe just 640/5(I use py2.7)

How to use Stencil was described in docs, in python it's really similar. You use with canvas: where canvas is your widget's canvas, therefore self.canvas. What is interesting for you is the mask() function, where I commented few things to make it clear for you.

To understand what mask actually do, imagine mask as a piece of paper with a hole in it. You see only what's in the hole.

from kivy.graphics import *
#^^^^^^ all imports you need for that to work + App()/runTouchApp()

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class Root(Widget):
    def __init__(self, **kw):
        super(Root, self).__init__()
        with self.canvas:
            Color(1,0,0,0.5)
            Rectangle(pos=self.pos, size=Window.size)

        bx=BoxLayout(width=Window.width)
        but1=Button(text="mask it!")
        but1.bind(on_release=self.mask)
        but2=Button(text="clear it!")
        but2.bind(on_release=self.clear)

        bx.add_widget(but1)
        bx.add_widget(but2)
        self.add_widget(bx)
        #after few hundred lines of code you'll hate to type even ".add_"
        #binding on_release and other is a way more shorter
        #learn kv, have less troubles :)

    def mask(self, *args):
        with self.canvas:
            #you have to "push" the stencil instructions
            #imagine StencilPush and StencilPop as { and }
            StencilPush()

            #set mask
            Rectangle(pos=[self.pos[0],self.pos[1]+200], size=[100,100])

            #use mask
            StencilUse()

            #draw something and mask will be placed on it
            #Color(1,1,1) for full color, otherwise it tints image
            Color(0,1,0)
            Rectangle(source='<your picture>',\
            pos=[0,0], size=[Window.width-200,Window.height-200])

            #disable Stencil or mask will take effect on everything
            #until StencilPop()
            StencilPop()

            #here it doesn't take effect
            Color(0,1,1)
            Rectangle(pos=[self.pos[0]+200,self.pos[1]+200],\
            size=[Window.width-200,Window.height-200])

            #However here, when StencilPop() and StencilUse()
            #are again, mask is still in the memory
            StencilPush()
            StencilUse()

            #mask is applied... and draws nothing
            #because it's not in the mask's area
            Color(1,1,0)
            Rectangle(pos=[self.pos[0]+200,self.pos[1]+200],\
            size=[Window.width-200,Window.height-200])

            #I think UnUse() is optional
            StencilUnUse()

            #and of course Pop() it
            StencilPop()

    def clear(self, *args):
        self.canvas.clear()
        self.__init__()

class My(App):
    def build(self):
        return Root()

My().run()


来源:https://stackoverflow.com/questions/35802203/how-do-i-mask-an-image-in-kivy-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!