Resize rectangle

244 views
Skip to first unread message

VDK

unread,
Jul 4, 2015, 3:01:12 PM7/4/15
to kivy-...@googlegroups.com
Hi,

I'm working on selecting parts of an image by drawing a rectangle. I want to use the on_touch_down and the on_touch_move events. I took the Simple paint app from the documentation as a starting point. I thought to use on_touch_down to draw a small rectangle; on_touch_move then increases the initial size and should draw a new rectangle with the new size, using the same x, y positions as the initial rectangle. What I've tried so far is this piece of code (it is not a nice piece of code at the moment):

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line, Rectangle


class MyPaintWidget(Widget):

    def on_touch_down(self, touch):
        with self.canvas:
            Color(1, 1, 0)
            self.d = 30.
            self.dd = self.d
            self.xpos = touch.x
            self.ypos = touch.y
            #self.rect = Rectangle(pos=(touch.x - d/2, touch.y - d/2), size=(self.width, self.height))
            Rectangle(pos=(touch.x - self.d / 2, touch.y - self.d / 2), size=(self.d, self.d))
            #touch.ud['line'] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
    self.dd += 10.
    Rectangle(pos=(self.xpos - self.d / 2, self.ypos - self.d / 2), size=(self.dd, self.dd))
    #touch.ud['line'].points += [touch.x, touch.y]        


class MyPaintApp(App):

    def build(self):
        return MyPaintWidget()


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


This does not do what I want. I wonder if it is possible to resize a rectangle. Any ideas?

Thanks,

Peter

ZenCODE

unread,
Jul 5, 2015, 2:17:26 PM7/5/15
to kivy-...@googlegroups.com
Yeah, the problem is that you are creating a new rectangle each time. This should help you get started, but not sure what you are trying to do with the sizing? :-)

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line, Rectangle


class MyPaintWidget(Widget):

   
def on_touch_down(self, touch):
       
with self.canvas:
           
Color(1, 1, 0)
           
self.d = 30.
           
self.dd = self.d
           
self.xpos = touch.x
           
self.ypos = touch.
y
           
self.rect = Rectangle(

                pos
=(touch.x - self.d / 2, touch.y - self.d / 2),
                size
=(self.d, self.d))


   
def on_touch_move(self, touch):
       
self.dd += 10.
       
# self.rect.pos = (self.xpos - self.d / 2, self.ypos - self.d / 2)
       
self.rect.pos = touch.pos
       
self.rect.size = (self.dd, self.dd)

VDK

unread,
Jul 6, 2015, 12:36:54 PM7/6/15
to kivy-...@googlegroups.com
Hi ZenCODE,

This was very helpful, thanks. What I try to achieve is to extract part of an image. The rectangle marks the area to be extracted.

Peter

VDK

unread,
Jul 6, 2015, 3:25:38 PM7/6/15
to kivy-...@googlegroups.com
I've got it working the way I had in mind. See the code below.

Peter

import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line, Rectangle
from kivy.graphics.instructions import InstructionGroup

class MyPaintWidget(Widget):
def on_touch_down(self, touch):
with self.canvas:
Color(1, 1, 0)
self.w = 1.
self.h = 1.
# Keep starting position and initialize variables to store
# the previous x and y position
self.start_x = self.pr_x = touch.x
self.start_y = self.pr_y = touch.y
         
self.rect = Rectangle(pos=(touch.x, touch.y), size=(self.w, self.h))

def on_touch_move(self, touch):
self.calculate_size(touch)
self.rect.pos = (self.start_x, self.start_y)
self.rect.size = (self.w, self.h)

def calculate_size(self, touch):
if touch.x > self.pr_x:
self.w += touch.x - self.pr_x
self.pr_x = touch.x
if touch.y > self.pr_y:
self.h += touch.y - self.pr_y
self.pr_y = touch.y
if touch.x < self.pr_x:
self.w -= self.pr_x - touch.x 
self.pr_x = touch.x
if touch.y < self.pr_y:
self.h -= self.pr_y - touch.y 
self.pr_y = touch.y

class MyPaintApp(App):
    def build(self):
        return MyPaintWidget()


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

VDK

unread,
Jul 7, 2015, 5:38:30 AM7/7/15
to kivy-...@googlegroups.com
I also made a version that uses Line to create a resizable rectangle, see code below.

Peter


import kivy
kivy.require('1.0.6')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line, Rectangle
from kivy.graphics.instructions import InstructionGroup

class MyPaintWidget(Widget):
def on_touch_down(self, touch):
with self.canvas:
Color(1, 1, 0)
# Keep starting position and initialize variables to store
# the previous x and y position
self.bl_x = touch.x # bottom left
self.bl_y = touch.y
self.br_x = 0 # bottom right
self.br_y = 0
self.ur_x = 0 # upper right
self.ur_y = 0
self.ul_x = 0 # upper left
self.ul_y = 0
         
self.line = Line(points=([self.bl_x, self.bl_y]), width=1)

def on_touch_move(self, touch):
self.calculate_points(touch)
self.line.points = [self.bl_x, self.bl_y, self.br_x, self.br_y,
self.ur_x, self.ur_y,self.ul_x, self.ul_y,
self.bl_x, self.bl_y]

def calculate_points(self, touch):
self.ur_x = touch.x
self.ur_y = touch.y

self.br_x = self.ur_x
self.br_y = self.bl_y
self.ul_x = self.bl_x
self.ul_y = self.ur_y

class MyPaintApp(App):
    def build(self):
        return MyPaintWidget()


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

On Monday, July 6, 2015 at 9:25:38 PM UTC+2, VDK wrote:
I've got it working the way I had in mind. See the code below.

Peter

(text deleted) 
Reply all
Reply to author
Forward
0 new messages