diff --git a/twobuntu/touch/forms.py b/twobuntu/touch/forms.py index ca2109a..68b8c04 100644 --- a/twobuntu/touch/forms.py +++ b/twobuntu/touch/forms.py @@ -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') diff --git a/twobuntu/touch/generator.py b/twobuntu/touch/generator.py new file mode 100644 index 0000000..35e2cf1 --- /dev/null +++ b/twobuntu/touch/generator.py @@ -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() diff --git a/twobuntu/touch/static/img/touch-preview.png b/twobuntu/touch/static/img/touch-preview.png deleted file mode 100644 index e21bab0..0000000 Binary files a/twobuntu/touch/static/img/touch-preview.png and /dev/null differ diff --git a/twobuntu/touch/static/img/touch/mx3-preview.png b/twobuntu/touch/static/img/touch/mx3-preview.png new file mode 100644 index 0000000..ec2ff93 Binary files /dev/null and b/twobuntu/touch/static/img/touch/mx3-preview.png differ diff --git a/twobuntu/touch/static/img/touch-frame.png b/twobuntu/touch/static/img/touch/mx3.png similarity index 100% rename from twobuntu/touch/static/img/touch-frame.png rename to twobuntu/touch/static/img/touch/mx3.png diff --git a/twobuntu/touch/static/img/touch-panel.png b/twobuntu/touch/static/img/touch/panel.png similarity index 100% rename from twobuntu/touch/static/img/touch-panel.png rename to twobuntu/touch/static/img/touch/panel.png diff --git a/twobuntu/touch/templates/touch/generator.html b/twobuntu/touch/templates/touch/generator.html index 94f8a12..9984ab4 100644 --- a/twobuntu/touch/templates/touch/generator.html +++ b/twobuntu/touch/templates/touch/generator.html @@ -4,7 +4,7 @@ {% block content %}