Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored the code that renders device art images.
- Loading branch information
1 parent
76a0a88
commit cad94bb
Showing
8 changed files
with
76 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
from django import forms | ||
|
||
from twobuntu.touch.generator import TEMPLATES | ||
|
||
|
||
class DeviceArtForm(forms.Form): | ||
""" | ||
Form for uploading an app screenshot. | ||
Form for uploading an app image. | ||
""" | ||
|
||
template = forms.TypedChoiceField( | ||
choices=[(i, t['title']) for i, t in enumerate(TEMPLATES)], | ||
coerce=int, | ||
) | ||
image = forms.ImageField() | ||
add_panel = forms.BooleanField(required=False, label='Add the Unity panel to the picture') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from io import BytesIO | ||
from math import ceil | ||
|
||
from django.contrib.staticfiles import finders | ||
from PIL import Image | ||
|
||
# Coordinates for rendering the picture with and without the Unity panel | ||
# Each item in the array represents a layer for the generator to render | ||
# The format of the tuple is (filename, dest_x, dest_y, dest_w, dest_h) | ||
TEMPLATES = [ | ||
{ | ||
'title': 'Meizu MX3 with Unity Panel', | ||
'dimensions': (1346, 2313), | ||
'layers': [ | ||
(None, 131, 286, 1080, 1727), | ||
('panel.png', 131, 213, 1080, 73), | ||
('mx3.png', 0, 0, 1346, 2313), | ||
], | ||
}, | ||
{ | ||
'title': 'Meizu MX3 without Unity Panel', | ||
'dimensions': (1346, 2313), | ||
'layers': [ | ||
(None, 131, 213, 1080, 1800), | ||
('mx3.png', 0, 0, 1346, 2313), | ||
], | ||
}, | ||
] | ||
|
||
|
||
def generate_device_art(template, image): | ||
""" | ||
Combine the layers for the template into a final image. | ||
""" | ||
# Create the final image upon which everything will be rendered | ||
t = TEMPLATES[template] | ||
o = Image.new('RGBA', t['dimensions']) | ||
# For each of the layers, resize and crop according to the definition, and blend | ||
for l in t['layers']: | ||
i = Image.open(finders.find('img/touch/%s' % l[0]) if l[0] else image) | ||
w, h = i.size | ||
i = i.crop((0, 0, w, int(ceil(float(l[4]) / float(l[3]) * w)))) | ||
i = i.resize((l[3], l[4]), Image.ANTIALIAS) | ||
c = Image.new('RGBA', t['dimensions']) | ||
c.paste(i, (l[1], l[2])) | ||
o = Image.alpha_composite(o, c) | ||
# Return a file-like object representing the rendered picture | ||
response = BytesIO() | ||
o.save(response, format='PNG') | ||
return response.getvalue() |
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters