29.12.2021.1

This commit is contained in:
ульба 2021-12-29 21:52:41 +05:00
commit 9cf14ca4ac
71 changed files with 476 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.DS_Store
.idea
*.pyc
__pycache__

87
MockupModule/__init__.py Normal file
View File

@ -0,0 +1,87 @@
import logging
import os
from importlib import import_module
from typing import List
from PIL import Image
from MockupModule import templates
from MockupModule.utils import random_string
generated_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'generated')
if not os.path.exists(generated_path):
os.makedirs(generated_path)
class MockupEngineer:
def __init__(self):
self.logger = logging.getLogger('MockupEngineer')
self.templates: List[templates.Template] = []
for template in templates.ALL_TEMPLATES:
self.templates.append(getattr(
import_module(name='MockupModule.templates.' + template,
package='MockupModule.templates.' + template + '.Device'), 'Device')())
def screenshot_resize(self, screenshot: str, width: int, height: int) -> str:
name = '{}/screenshot{}.png'.format(generated_path, random_string(16))
img = Image.open(screenshot)
img = img.resize((width, height), Image.ANTIALIAS)
img.save(name)
return name
def get_templates(self) -> dict:
dicted = dict()
for template in self.templates:
if template.type in dicted.keys():
dicted[template.type].append(template)
else:
dicted[template.type] = [template]
return dicted
def generate(self, template: templates.Template, screenshot: str, color: str = None, orientation: str = None):
self.logger.info(
'Generating: {}'.format(
', '.join(
[': '.join([str(a).upper(), str(b)]) for a, b in template.__dict__.items()])))
if not orientation:
img = Image.open(screenshot)
width, height = img.size
orientation = 'portrait' if height > width or height == width else 'landscape'
if orientation == 'portrait':
screen_width = template.portrait_resolution.width
screen_height = template.portrait_resolution.height
width_coordinate = template.__portrait_width__
height_coordinate = template.__portrait_height__
else:
screen_width = template.landscape_resolution.width
screen_height = template.landscape_resolution.height
width_coordinate = template.__landscape_width__
height_coordinate = template.__landscape_height__
screenshot = self.screenshot_resize(screenshot, screen_width, screen_height)
for template_color in template.colors:
if template_color.color == color:
template_path = template_color.portrait_path \
if orientation == 'portrait' else template_color.landscape_path
break
else:
template_path = template.colors[-1].portrait_path \
if orientation == 'portrait' else template.colors[-1].landscape_path
name = '{}/mockup{}.png'.format(generated_path, random_string(16))
template_img = Image.open(template_path)
mask_img = Image.new('RGBA', template_img.size, (0, 0, 0, 0))
screenshot_img = Image.open(screenshot)
mask_img.paste(screenshot_img, (width_coordinate, height_coordinate))
mask_img.paste(template_img, (0, 0), template_img)
mask_img.save(name)
return name

28
MockupModule/__main__.py Normal file
View File

@ -0,0 +1,28 @@
import os
from MockupModule import MockupEngineer
def main():
mockup = MockupEngineer()
i = 0
for template in mockup.templates:
print('[{}] {} {} ({})'.format(i, template.manufacturer, template.name, template.year))
i += 1
template = mockup.templates[int(input('Choose device: '))]
print('- - - - - - - - - -')
i = 0
for color in template.colors:
print('[{}] {}'.format(i, color.color))
i += 1
color = template.colors[int(input('Choose color: '))].color
print('- - - - - - - - - -')
screenshot = input('Enter path to screenshot: ')
print('- - - - - - - - - -\nWorking...')
mockup = mockup.generate(template, screenshot, color)
print('- - - - - - - - - -\nSuccess: {}'.format(mockup))
os.system('open {}'.format(mockup))
if __name__ == '__main__':
main()

View File

