-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport_assistant.py
60 lines (50 loc) · 1.99 KB
/
export_assistant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from PyQt5.QtWidgets import QFileDialog
from PIL import Image
def export_format(fileending):
# check if the fileending is a picture
picture_endings = [".png", ".jpg", ".bmp"]
if fileending in picture_endings:
return "picture"
# add checks for other files here
def get_path():
# gets the path where to save and adds the appropriate fileending
# if it is not there
saveable_formats = """Png (*.png);;Jpg (*.jpg);;
Bmp (*.bmp)"""
file = QFileDialog.getSaveFileName(
None, caption="Export colors",
filter=saveable_formats)
# filename[1][6:-1]
fileending = file[1][6:-1]
filepath = file[0]
if not filepath.endswith(fileending):
filepath += fileending
file_type = export_format(fileending)
return filepath, file_type
def remove_alpha(colors):
# doest "remove" the alpha channel but 255 is not transparent
for color in colors:
color.setAlpha(255)
def save_picture(path, settings, colors):
# gets called when its known that the palette should be saved as a picture
# sorts the qcolors by theyre hsv value (mostly by hue)
colors.sort(key=lambda qcolor: qcolor.getHsv())
palette = Image.new("RGB", size=(len(colors), 1))
# pretty much useless
if settings["alpha"] is False:
remove_alpha(colors)
# puts one pixel text to the other; you get a pixelstrip
for index, qcolor in enumerate(colors):
palette.putpixel((index, 0), qcolor.getRgb())
# the strip gets independently resized after its creation
if settings["resize"][0] is True:
_, width, height = settings["resize"]
palette = palette.resize((width, height), resample=Image.NEAREST)
elif settings["scale"][0] is True:
factor = settings["scale"][1]
palette = palette.resize(
(palette.width * factor, palette.height * factor),
resample=Image.NEAREST)
# platte.save() when developing; palette.save() when shipping
# palette.show()
palette.save(path)