Pre define image for optimized frame rate
I have a fairly complex (static) backdrop image on top of which I draw some animation.
Because I do not understand exactly how Pythonista deals with objects in the draw() function of a scene, I would expect it to be a good idea to pre draw the backdrop to an image and then refresh this every frame.
However is that basicly necessary and a good idea ?
Secondly I am having a lot of trouble trying to define such an image.
The image creation goes fine but no matter what I try to draw, it allways ends up as a white rectangle.
Sorry to be a little un specific and not showing any code, but is there an example somewhere showing this for a scene setup() and draw() ?
On second thoughts, here are my code in the scene...
def setup(self): global img img = Image.new("RGB", (300,300)) draw = ImageDraw.Draw(img) draw.line((0,0,200,200),fill=200) del draw def draw(self): background(0.09,0.09,0.102) global img image('img',300,300,300,300)
Does the solution below help?
I think <b>load_pil_image()</b> was the missing function call... It converts a PIL image into an image name (string) that scene can understand.
The fill=200 line color is too close the background color. I added the ivory and blue to make sure the image and the line in the image stand out. When specifying colors, I prefer to use names (ivory) to numbers (200).
Also, I would encourage avoiding the use of 'global' wherever possible and the use of 'self.' In its place. Below, I used self. to pass imageName from setup() to draw().
from PIL import Image, ImageDraw
class MyScene (Scene):
img = Image.new("RGBA", (300,300), 'ivory')
draw = ImageDraw.Draw(img)
self.imageName = load_pil_image(img)
def draw(self): background(0.09,0.09,0.102) image(self.imageName,300,300,300,300)
That put a lot in place for me :-)
I had messed around with the load_pil_image but not in conjugation with the init of the image.
I come from Codea where things are a bit more straight forward and direct understandable.
A task like this would be. Define the image - write to context - end context, and when you want to use the image, it is just a call to the name, because it allready exist in memory.
Also I took a mental note on the global issue and use self instead.