@ -0,0 +1,49 @@
import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import List
templates_path = os.path.join("/".join(os.path.dirname(os.path.abspath(__file__)).split('/')[:-1]), 'templates')
def __list_all_templates():
all_templates = [x.name for x in Path(templates_path).iterdir()
if x.is_dir() and not str(x).startswith('__') and not str(x).endswith('__')]
return all_templates
ALL_TEMPLATES = sorted(__list_all_templates())
__all__ = ALL_TEMPLATES + ["ALL_TEMPLATES"]
@dataclass(frozen=False)
class TemplateResolution:
width: int = field(default_factory=int)
height: int = field(default_factory=int)
@dataclass(frozen=False)
class TemplateColor:
color: str = ''
portrait_path: str = field(default_factory=str)
landscape_path: str = field(default_factory=str)
@dataclass(frozen=False)
class Template:
manufacturer: str = field(default_factory=str)
name: str = field(default_factory=str)
type: str = field(default_factory=str)
year: int = field(default_factory=int)
example_path: str = field(default_factory=str)
portrait_resolution: TemplateResolution = field(default_factory=TemplateResolution)
landscape_resolution: TemplateResolution = field(default_factory=TemplateResolution)
colors: List[TemplateColor] = field(default_factory=list)
__portrait_width__: int = field(default_factory=int)
__portrait_height__: int = field(default_factory=int)
__landscape_width__: int = field(default_factory=int)
__landscape_height__: int = field(default_factory=int)

View File

@ -0,0 +1,49 @@
from dataclasses import dataclass
from MockupModule.templates import templates_path, TemplateResolution, TemplateColor, Template
@dataclass
class Device(Template):
def __post_init__(self):
self.manufacturer = 'Apple'
self.name = 'iPhone 12'
self.type = 'phone'
self.year = 2020
self.example_path = templates_path + '/iphone12/example.png'
self.portrait_resolution = TemplateResolution(width=1170, height=2532)
self.landscape_resolution = TemplateResolution(width=2532, height=1170)
self.colors = [
TemplateColor(
color='Black',
portrait_path=templates_path + '/iphone12/black/portrait.png',
landscape_path=templates_path + '/iphone12/black/landscape.png',
),
TemplateColor(
color='Blue',
portrait_path=templates_path + '/iphone12/blue/portrait.png',
landscape_path=templates_path + '/iphone12/blue/landscape.png',
),
TemplateColor(
color='Green',
portrait_path=templates_path + '/iphone12/green/portrait.png',
landscape_path=templates_path + '/iphone12/green/landscape.png',
),
TemplateColor(
color='PRODUCT RED',
portrait_path=templates_path + '/iphone12/red/portrait.png',
landscape_path=templates_path + '/iphone12/red/landscape.png',
),
TemplateColor(
color='White',
portrait_path=templates_path + '/iphone12/white/portrait.png',
landscape_path=templates_path + '/iphone12/white/landscape.png',
)
]
self.__portrait_width__ = 180
self.__portrait_height__ = 180
self.__landscape_width__ = 180
self.__landscape_height__ = 180

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 319 KiB

View File

@ -0,0 +1,49 @@
from dataclasses import dataclass
from MockupModule.templates import templates_path, TemplateResolution, TemplateColor, Template
@dataclass
class Device(Template):
def __post_init__(self):
self.manufacturer = 'Apple'
self.name = 'iPhone 12 Mini'
self.type = 'phone'
self.year = 2020
self.example_path = templates_path + '/iphone12mini/example.png'
self.portrait_resolution = TemplateResolution(width=1080, height=2340)
self.landscape_resolution = TemplateResolution(width=2340, height=1080)
self.colors = [
TemplateColor(
color='Black',
portrait_path=templates_path + '/iphone12mini/black/portrait.png',
landscape_path=templates_path + '/iphone12mini/black/landscape.png',
),
TemplateColor(
color='Blue',
portrait_path=templates_path + '/iphone12mini/blue/portrait.png',
landscape_path=templates_path + '/iphone12mini/blue/landscape.png',
),
TemplateColor(
color='Green',
portrait_path=templates_path + '/iphone12mini/green/portrait.png',
landscape_path=templates_path + '/iphone12mini/green/landscape.png',
),
TemplateColor(
color='PRODUCT RED',
portrait_path=templates_path + '/iphone12mini/red/portrait.png',
landscape_path=templates_path + '/iphone12mini/red/landscape.png',
),
TemplateColor(
color='White',
portrait_path=templates_path + '/iphone12mini/white/portrait.png',
landscape_path=templates_path + '/iphone12mini/white/landscape.png',
)
]
self.__portrait_width__ = 160
self.__portrait_height__ = 160
self.__landscape_width__ = 180
self.__landscape_height__ = 180

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 278 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 208 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 304 KiB

