Created
June 3, 2025 15:47
-
-
Save Amelia-Wei2/081a713d985ac67e31888ca4f53ebced to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import filedialog | |
import numpy as np | |
import cv2 | |
from sklearn.cluster import KMeans | |
import matplotlib.pyplot as plt | |
import matplotlib.gridspec as gridspec | |
# 初始化全局变量 | |
filename = "" | |
K = 0 | |
# 显示颜色卡片的函数 | |
def show_color_card(): | |
global filename, K | |
if filename == "" or K == 0: | |
print("Please upload an image and set K value.") | |
return | |
# 读取并处理图像 | |
image = cv2.imread(filename) | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 转换为 RGB 格式 | |
imageData = image.reshape(-1, 3) | |
# KMeans 聚类,提取主要颜色 | |
kmeans = KMeans(n_clusters=K) | |
kmeans.fit(imageData) | |
centers = kmeans.cluster_centers_.astype(int) | |
# RGB 转十六进制颜色代码 | |
def rgb_hex(color_rgb): | |
hexRGB = np.vectorize(lambda r, g, b: f'#{r:02x}{g:02x}{b:02x}')(color_rgb[:, 0], color_rgb[:, 1], | |
color_rgb[:, 2]) | |
return hexRGB | |
# 提取颜色的十六进制代码和名称 | |
hexRGB = rgb_hex(centers) | |
# 创建图形并设置网格布局 | |
fig = plt.figure(figsize=(10, 8)) | |
gs = gridspec.GridSpec(2, K, height_ratios=[1, 1], wspace=0.1, hspace=0.01) | |
# 显示原始图像 | |
ax0 = plt.subplot(gs[0, :]) # 第一行,所有列 | |
ax0.imshow(image) | |
ax0.axis('off') | |
# 显示颜色块和对应的颜色名称 | |
for i, color in enumerate(centers): | |
ax = plt.subplot(gs[1, i]) # 第二行,第 i 列 | |
ax.imshow([[color / 255]]) | |
ax.axis('off') | |
# 显示颜色的 HEX 值 | |
ax.text(0.5, -0.5, f"{hexRGB[i]} ", ha='center', va='center', transform=ax.transAxes, fontsize=16) | |
plt.show() | |
# 上传图片并调用颜色提取功能 | |
def upload_image(): | |
global filename | |
filename = filedialog.askopenfilename( | |
title="Select an image file", | |
filetypes=[("Image files", "*.jpg;*.png;*.jpeg;*.bmp")] | |
) | |
print(f"Image selected: {filename}") | |
# 设置颜色数量 K | |
def set_k_value(): | |
global K | |
try: | |
K = int(entry.get()) # 获取用户输入的 K 值(颜色数量) | |
print(f"K value set to: {K}") | |
except ValueError: | |
print("Please enter a valid number for K.") | |
root = tk.Tk() | |
root.title("Make your own colors") | |
root.configure(bg="#e6e6e6") # 设定浅色背景 | |
# 大标签 | |
label_title = tk.Label(root, text="How many main colors do you want to extract?", font=("Segoe UI", 14, "bold"), bg="#c97a44", fg="white") | |
label_title.pack(pady=15) | |
# 输入框 | |
entry = tk.Entry(root, font=("Segoe UI", 12), width=10) | |
entry.pack(pady=5) | |
# 确认按钮 | |
button_set_k = tk.Button(root, text="confirm", bg="#a58c66", fg="white", font=("Segoe UI", 10, "bold"), command=set_k_value) | |
button_set_k.pack(pady=10) | |
# 上传图片按钮 | |
button_upload = tk.Button(root, text="Upload Image", bg="#d1c58e", fg="white", font=("Segoe UI", 12),command=upload_image) | |
button_upload.pack(pady=10) | |
# 生成色卡按钮 | |
button_generate = tk.Button(root, text="Click here\nto produce your own color card", bg="#c97a44", fg="white", font=("Segoe UI", 12, "bold"),command=show_color_card) | |
button_generate.pack(pady=20) | |
root.mainloop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment