You are not logged in.

#1 2018-04-15 19:54:55

Cristhian
Member
Registered: 2018-02-17
Posts: 76

How to detect dominant color on screen [SOLVED]

Hi to everyone, I recently bought the Xiaomi Yeelight bulb  and I want to make a script that detects colors on screen (while I'm watching films for example) and change the color of the bulb.
My problem is "just" about the first part, how to detect dominant color on the screen.
I found this python script, but it does not works.
This is the result of the command :

 yeelight-cinema.py

  File "/usr/bin/yeelight-cinema.py", line 36
    print "invalid resolution '{0}'. Use WIDTHxHEIGHT format (for example 1920x1080)".format(resolution_str)

Do you have any idea what kind of wiki can help me in doing this? Thanks a lot for helping.

Last edited by Cristhian (2018-04-23 21:21:09)

Offline

#2 2018-04-15 20:12:56

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,808
Website

Re: How to detect dominant color on screen [SOLVED]

the error message tells exactly what the problem is


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#3 2018-04-15 20:25:55

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

I'm sorry I did not post the link of the script:
https://github.com/acrap/yeelight-cinema

I tried also with the arguments , but there is still the same error.

Offline

#4 2018-04-15 20:36:00

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,808
Website

Re: How to detect dominant color on screen [SOLVED]

I would make an issue on their GitHub. But for clarity post the entire command you used and full output of that command.


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#5 2018-04-15 20:42:13

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

ugjka wrote:

I would make an issue on their GitHub. But for clarity post the entire command you used and full output of that command.

The commands I tried are:

yeelight-cinema.py
yeelight-cinema.py --bulb_ip=my_public_ip --resolution_l=1920x1080
yeelight-cinema.py --bulb_ip=192.168.1.240 --target_screen=right --resolution_l=1920x1080 --resolution_r=1920x1080

And every command produced the same output , that is:

 File "/usr/bin/yeelight-cinema.py", line 36
    print "invalid resolution '{0}'. Use WIDTHxHEIGHT format (for example 1920x1080)".format(resolution_str)
                                                                                    ^
SyntaxError: invalid syntax

Offline

#6 2018-04-15 20:59:47

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,808
Website

Re: How to detect dominant color on screen [SOLVED]

I don't know python, but Syntax error probably means something is wrong with the program itself


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#7 2018-04-15 21:13:42

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,532
Website

Re: How to detect dominant color on screen [SOLVED]

That script seems rather involved.  This would get the most common color on the screen:

import -window root -format %c histogram:info: | awk '{if ($1 > max) {max = $1; col=$0;}} END {print col;}'

Depending on what format you want it in, you could change the END clause to print just what you needed.  You may also want to use `-colors NNN` on the import command to have imagemagic reduce the number of colors which will combine close colors into a single color so a lot of varied shades or red can be counted together to give an output of red rather than the strictly most common pixel color.

As for the python errors, there may be others, but at least one is that the script shebang is just "python" which on archlinux is python3, but that is python2 code.  At very least, you'd need to call it via python2 or change the shebang.

Last edited by Trilby (2018-04-15 21:15:20)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2018-04-16 16:07:30

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Thank you very much ! Can you link me a guide to learn how to use this staff in bash scripts? Because I don't want just to copy your solution, I want to understand how it works and change it if necessary.

Offline

#9 2018-04-16 18:26:28

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,532
Website

Re: How to detect dominant color on screen [SOLVED]


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2018-04-16 20:22:13

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

I think imagemagick  will take too much time and there will be a big delay between the frame and the result on the bulb .
Thank you so much for everything! I'va just something else to ask you... I can't find guides on how to use the function ( if that is a function) "import"

Offline

#11 2018-04-16 20:35:20

ooo
Member
Registered: 2013-04-10
Posts: 1,638

Re: How to detect dominant color on screen [SOLVED]

import is a program, not function.
It has a man page, as command line programs often do. See

man import

Offline

#12 2018-04-16 20:39:29

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Thank you so much, I'm so sorry !! Thanks to be patient!

Offline

#13 2018-04-16 20:50:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,532
Website

Re: How to detect dominant color on screen [SOLVED]

Cristhian wrote:

I think imagemagick  will take too much time...

You think, of your measured?  I'd bet imagemagick will be faster than any other option you find.

EDIT: I double down on the above bet as the python script you linked to is a wrapper around another python wrapper around imagemagic.

Last edited by Trilby (2018-04-16 20:53:31)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#14 2018-04-16 21:02:34

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Trilby wrote:
Cristhian wrote:

I think imagemagick  will take too much time...

You think, of your measured?  I'd bet imagemagick will be faster than any other option you find.

EDIT: I double down on the above bet as the python script you linked to is a wrapper around another python wrapper around imagemagic.

I did not try, but I red on the guide of the python script , that it was very slow. Yes, maybe beacuse is a double wrapper, but I used imagemagick in the past, to take a screenshot and make a blur effect to use the image as lock screen and this method take a lot of time.
Anyway I tried the script you wrote me before and I don't know why (maybe because I'm using i3wm) it does not work until i press F11 to full screen a window

Offline

#15 2018-04-16 21:15:38

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,532
Website

Re: How to detect dominant color on screen [SOLVED]

What does "does not work" mean?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#16 2018-04-17 10:29:39

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,808
Website

Re: How to detect dominant color on screen [SOLVED]

I played with some go packages out there (first results on google)

package main

import (
	"fmt"
	"time"

	"github.com/EdlinOrg/prominentcolor"
	"github.com/vova616/screenshot"
)

func main() {
	now := time.Now()
	img, err := screenshot.CaptureScreen()
	if err != nil {
		panic(err)
	}
	items, err := prominentcolor.Kmeans(img)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Latency: %s\n", time.Now().Sub(now))
	fmt.Printf("Dominant color: #%s\n", items[0].AsString())
}

result:

[ugjka@archee dominantC]$ go run main.go 
Latency: 210.679072ms
Dominant color: #ECE8EA

200 ms latency seems bad, but I hacked this together in 5 minutes .
Also the screenshotting package seems to take both of my screens as one image, but it seem you can specify a specific rectangle if you want.
prominentcolor package has lots of tunables, that I haven't touched

Edit: it seems that screenshotting takes the most time 186ms while processing the image is only 40ms

Edit2: I got the overall latency down 100ms by going into lower level stuff and I'm currently adding the ability to select screens via xinerama
I will post the code later after I clean it up

Last edited by ugjka (2018-04-17 12:41:02)


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#17 2018-04-17 14:22:33

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

I just want to say thank you for helping!! To be honest I really don't know anything about packages , awk scripts etc etc. I don't know how to run your code, but I suppose google has the answer

Offline

#18 2018-04-17 14:28:00

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Trilby wrote:

What does "does not work" mean?

It means that the final color is not the dominant color. Also with windows where white is the dominant color like in chrome tabs , the output is something like rgb(0,17,16) and similar, that are dark colors. But if i press F11 it seems to work pretty good and if I try images of red or white or green at full screen, the scripts works good and give me directly the name of the color beacuse it recognise that is pure color rgb.

Offline

#19 2018-04-17 15:08:47

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

I installed go and go-tools. I've created a program with your script and the output is :

hello.go:7:2: cannot find package "github.com/EdlinOrg/prominentcolor" in any of:
        /usr/lib/go/src/github.com/EdlinOrg/prominentcolor (from $GOROOT)
        /home/sirth/go/src/github.com/EdlinOrg/prominentcolor (from $GOPATH)
hello.go:8:2: cannot find package "github.com/vova616/screenshot" in any of:
        /usr/lib/go/src/github.com/vova616/screenshot (from $GOROOT)
        /home/sirth/go/src/github.com/vova616/screenshot (from $GOPATH)

Offline

#20 2018-04-17 15:27:25

Morn
Member
Registered: 2012-09-02
Posts: 886

