# Colorfy
A small program written in Python 3 that sets the color of your LED-strip to the background color that Spotify sets when playing on a Chromecast. It analyzes the album artwork and computes the correct background color (same as Spotify sets) in about 80 % of the cases.
The program uses k-means clustering to find distinct colors in the artwork and then computes a colorfulness index as defined by [Hasler and Süsstrunk (2003)](https://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf) for each of the colors. The color with the highest index is then set to the LED-strip if it is greater than or equal to a given colorfulness tolerance.
## Images
### Spotify background color
<img src="images/rhcp.jpg">
<p float="center">
<img src="images/color_gradient.gif" width="481" height="271"/>
<img src="images/5sos.jpg" width="409" height="271"/>
</p>
<img src="images/khalid.jpg">
<img src="images/falco.jpg">
### Web app
Simple web app built with Flask to easily switch between setting your LED-strip to the color based on your current Spotify playback, set manually using a color picker or if it should be turned off.
<p float="center">
<img src="images/web_app_home.png" width="445" height="890"/>
<img src="images/web_app_manual.png" width="445" height="890"/>
</p>
## Running locally
### Connect LED-strip to Raspberry Pi
Many different LED-strips can be used with this program. The only requirement is that it needs to connect with three data pins to the Raspberry Pi, i.e. one data pin for each of the color channels R, G and B.
I used the [IKEA Dioder](https://www.ikea.com/us/en/catalog/categories/series/25230/) LED-strip which I connected to my Raspberry Pi 3 model B+ by following [this](https://dordnung.de/raspberrypi-ledstrip/) guide.
For information about how to wire and set up WS281X leds you can refer to [this](https://tutorials-raspberrypi.com/connect-control-raspberry-pi-ws2812-rgb-led-strips/) guide but don't run the setup commands there as the required library is now installed by using `pip` rather than the manual way in the guide. You can also find the library used [here](https://github.com/jgarff/rpi_ws281x).
### Clone/download repository
When the hardware is set up, you will need to [download](https://github.com/davidkrantz/Colorfy/archive/master.zip) or clone the repository, the latter can be done by typing
```
git clone https://github.com/davidkrantz/Colorfy.git
```
in your terminal.
### Create a Spotify application
1. Go to your [Spotify dashboard](https://developer.spotify.com/dashboard/applications).
2. Create a new application.
3. Press *EDIT SETTINGS* and set the redirect URI to for example `http://localhost/` and save it.
### Set environment variables
These are found inside your Spotify application and are preferably added to `~/.profile` (or something similar like `~/.bash_login` or `~/.bashrc`) on your Raspberry Pi.
```
export SPOTIPY_CLIENT_ID=''
export SPOTIPY_CLIENT_SECRET=''
export SPOTIPY_REDIRECT_URI=''
```
### Get refresh token
1. Run the included `spotify_setup.py` script with a string as argument. This can be any string, but preferably something your will remember (for example your Spotify username to which you wish to connect the Spotify application to) since this will make it easy to find the generated refresh token if you lose it. So for example:
```
python spotify_setup.py spotify_username
```
2. A page will open. Accept the terms and then copy the URL you are redirected to into the terminal and hit enter.
3. Your refresh token will then be printed in the terminal. It will also be saved in a file called `.cache-{spotify_username}` inside the active directory.
4. Set the refresh token as an environment variable just as the ones before with `export SPOTIPY_REFRESH_TOKEN=''`.
### Set up config file
1. Copy and rename `config.ini.default` to `config.ini`.
2. Edit `config.ini` such that the used GPIO pins and Chromecast name matches yours, for example:
```
[GPIO PINS]
red_pin = 17
green_pin = 22
blue_pin = 24
[CHROMECAST]
name = Chromecast Krantz
```
To use a WS281X led strip (Neopixels) you need to set the `is_active` value in `config.ini` to `True`, the `led_count` value to the number of leds in your strip and the `led_pin` to the GPIO pin you connected the data input of your led strip to. The other values under `[WS281X]` are optional and set as default.
### Run it
1. First you will have to install the needed packages. These are listed in the `requirements.txt` file and *should* be easily installed using `pip` with
```
pip3 install -r /path/to/requirements.txt
```
**NOTE:** The Spotipy package needs to be installed directly from GitHub, otherwise an old version will be installed
```
pip3 install git+https://github.com/plamere/spotipy.git --upgrade
```
2. Initialize `pigpiod` on your Raspberry Pi with `sudo pigpiod`
3. Run `main.py` together with some suitable arguments, for example
```
python3 main.py -k 8 -t 10 -s 100 100
```
which will resize the album artworks to `100x100`, find `8` distinct colors and return the most colorful color if it is greater than or equal to the colorfulness tolerance `10`. If no arguments are inputted `python3 main.py`, the default values will be used. The default values are the arguments which gave me the best result with regards to accuracy and computational time, which is why I recommend using them. But feel free to experiment with these to try to improve the accuracy!
## Starting and updating on reboot
The two previous steps can be automated by doing the following:
1. Run `sudo systemctl enable pigpiod` once on your Raspberry Pi.
2. Copy and rename `start.sh.default` to `start.sh`.
3. Edit `start.sh` to your liking and such that it works on your Raspberry Pi.
4. Add `@reboot . $HOME/path/to/env/var; sh /path/to/project/start.sh > /path/to/project/logs/log.txt 2>&1` to `crontab -e`. For the logging to work you will have create a directory with `mkdir logs` inside the project folder. A file called `log.txt` in `/path/to/project/logs` will then contain the logs which can be used for debugging.
If you choose to run the web server, it can later be found on the address `IPTOYOURRPI:5000`.
没有合适的资源?快使用搜索试试~ 我知道了~
一个用Python 3编写的小程序,可将LED灯条的颜色设置为Spotify在Chromecast上播放时设置的背景色。 它分析
共27个文件
py:7个
html:4个
jpg:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 104 浏览量
2023-09-25
14:23:14
上传
评论
收藏 47.91MB ZIP 举报
温馨提示
一个用Python 3编写的小程序,可将LED灯条的颜色设置为Spotify在Chromecast上播放时设置的背景色。 它分析专辑插图并在大约80%的情况下计算正确的背景颜色(与Spotify集相同)。__该程序使用k均值聚类在艺术品中找到不同的颜色,然后为每种颜色计算由定义的色彩指数。 如果具有最大索引的颜色大于或等于给定的色彩公差,则将其设置为LED带.zip。 它分析专辑插图并在大约80%的情况下计算正确的背景颜色(与Spotify集相同)。__该程序使用k均值聚类在艺术品中找到不同的颜色,然后为每种颜色计算由定义的色彩指数。 如果具有最大索引的颜色大于或等于给定的色彩公差,则将其设置为LED带.zip
资源推荐
资源详情
资源评论
收起资源包目录
一个用Python 3编写的小程序,可将LED灯条的颜色设置为Spotify在Chromecast上播放时设置的背景色。 它分析专辑插图并在大约80%的情况下计算正确的背景颜色(与Spotify集相同)。__该程序使用k均值聚类在艺术品中找到不同的颜色,然后为每种颜色计算由定义的色彩指数。 如果具有最大索引的颜色大于或等于给定的色彩公差,则将其设置为LED带.zip (27个子文件)
T
Colorfy-master
config.ini.default 742B
app.py 3KB
main.py 4KB
LICENSE 1KB
spotify_background_color.py 5KB
templates
manual.html 980B
off.html 528B
spotify.html 613B
index.html 531B
current_spotify_playback.py 5KB
led_controller.py 3KB
requirements.txt 139B
spotify_setup.py 648B
.gitignore 90B
images
5sos.jpg 8.95MB
falco.jpg 6.95MB
color_gradient.gif 13.85MB
web_app_home.png 87KB
rhcp.jpg 9.45MB
khalid.jpg 8.48MB
web_app_manual.png 255KB
start.sh.default 596B
static
js
iro.js 13KB
color.js 878B
css
layout.css 1KB
README.md 6KB
ws281x_controller.py 3KB
A
共 27 条
- 1
资源评论
处处清欢
- 粉丝: 158
- 资源: 2511
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功