我如何使用python遮盖猕猴桃中的图像? [英] How do i mask an image in kivy using python?
问题描述
我正在尝试使用特定的遮罩遮罩图像,但是我似乎找不到办法.
I'm trying to mask an image with a specific mask, but i can't seem to find a way.
我已经阅读了文档,并找到了 https://kivy. org/docs/api-kivy.graphics.stencil_instructions.html#看起来很有希望,但该示例仅使用奇异果语言(我在我的项目中未使用过)
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)
我还找到了这个 https://groups.google .com/forum/#!topic/kivy-users/-XXe3F2JoSc ,但第一个示例有点令人困惑,第二个示例使用的StencilView似乎是另一回事
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
部分代码:
#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...
推荐答案
Tito已经在邮件列表中回答了它,但我将对其进行更多描述.要在画布中包含图像,可以完全使用Rectangle()
,source
.但是这里会出现一个大小合适的问题,所以要么写出图像的完整大小,要么写一个size=[640/5.0,480/5.0]
或仅仅写一个640/5
(我用py2.7)
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)
文档中描述了如何使用Stencil,在python中,它的确非常相似.您使用with canvas:
,其中canvas是小部件的画布,因此为self.canvas
. mask()
函数对您来说很有趣,在这里我只说了几句话以使您清楚.
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()
这篇关于我如何使用python遮盖猕猴桃中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!