Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2017 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a781c5f46e3292821f18736fd6f00ec7 to your computer and use it in GitHub Desktop.
Save anonymous/a781c5f46e3292821f18736fd6f00ec7 to your computer and use it in GitHub Desktop.
generate_image_from_pyui.py
import ui
import os
import math
def get_image(template_view):
x,y,w,h = template_view.frame
with ui.ImageContext(w,h) as ctx:
for t in template_view.subviews:
k = t.name
x,y,w,h = t.frame
if k.startswith('button'):
if t.title == 'oval':
path = ui.Path.oval(x,y,w,h)
elif t.title == 'rect':
path = ui.Path.rect(x,y,w,h)
elif t.title == 'rounded_rect':
path = ui.Path.rounded_rect(x,y,w,h, t.corner_radius)
else:
path = ui.Path()
ui.set_color(t.background_color)
path.fill()
path.line_width = t.border_width
ui.set_color(t.border_color)
path.stroke()
elif k.startswith('label'):
ui.draw_string(t.text, rect=(x,y,w,h), font=t.font,
color=t.text_color, alignment=t.alignment,
line_break_mode=t.line_break_mode)
elif k.startswith('imageview'):
t.image.draw(x,y,w,h)
return ctx.get_image()
def showimage_action(sender):
main_view.image.show()
filename = 'image_savefile.png'
def saveimage_action(sender):
img = main_view.image
with open(filename, 'wb') as fp:
fp.write(img.to_png())
def create_main_view(pyui_file):
template_view = ui.load_view(pyui_file)
x,y,w,h = template_view.frame
main_view = ui.ImageView(frame=(0,0,w,h), image=get_image(template_view))
main_view.right_button_items = [ui.ButtonItem(action=showimage_action, title='show'),
ui.ButtonItem(action=saveimage_action, title='save')]
return main_view
main_view = create_main_view('test1.pyui')
main_view.present('sheet')
[
{
"class" : "View",
"attributes" : {
"name" : "",
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {500, 500}}",
"selected" : false,
"nodes" : [
{
"class" : "Button",
"attributes" : {
"font_size" : 15,
"title" : "oval",
"name" : "button2",
"corner_radius" : 0,
"border_width" : 10,
"border_color" : "RGBA(0.822222,1.000000,0.466667,1.000000)",
"class" : "Button",
"frame" : "{{80, 104}, {80, 32}}",
"background_color" : "RGBA(1.000000,0.400000,0.400000,1.000000)",
"uuid" : "3786824A-4414-413A-A2EF-FB5ECCB0256A"
},
"frame" : "{{50, 20}, {400, 70}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "Dog",
"font_name" : "Georgia",
"name" : "label1",
"text_color" : "RGBA(0.366667,0.577778,1.000000,1.000000)",
"class" : "Label",
"alignment" : "center",
"frame" : "{{75, 134}, {150, 32}}",
"uuid" : "342AE6B0-9C7D-4045-BE9A-1A9DA71A0981"
},
"frame" : "{{50, 150}, {169, 50}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "ImageView",
"attributes" : {
"class" : "ImageView",
"name" : "imageview1",
"uuid" : "803C1BEF-4623-464C-9759-EC6AA76E87BE",
"frame" : "{{100, 100}, {100, 100}}",
"custom_attributes" : "{'image':ui.Image('Dog_Face')}"
},
"frame" : "{{50, 260}, {200, 200}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Label",
"attributes" : {
"font_size" : 18,
"text" : "cat",
"font_name" : "<System>",
"name" : "label2",
"class" : "Label",
"alignment" : "center",
"frame" : "{{175, 234}, {150, 32}}",
"uuid" : "BE5A2534-E29F-4D09-8E30-F0AB95CAA269"
},
"frame" : "{{312, 150}, {150, 50}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "ImageView",
"attributes" : {
"class" : "ImageView",
"name" : "imageview2",
"uuid" : "F17AA584-2C5F-4029-9846-BF408A177F94",
"frame" : "{{200, 200}, {100, 100}}",
"custom_attributes" : "{'image':ui.Image('Cat_Face')}"
},
"frame" : "{{296, 260}, {200, 200}}",
"selected" : false,
"nodes" : [
]
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment