Skip to content

Commit c0f270c

Browse files
authored
Improve Image quality 👾
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.
2 parents cf44120 + 81c3c7e commit c0f270c

File tree

6 files changed

+165
-12
lines changed

6 files changed

+165
-12
lines changed

poetry.lock

Lines changed: 150 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ classifiers = [
2323
[tool.poetry.dependencies]
2424
python = "^3.8"
2525
requests = "2.25.1"
26-
urllib3 = "1.26.4"
2726
Jinja2 = "3.0.1"
27+
Pillow = "^8.3.1"
28+
urllib3 = "^1.26.6"
2829

2930
[tool.poetry.dev-dependencies]
3031
pytest = "^6.2.4"
3132
flake8 = "^3.9.2"
33+
pylint = "^2.9.6"
3234

3335
[tool.poetry.scripts]
3436
tkdesigner = 'tkdesigner.cli:main'

tkdesigner/figma/custom_elements.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from .vector_elements import Vector, Rectangle
22

3-
43
TEXT_INPUT_ELEMENT_TYPES = {
54
"TextArea": "Text",
65
"TextBox": "Entry"
@@ -150,8 +149,7 @@ def __init__(self, node, frame, image_path, *, id_):
150149
def to_code(self):
151150
return f"""
152151
entry_image_{self.id_} = PhotoImage(
153-
file=relative_to_assets("{self.image_path}")
154-
)
152+
file=relative_to_assets("{self.image_path}"))
155153
entry_bg_{self.id_} = canvas.create_image(
156154
{self.x},
157155
{self.y},

tkdesigner/figma/endpoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_file(self) -> dict:
3333

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

tkdesigner/template.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
66
from pathlib import Path
77
8-
from tkinter import *
8+
# from tkinter import *
9+
# Explicit imports to satisfy Flake8
10+
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
911
1012
1113
OUTPUT_PATH = Path(__file__).parent
@@ -42,4 +44,5 @@ def relative_to_assets(path: str) -> Path:
4244
4345
window.resizable(False, False)
4446
window.mainloop()
47+
4548
"""

tkdesigner/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Small utility functions.
33
"""
44
import requests
5+
from PIL import Image
6+
import io
57

68

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

1719
def download_image(url, image_path):
1820
response = requests.get(url)
21+
content = io.BytesIO(response.content)
22+
im = Image.open(content)
23+
im = im.resize((im.size[0] // 2, im.size[1] // 2), Image.ANTIALIAS)
1924
with open(image_path, "wb") as file:
20-
file.write(response.content)
25+
im.save(file)

0 commit comments

Comments
 (0)