Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import base64
from scene import *
from PIL import Image
def load_keyboard(retina_display = False):
kb_filename = "keyboard.png"
if retina_display:
kb_filename = "keyboard@2x.png"
kb_image = load_image_file(kb_filename)
if not kb_image:
# File didn't load, assume it needs creation
import ImageFile
keyboard_b64 = 'iVBORw0KGgoAAAANSUhEUgAAAT4AAADGAgMAAAA24ARjAAAACVBMVEX///8AAAAAAP9'+\
'TU0bQAAAAq0lEQVR4Xu3YKw7DMBCGwSW935IQn64kxKes1EdIFUWNttK2mo+YWMMMfs'+\
'fyaBSdWQ9m3BtFZ3sQCAQCgUAgEAgE9t8ps7br/4JAYNXz+x5YtAC6g0AgEAgEAoFAI'+\
'PBndspx1igQuGydf4Jz3ZCsBzOeXc6vgbnGq+4gEAgEAoFAIBAI7L9TrNEPA/pz2BrL'+\
'Xsf3sh7MeGtk7HR8rzsIBAKBQCAQCAQC+++UG1ekEcXm6D5EAAAAAElFTkSuQmCC'
keyboard_png = base64.b64decode(keyboard_b64)
# Write out the 1x version
parser = ImageFile.Parser()
parser.feed(keyboard_png)
kb_png = parser.close()
kb_png.save("keyboard.png")
orig_size = list(kb_png.size)
retina_size = map(lambda x: x*2, orig_size)
kb_png.resize(retina_size)
# Write out the 2x version
kb_png.save("keyboard@2x.png")
kb_image = load_image_file(kb_filename)
return [kb_image, Image.open(kb_filename).size]
class MyScene (Scene):
def setup(self):
self.root_layer = Layer(self.bounds)
self.retina_display = False
if min(self.bounds.w, self.bounds.h) > 500:
self.retina_display = True
self.keyboard, self.kb_size = load_keyboard(self.retina_display)
self.layer = Layer(Rect(0, 0, self.kb_size[0], self.kb_size[1]))
self.layer.background = Color(0, 0, 0)
self.layer.image = self.keyboard
self.root_layer.add_layer(self.layer)
def draw(self):
background(0, 0, 0)
self.root_layer.update(self.dt)
self.root_layer.draw()
run(MyScene())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.