Skip to content

Commit

Permalink
Improve Image quality 👾
Browse files Browse the repository at this point in the history
This PR improves the image quality of the elements by fetching the 2X version of the image from figma and then scaling it down to 1X by using PIL.
  • Loading branch information
ParthJadhav authored Aug 7, 2021
2 parents cf44120 + 81c3c7e commit c0f270c
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 12 deletions.
155 changes: 150 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.8"
requests = "2.25.1"
urllib3 = "1.26.4"
Jinja2 = "3.0.1"
Pillow = "^8.3.1"
urllib3 = "^1.26.6"

[tool.poetry.dev-dependencies]
pytest = "^6.2.4"
flake8 = "^3.9.2"
pylint = "^2.9.6"

[tool.poetry.scripts]
tkdesigner = 'tkdesigner.cli:main'
Expand Down
4 changes: 1 addition & 3 deletions tkdesigner/figma/custom_elements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .vector_elements import Vector, Rectangle


TEXT_INPUT_ELEMENT_TYPES = {
"TextArea": "Text",
"TextBox": "Entry"
Expand Down Expand Up @@ -150,8 +149,7 @@ def __init__(self, node, frame, image_path, *, id_):
def to_code(self):
return f"""
entry_image_{self.id_} = PhotoImage(
file=relative_to_assets("{self.image_path}")
)
file=relative_to_assets("{self.image_path}"))
entry_bg_{self.id_} = canvas.create_image(
{self.x},
{self.y},
Expand Down
2 changes: 1 addition & 1 deletion tkdesigner/figma/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_file(self) -> dict:

def get_image(self, item_id) -> str:
response = requests.get(
f"{self.API_ENDPOINT_URL}/images/{self.file_key}?ids={item_id}",
f"{self.API_ENDPOINT_URL}/images/{self.file_key}?ids={item_id}&scale=2",
headers={"X-FIGMA-TOKEN": self.token}
)

Expand Down
5 changes: 4 additions & 1 deletion tkdesigner/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from pathlib import Path
from tkinter import *
# from tkinter import *
# Explicit imports to satisfy Flake8
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
OUTPUT_PATH = Path(__file__).parent
Expand Down Expand Up @@ -42,4 +44,5 @@ def relative_to_assets(path: str) -> Path:
window.resizable(False, False)
window.mainloop()
"""
7 changes: 6 additions & 1 deletion tkdesigner/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Small utility functions.
"""
import requests
from PIL import Image
import io


def find_between(s, first, last):
Expand All @@ -16,5 +18,8 @@ def find_between(s, first, last):

def download_image(url, image_path):
response = requests.get(url)
content = io.BytesIO(response.content)
im = Image.open(content)
im = im.resize((im.size[0] // 2, im.size[1] // 2), Image.ANTIALIAS)
with open(image_path, "wb") as file:
file.write(response.content)
im.save(file)

0 comments on commit c0f270c

Please sign in to comment.