Re: How to detect dominant color on screen [SOLVED]

I think you need to run "go get" to install the dependencies first.

Offline

#21 2018-04-17 15:47:02

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Thank you , this is the output:

 go run xiaomi.go
Latency: 20.03899ms
Dominant color: #404451

Tonight I will try to ad a little part of bash script to extract di hex color and send it to the Yeelight.

PS: With full screen the colors seems to be perfect. Do you know how the scripts works in terms of area or selected window? Beacuse I want to know how to adapt this program to i3wm and its tiled and floating windows

Offline

#22 2018-04-17 15:52:10

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,808
Website

Re: How to detect dominant color on screen [SOLVED]

Full program...  https://github.com/ugjka/yeelight-cinema-go

PKGBUILD https://github.com/ugjka/yeelight-cinem … x/PKGBUILD

Usage:

yeelight-cinema-go -addr 192.168.1.1 -port 55443 -head 0

-head is your primary display, if you have just one monitor leave it to 0
addr and port are your yeelight settings, adjust accordingly

I wrote this without having access to yeelight lightbulb myself, so it is probably broken lol
but in theory it should work


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#23 2018-04-17 15:59:38

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Thank you ugjka!!! I will try tonight !! But just in case it will fail, I think the first solution with Go, will be perfect. I've just to extract the hex color , but is very simple, I think to use the command cut .

Offline

#24 2018-04-17 16:16:08

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,808
Website

Re: How to detect dominant color on screen [SOLVED]

The first solution is suboptimal if you have more than one monitor, let me know if you need to have a version of it where you can select a monitor

Edit: for the record here is the first version that just prints the prominent color of selected monitor

package main

import (
	"flag"
	"fmt"
	"image"
	"os"

	"github.com/BurntSushi/xgb"
	"github.com/BurntSushi/xgb/xinerama"
	"github.com/BurntSushi/xgb/xproto"
	"github.com/EdlinOrg/prominentcolor"
)

func main() {
	headFlag := flag.Int("head", 0, "select monitor")
	flag.Parse()
	c, err := xgb.NewConn()
	if err != nil {
		panic(err)
	}

	err = xinerama.Init(c)
	if err != nil {
		panic(err)
	}

	reply, err := xinerama.QueryScreens(c).Reply()
	if err != nil {
		panic(err)
	}
	if len(reply.ScreenInfo) < *headFlag+1 {
		fmt.Fprintf(os.Stderr, "%d %s\n", *headFlag, "head out of range!")
		os.Exit(1)
	}
	head := reply.ScreenInfo[*headFlag]

	screen := xproto.Setup(c).DefaultScreen(c)
	x0 := head.XOrg
	y0 := head.YOrg
	//now := time.Now()
	xImg, err := xproto.GetImage(c, xproto.ImageFormatZPixmap, xproto.Drawable(screen.Root), x0, y0, head.Width, head.Height, 0xffffffff).Reply()
	if err != nil {
		panic(err)
	}

	data := xImg.Data
	for i := 0; i < len(data); i += 4 {
		data[i], data[i+2], data[i+3] = data[i+2], data[i], 255
	}
	img := &image.RGBA{data, 4 * int(head.Width), image.Rect(0, 0, int(head.Width), int(head.Height))}
	items, err := prominentcolor.Kmeans(img)
	if err != nil {
		panic(err)
	}
	//fmt.Printf("\rLatency: %s\n", time.Now().Sub(now))
	fmt.Printf("#%s\n", items[0].AsString())

}

use -head to select the monitor you want -head 0 is your primary monitor

Last edited by ugjka (2018-04-17 16:32:45)


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#25 2018-04-17 16:34:30

Cristhian
Member
Registered: 2018-02-17
Posts: 76

Re: How to detect dominant color on screen [SOLVED]

Yep I'll let you know after all my tests tonight! But  is there a simple way to display just the hex color ? I tried to edit the program , but I failed

Offline

Board footer

Powered by FluxBB