Skip to content

Commit

Permalink
Revert "Preview first mockup after updating image"
Browse files Browse the repository at this point in the history
  • Loading branch information
pkong-ds authored Aug 19, 2024
1 parent 2d78d8c commit e0f6864
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 361 deletions.
1 change: 0 additions & 1 deletion mockup_package/mockup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .image import mockup as startMockup # noqa: F401
from .image import previewMockup # noqa: F401
91 changes: 28 additions & 63 deletions mockup_package/mockup/image.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,49 @@
import base64
import sys
from pathlib import Path
from typing import Any
from pyodide.http import pyfetch
from mockup.image_generator import ImageGenerator as IG
import os


async def generate(
location: str,
original_img_path: str,
spec: dict[str, Any],
ig: IG,
) -> tuple[str, str, str]:
device_path_prefix: str = f"{location.split('/')[0]}//{location.split('/')[2]}"
device_mask_path_prefix: str = device_path_prefix + "/images/mockup_mask_templates/"
async def mockup(location, device_id, original_img_path_list, device_info):
device_path_prefix = f"{location.split('/')[0]}//{location.split('/')[2]}"
device_mask_path_prefix = device_path_prefix + "/images/mockup_mask_templates/"
device_path_prefix += "/images/mockup_templates/"
device_path: str = "./device.png"
device_mask_path: str = "./device_mask.png"

try:
await process_response(
device_path_prefix + str(spec["image"]),
device_path,
)
await process_response(
device_mask_path_prefix + str(spec["image"]),
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(original_img_path))[0]}"
+ f"-{deviceView}.png"
)
ig.create_mockup_image(device_path, device_mask_path, path)
return (path, original_img_path, deviceView)


async def mockup(
location: str,
device_id: str,
original_img_path_list: list[str],
device_info: dict[str, Any],
):
output_img_path_list: list[tuple[str, str, str]] = []
device_path = "./device.png"
device_mask_path = "./device_mask.png"
output_img_path_list = []
for original_img_path in original_img_path_list:
ig = IG(original_img_path, device_id, device_info)
ig.create_fit_resolution_image()
for spec in ig.phone_models.get(device_id).get("mockups").values():
output_img_path_list.append(
await generate(location, original_img_path, spec, ig)
try:
await process_response(
device_path_prefix + str(spec["image"]),
device_path,
)
await process_response(
device_mask_path_prefix + str(spec["image"]),
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(original_img_path))[0]}"
+ f"-{deviceView}.png"
)
ig.create_mockup_image(device_path, device_mask_path, path)
output_img_path_list.append([path, original_img_path, deviceView])

original_img_path_list.clear()
return output_img_path_list


async def previewMockup(
location: str,
device_id: str,
original_img_path: str,
device_info: dict[str, Any],
preview_orientation_index: int = 0,
):
ig = IG(original_img_path, device_id, device_info)
ig.create_fit_resolution_image()
spec = list(ig.phone_models.get(device_id).get("mockups").values())[
preview_orientation_index
]
output_img_path = await generate(location, original_img_path, spec, ig)

return output_img_path


async def download(url: str):
async def download(url):
filename = Path(url).name
response = await pyfetch(url)
if response.status == 200:
Expand All @@ -92,7 +57,7 @@ async def download(url: str):
return filename, status


async def process_response(url: str, path: str):
async def process_response(url, path):
response_content = await download(url)
if response_content[1] == 200:
data = base64.b64encode(open(response_content[0], "rb").read())
Expand Down
44 changes: 4 additions & 40 deletions public/image_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import io
import os

from js import Uint8Array, imageUploadList, imageUpload
from js import Uint8Array, imageUploadList
from PIL import Image


async def upload_single_image_and_save_to_list(
origin_image, file_name, original_img_path_list
):
async def upload_single_image(origin_image, file_name, original_img_path_list):
array_buf = Uint8Array.new(await origin_image.arrayBuffer())
bytes_list = bytearray(array_buf)
origin_bytes = io.BytesIO(bytes_list)
Expand All @@ -18,51 +16,17 @@ async def upload_single_image_and_save_to_list(
my_image.save(filePath)


async def upload_single_image(origin_image, file_name):
array_buf = Uint8Array.new(await origin_image.arrayBuffer())
bytes_list = bytearray(array_buf)
origin_bytes = io.BytesIO(bytes_list)
my_image = Image.open(origin_bytes)
filePath = f"./{file_name}.png"
my_image.save(filePath)
return filePath


async def upload_file():
basename, ext = os.path.splitext(imageUpload.name)
if ext.lower() not in [".psd", ".jpg", ".jpeg", ".png"]:
return
original_img_path = await upload_single_image(imageUpload, basename)
return original_img_path


async def upload_files():
original_img_path_list = []
for fileItem in imageUploadList:
basename, ext = os.path.splitext(fileItem.name)
if ext.lower() not in [".psd", ".jpg", ".jpeg", ".png"]:
return
await upload_single_image_and_save_to_list(
fileItem, basename, original_img_path_list
)
await upload_single_image(fileItem, basename, original_img_path_list)
return original_img_path_list


def save_image(image):
print("image", image)
path = image[0]
my_image = Image.open(path)
my_stream = io.BytesIO()
my_image.save(my_stream, format="PNG")
binary_fc = open(path, "rb").read()
base64_utf8_str = base64.b64encode(binary_fc).decode("utf-8")
basename, ext = os.path.splitext(path)
dataurl = f"data:image/{ext};base64,{base64_utf8_str}"
print(basename)
return [f"img{basename}", dataurl]


def save_images(imageList):
def save_image(imageList):
returnList = []
for image in imageList:
path = image[0]
Expand Down
Binary file modified public/mockup.zip
Binary file not shown.
2 changes: 0 additions & 2 deletions public/scripts/models/image-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class ImageUpload {
signedData = null;
readState = ReadState.ReadyForRead;
message = null;
ulid = null;
previewUrl = null;

loadDimensionPromise = null;

Expand Down
69 changes: 0 additions & 69 deletions public/scripts/preview_worker.js

This file was deleted.

Loading

0 comments on commit e0f6864

Please sign in to comment.