Skip to content

Commit

Permalink
Added palette extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStahL committed Jun 28, 2024
1 parent 4aaf045 commit 6aa5a78
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 47 deletions.
31 changes: 31 additions & 0 deletions imagecolorpicker/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from imagecolorpicker.representation import Representation
from imagecolorpicker.language import Language
from imagecolorpicker.gradienteditor import GradientEditor
from Pylette import extract_colors
from tempfile import TemporaryDirectory
from pathlib import Path


class MainWindow(QMainWindow):
Expand Down Expand Up @@ -113,6 +116,34 @@ def __init__(

self.colorLabel: QLabel

self.actionExtract_Palette: QAction
self.actionExtract_Palette.triggered.connect(self.extractPalette)

def extractPalette(self: Self) -> None:
with TemporaryDirectory() as directory:
imagePath: Path = Path(directory) / 'image.jpg'
self.picker._image.save(str(imagePath))
palette = extract_colors(
image=str(Path(directory) / 'image.jpg'),
palette_size=int(self.colorCountSpinBox.value()),
resize=False,
# mode='MC',
mode='KM',
sort_mode='luminance',
)
palette = list(map(
lambda color: QColor.fromRgb(*color.rgb),
palette,
))
palette = list(sorted(palette, key=lambda color: color.hue()))
for colorIndex in range(int(self.colorCountSpinBox.value())):
index = self.gradientEditor._gradientModel.index(colorIndex, 0)
self.gradientEditor._gradientModel.setData(index, palette[colorIndex])
self.gradientEditor._gradient = index.model()._gradient
self.gradientEditor._allColorMaps = index.model()._allColorMaps
self.gradientEditor.gradientPreview.changeColorMaps(index.model()._allColorMaps)


def degreeChanged(self: Self, value: int) -> None:
ColorGradient.Degree = value
self.gradientEditor._gradientModel.reload()
Expand Down
7 changes: 7 additions & 0 deletions imagecolorpicker/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
</property>
<addaction name="actionCopy"/>
<addaction name="actionPaste"/>
<addaction name="separator"/>
<addaction name="actionExtract_Palette"/>
</widget>
<widget class="QMenu" name="menu">
<property name="title">
Expand Down Expand Up @@ -201,6 +203,11 @@
<string>Update selected</string>
</property>
</action>
<action name="actionExtract_Palette">
<property name="text">
<string>Extract Palette</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
Loading

0 comments on commit 6aa5a78

Please sign in to comment.