cecil commited on
Commit
41b7e31
·
1 Parent(s): f8f58d9
Files changed (5) hide show
  1. Dockerfile +9 -0
  2. README.md +2 -2
  3. app.py +45 -0
  4. requirements.txt +4 -0
  5. thumbor.conf +10 -0
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /app
4
+ COPY . .
5
+
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ EXPOSE 7860
9
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Thumbor
3
  emoji: 📊
4
  colorFrom: indigo
5
  colorTo: red
@@ -8,4 +8,4 @@ pinned: false
8
  short_description: thumbor 是 globo.com 提供的开源照片缩略图服务
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: 获取图片的主色调
3
  emoji: 📊
4
  colorFrom: indigo
5
  colorTo: red
 
8
  short_description: thumbor 是 globo.com 提供的开源照片缩略图服务
9
  ---
10
 
11
+ Use `/color?url=https://your-image.jpg` to get dominant color
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from PIL import Image
3
+ import requests
4
+ from io import BytesIO
5
+ from sklearn.cluster import KMeans
6
+ import numpy as np
7
+
8
+ app = Flask(__name__)
9
+
10
+ def extract_dominant_color(image_url):
11
+ try:
12
+ response = requests.get(image_url)
13
+ img = Image.open(BytesIO(response.content)).convert('RGB')
14
+ img = img.resize((100, 100)) # 降低像素加快处理
15
+ pixels = np.array(img).reshape(-1, 3)
16
+
17
+ kmeans = KMeans(n_clusters=1, random_state=0).fit(pixels)
18
+ dominant_color = kmeans.cluster_centers_[0].astype(int)
19
+ hex_color = '#{:02x}{:02x}{:02x}'.format(*dominant_color)
20
+
21
+ return hex_color
22
+ except Exception as e:
23
+ return str(e)
24
+
25
+ @app.route('/')
26
+ def index():
27
+ return jsonify({
28
+ "message": "Use /color?url=https://your-image.jpg to get dominant color."
29
+ })
30
+
31
+ @app.route('/color')
32
+ def get_color():
33
+ image_url = request.args.get('url')
34
+ if not image_url:
35
+ return jsonify({'error': 'Missing image URL ?url=xxx'}), 400
36
+
37
+ hex_color = extract_dominant_color(image_url)
38
+
39
+ if hex_color.startswith('#'):
40
+ return jsonify({'hex': hex_color})
41
+ else:
42
+ return jsonify({'error': 'Failed to extract color', 'detail': hex_color}), 500
43
+
44
+ if __name__ == '__main__':
45
+ app.run(host='0.0.0.0', port=7860)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ flask
2
+ requests
3
+ pillow
4
+ scikit-learn
thumbor.conf ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # thumbor.conf(只保留必要项)
2
+ QUALITY = 85
3
+ PORT = 7860
4
+ ALLOW_UNSAFE_URL = True
5
+ MAX_AGE = 86400
6
+
7
+ # 开启 dominant_color 插件
8
+ FILTERS = [
9
+ 'thumbor.filters.dominant_color'
10
+ ]