Image.Colors OpenBI
The dominant colours of a picture, as a table: rank, colour, name, how many pixels and their share.
Group: Images and colours. OpenBI’s own: Power Query M has no function of this name.
Syntax
Image.Colors(image as any, optional options as nullable record) as table
| Parameter | Type | Description |
|---|---|---|
image | any | The image's bytes (File.Contents, Web.Contents), or text: an http:// or https:// address, a data: URI or base 64. |
options | nullable record optional | Count (how many colours, 1 to 50; 9), ExcludeWhite and ExcludeBlack (leave near-white and near-black pixels out; both true), WhiteTolerance and BlackTolerance (how near, 0 to 255; 20). |
Returns
table. A row per colour, the most pixels first: Rank, Color (written #RRGGBB), its Name, its Red, Green and Blue, Pixels and Share; null for a null image.
Remarks
- The pixels are grouped by k-means into as many colours as Count asks for (fewer when the picture has fewer), and each group's average colour gets the name of the closest colour in a list of named colours (CSS's and more, wine colours among them). Transparent pixels never count.
- Share is of the pixels counted: those left out as white or black don't count.
- JPEG, PNG, GIF and BMP pictures are read.
- In
Table.AddColumnover a table of picture addresses, the pictures are fetched and their colours found several at a time, then each row reads its own.Table.ExpandTableColumnmakes a row per colour.
Compared with Power Query
An OpenBI function: Power Query has none like it, so a query that uses it runs in OpenBI only, not in Power BI Desktop or the Power BI service.
Examples
A swatch's colours
The picture is 10 by 10 pixels: 60 wine red, 30 gold and 10 white.
Image.Colors(File.Contents("C:\Data\swatch.png"))
| Rank | Color | Name | Red | Green | Blue | Pixels | Share |
|---|---|---|---|---|---|---|---|
| 1 | #7B2D3B | wine | 123 | 45 | 59 | 60 | 0.6666666666666666 |
| 2 | #D4A017 | goldenrod | 212 | 160 | 23 | 30 | 0.3333333333333333 |
The white pixels are left out, so the two colours share what is left: two thirds and one third.
White counted too
Image.Colors(File.Contents("C:\Data\swatch.png"), [ExcludeWhite = false, Count = 3])
| Rank | Color | Name | Red | Green | Blue | Pixels | Share |
|---|---|---|---|---|---|---|---|
| 1 | #7B2D3B | wine | 123 | 45 | 59 | 60 | 0.6 |
| 2 | #D4A017 | goldenrod | 212 | 160 | 23 | 30 | 0.3 |
| 3 | #FFFFFF | white | 255 | 255 | 255 | 10 | 0.1 |
Related functions
Image.Color, Color.Name, File.Contents, Table.ExpandTableColumn.