Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How can I add an easer in “sketch.py” in Example?
-
I want to customize “sketch.py” in Example. I want to add a button to switch to an eraser mode which enables to erase the scribbled line. “sketch.py” is very simple using only ui.Path to draw, and I first thought that changing color of the path (to white which is the same color of the view’s background) would do. However, ui.Path has no attribute for setting it’s color (is it correct?). Is there any good way to add the eraser function?
-
@satsuki.kojima said:
ui.Path has no attribute for setting it’s color (is it correct?)
No 😀
During drawing
def draw(self): if self.path: ui.set_color('red') self.path.stroke()
After saving
def path_action(self, sender): path = sender.path old_img = self.image_view.image width, height = self.image_view.width, self.image_view.height with ui.ImageContext(width, height) as ctx: if old_img: old_img.draw() ui.set_color('blue') path.stroke() self.image_view.image = ctx.get_image()
-
@satsuki.kojima said:
Is there any good way to add the eraser function?
That's what the clear button does, isn'it?
-
@cvp, I guess the idea is to add an eraser that clears some parts of the image.
This is challenging because Path is a vector, so you would need to see which of the paths points are covered by the eraser, and remove & split the path accordingly. This would not be perfect if the line was drawn fast and has longer straight segments.
Alternative is to turn the image to a bitmap when eraser is used. Depending on what you want to use the image for, loss of vector information is probably not an issue.
-
@mikael ok, agree. But small questions imply short answers 😀 And, of course, we're ready to help more during a project.
-
@satsuki.kojima you could try this (a very little) modified MySketch.py
-
@cvp, aa, a white, fatter path, of course.
-
@mikael ok, next button = line width 😀
-
to :@cvp
Thank you, it helps a lot. I may borrow your color palette to my app as well. -
@satsuki.kojima said:
your color palette
Not mine, old sample from, I think, Pythonista 's creator (or I even forgot I did it, very old brain)
-
@satsuki.kojima for the fun, MySketch.py modified for width (idea of @mikael )
-
Small modif of gist to 'show' white color in slider
-
to:@cvp
Yeah.... I finally decided my eraser to set the line_width to 20 while the default pen to 2.
I’m building a game “4 numbers”, which is to guess 4 randomly given numbers by hints of hits and blows. I wanted to add a sketch board so the players can scribble their process of guessing.By the way, I actually took for a while to fix my problem even though your advise was very clear. My sketch board showed some odd behavior. I finally found it was because I was writing my program adding subview of path_view before image_view like as below.
iv = ui.ImageView(frame=(0, 0, width, height)) pv = PathView(frame=self.bounds) pv.action = self.path_action pv.main = self self.add_subview(pv). [ <- shoud be after self.add_subview(iv)] self.add_subview(iv)
-