View File

@ -0,0 +1,44 @@
from dataclasses import dataclass
from MockupModule.templates import templates_path, TemplateResolution, TemplateColor, Template
@dataclass
class Device(Template):
def __post_init__(self):
self.manufacturer = 'Apple'
self.name = 'iPhone 12 Pro'
self.type = 'phone'
self.year = 2020
self.example_path = templates_path + '/iphone12pro/example.png'
self.portrait_resolution = TemplateResolution(width=1170, height=2532)
self.landscape_resolution = TemplateResolution(width=2532, height=1170)
self.colors = [
TemplateColor(
color='Gold',
portrait_path=templates_path + '/iphone12pro/gold/portrait.png',
landscape_path=templates_path + '/iphone12pro/gold/landscape.png',
),
TemplateColor(
color='Graphite',
portrait_path=templates_path + '/iphone12pro/graphite/portrait.png',
landscape_path=templates_path + '/iphone12pro/graphite/landscape.png',
),
TemplateColor(
color='Pacific Blue',
portrait_path=templates_path + '/iphone12pro/pacificblue/portrait.png',
landscape_path=templates_path + '/iphone12pro/pacificblue/landscape.png',
),
TemplateColor(
color='Silver',
portrait_path=templates_path + '/iphone12pro/silver/portrait.png',
landscape_path=templates_path + '/iphone12pro/silver/landscape.png',
)
]
self.__portrait_width__ = 180
self.__portrait_height__ = 180
self.__landscape_width__ = 180
self.__landscape_height__ = 180

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 KiB

View File

@ -0,0 +1,44 @@
from dataclasses import dataclass
from MockupModule.templates import templates_path, TemplateResolution, TemplateColor, Template
@dataclass
class Device(Template):
def __post_init__(self):
self.manufacturer = 'Apple'
self.name = 'iPhone 12 Pro Max'
self.type = 'phone'
self.year = 2020
self.example_path = templates_path + '/iphone12promax/example.png'
self.portrait_resolution = TemplateResolution(width=1284, height=2778)
self.landscape_resolution = TemplateResolution(width=2778, height=1284)
self.colors = [
TemplateColor(
color='Gold',
portrait_path=templates_path + '/iphone12promax/gold/portrait.png',
landscape_path=templates_path + '/iphone12promax/gold/landscape.png',
),
TemplateColor(
color='Graphite',
portrait_path=templates_path + '/iphone12promax/graphite/portrait.png',
landscape_path=templates_path + '/iphone12promax/graphite/landscape.png',
),
TemplateColor(
color='Pacific Blue',
portrait_path=templates_path + '/iphone12promax/pacificblue/portrait.png',
landscape_path=templates_path + '/iphone12promax/pacificblue/landscape.png',
),
TemplateColor(
color='Silver',
portrait_path=templates_path + '/iphone12promax/silver/portrait.png',
landscape_path=templates_path + '/iphone12promax/silver/landscape.png',
)
]
self.__portrait_width__ = 200
self.__portrait_height__ = 200
self.__landscape_width__ = 200
self.__landscape_height__ = 200

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 287 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 343 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 290 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 KiB

View File

