Skip to content

Commit

Permalink
Implement MockupGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
YayunHuang committed Aug 29, 2024
1 parent 37d30eb commit 23d9fcd
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 94 deletions.
2 changes: 1 addition & 1 deletion mockup_package/mockup/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .image_mockup import mockup as startMockup # noqa: F401
from .mockup_generator import MockupGenerator # noqa: F401
91 changes: 0 additions & 91 deletions mockup_package/mockup/image_mockup.py

This file was deleted.

112 changes: 112 additions & 0 deletions mockup_package/mockup/mockup_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import base64
import sys
from pathlib import Path
from typing import Any, Self
from pyodide.http import pyfetch
from mockup.image_generator import ImageGenerator as IG
import os


class MockupGenerator:
location: str
device_id: str
original_img_path: str
device_info: dict[str, Any]
orientation_name: str
device_path_prefix: str
device_mask_path_prefix: str
device_path: str
device_mask_path: str

def __init__(
self,
location,
device_id: str,
original_img_path: str,
device_info: dict[str, Any],
orientation_name: str,
):
self.location = location
self.device_id = device_id
self.original_img_path = original_img_path
self.device_info = device_info
self.orientation_name = orientation_name

self.device_path_prefix: str = (
f"{location.split('/')[0]}//{location.split('/')[2]}"
)
self.device_mask_path_prefix: str = (
self.device_path_prefix + "/images/mockup_mask_templates/"
)
self.device_path_prefix += "/images/mockup_templates/"
self.device_path: str = "./device.png"
self.device_mask_path: str = "./device_mask.png"

async def generate(
self: Self,
spec: dict[str, Any],
ig: IG,
) -> tuple[str, str, str]:
try:
await self.process_response(
self.device_path_prefix + str(spec["image"]),
self.device_path,
)
await self.process_response(
self.device_mask_path_prefix + str(spec["image"]),
self.device_mask_path,
)
except Exception as e:
print(e, file=sys.stderr)
# js.errorBox(e)
raise
ig.create_fit_coord_image(spec)
deviceView = str(spec["image"]).split("-")[-1].split(".")[0]
path = (
f"{os.path.splitext(os.path.basename(self.original_img_path))[0]}"
+ f"-{deviceView}.png"
)
ig.create_mockup_image(self.device_path, self.device_mask_path, path)
return (path, self.original_img_path, deviceView)

async def mockup(
self: Self,
):
ig = IG(self.original_img_path, self.device_id, self.device_info)
ig.create_fit_resolution_image()
mockups = list(ig.phone_models.get(self.device_id).get("mockups").values())

for spec in mockups:
if spec.get("name") == self.orientation_name:
output_img_path = await self.generate(spec, ig)

return output_img_path

raise Exception("Cannot find orientation", self.orientation_name)

async def download(self: Self, url: str):
filename = Path(url).name
response = await pyfetch(url)
if response.status == 200:
status = response.status
with open(filename, mode="wb") as file:
file.write(await response.bytes())
return filename, status
else:
status = response.status
filename = None
return filename, status

async def process_response(self: Self, url: str, path: str):
response_content = await self.download(url)
if response_content[1] == 200:
data = base64.b64encode(open(response_content[0], "rb").read())
data = data.decode("utf-8")
imgdata = base64.b64decode(data)
filename = path # I assume you have a way of picking unique filenames
with open(filename, "wb+") as f:
f.write(imgdata)
return filename
else:
src = None
return src
Binary file modified public/mockup.zip
Binary file not shown.
6 changes: 4 additions & 2 deletions public/scripts/mockup_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function runMockup(pyodide) {
let pythonNamespace = pyodide.globals.get("dict")();
await pyodide.runPythonAsync(
`
import mockup
from mockup import MockupGenerator
import image_process
from js import locationKey, deviceInfo, deviceId, orientationsQueue
Expand All @@ -34,7 +34,9 @@ async function runMockup(pyodide) {
orientation = orientationsQueue.shift()
print("start mockup", origin_image_path)
print("orientation", orientation)
return await mockup.startMockup(locationKey, deviceId, origin_image_path, deviceInfo, orientation)
mockup_generator = MockupGenerator(locationKey, deviceId, origin_image_path, deviceInfo, orientation)
return await mockup_generator.mockup()
output_img = await runMockup()
`,
Expand Down

0 comments on commit 23d9fcd

Please sign in to comment.