@ -0,0 +1,39 @@
from dataclasses import dataclass
from MockupModule.templates import templates_path, TemplateResolution, TemplateColor, Template
@dataclass
class Device(Template):
def __post_init__(self):
self.manufacturer = 'Apple'
self.name = 'iPhone SE'
self.type = 'phone'
self.year = 2020
self.example_path = templates_path + '/iphonese2020/example.png'
self.portrait_resolution = TemplateResolution(width=750, height=1334)
self.landscape_resolution = TemplateResolution(width=1334, height=750)
self.colors = [
TemplateColor(
color='Black',
portrait_path=templates_path + '/iphonese2020/black/portrait.png',
landscape_path=templates_path + '/iphonese2020/black/landscape.png',
),
TemplateColor(
color='PRODUCT RED',
portrait_path=templates_path + '/iphonese2020/red/portrait.png',
landscape_path=templates_path + '/iphonese2020/red/landscape.png',
),
TemplateColor(
color='White',
portrait_path=templates_path + '/iphonese2020/white/portrait.png',
landscape_path=templates_path + '/iphonese2020/white/landscape.png',
)
]
self.__portrait_width__ = 150
self.__portrait_height__ = 300
self.__landscape_width__ = 300
self.__landscape_height__ = 140

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 241 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 231 KiB

View File

@ -0,0 +1,54 @@
from dataclasses import dataclass
from MockupModule.templates import templates_path, TemplateResolution, TemplateColor, Template
@dataclass
class Device(Template):
def __post_init__(self):
self.manufacturer = 'Apple'
self.name = 'iPhone Xr'
self.type = 'phone'
self.year = 2018
self.example_path = templates_path + '/iphonexr/example.png'
self.portrait_resolution = TemplateResolution(width=828, height=1792)
self.landscape_resolution = TemplateResolution(width=1792, height=828)
self.colors = [
TemplateColor(
color='Blue',
portrait_path=templates_path + '/iphonexr/blue/portrait.png',
landscape_path=templates_path + '/iphonexr/blue/landscape.png',
),
TemplateColor(
color='Coral',
portrait_path=templates_path + '/iphonexr/coral/portrait.png',
landscape_path=templates_path + '/iphonexr/coral/landscape.png',
),
TemplateColor(
color='PRODUCT RED',
portrait_path=templates_path + '/iphonexr/red/portrait.png',
landscape_path=templates_path + '/iphonexr/red/landscape.png',
),
TemplateColor(
color='Silver',
portrait_path=templates_path + '/iphonexr/silver/portrait.png',
landscape_path=templates_path + '/iphonexr/silver/landscape.png',
),
TemplateColor(
color='Space Gray',
portrait_path=templates_path + '/iphonexr/spacegray/portrait.png',
landscape_path=templates_path + '/iphonexr/spacegray/landscape.png',
),
TemplateColor(
color='Yellow',
portrait_path=templates_path + '/iphonexr/yellow/portrait.png',
landscape_path=templates_path + '/iphonexr/yellow/landscape.png',
)
]
self.__portrait_width__ = 110
self.__portrait_height__ = 110
self.__landscape_width__ = 110
self.__landscape_height__ = 110

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 208 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 265 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

29
MockupModule/utils.py Normal file
View File

@ -0,0 +1,29 @@
import asyncio
import functools
import random
import string
from FlowyModule.models import Track
def run_sync(func, *args, **kwargs):
return asyncio.get_event_loop() \
.run_in_executor(None,
functools.partial(func,
*args,
**kwargs))
def random_string(n: int):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(n))
def prepare_name(name: str):
symbols = '-1234567890qwertyuiopasdfghjklzxcvbnmйцукенгшщзхъфывапролджэячсмитьбю'
return "".join([a if a.lower() in symbols else "-" for a in name])
def gen_track_name(element: Track, container: str):
return '{}-{}-{}.{}'.format(prepare_name(element.artists[0].name),
prepare_name(element.title[:15]),
random_string(6), container)