python 使用libvips python在8位RGB图像中找到主色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用libvips python在8位RGB图像中找到主色相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

import sys
from gi.repository import Vips

im = Vips.Image.new_from_file(sys.argv[1], access = Vips.Access.SEQUENTIAL)

N_BINS = 10
BIN_SIZE = 256 / N_BINS

# make a 3D histogram of the RGB image ... 10 bins in each axis
hist = im.hist_find_ndim(bins = N_BINS)

# find the position of the maximum
v, x, y = hist.maxpos()

# get the pixel at (x, y)
pixel = hist(x, y)

# find the index of the max value in the pixel
band = pixel.index(v)

print "dominant colour:"
print "   R = ", x * BIN_SIZE + BIN_SIZE / 2
print "   G = ", y * BIN_SIZE + BIN_SIZE / 2
print "   B = ", band * BIN_SIZE + BIN_SIZE / 2
#!/usr/bin/python

import sys
from gi.repository import Vips

N_BINS = 10
BIN_SIZE = 256 / N_BINS

im = Vips.Image.new_from_file(sys.argv[1], access = Vips.Access.SEQUENTIAL)

# turn to lab
im = im.colourspace("lab")

# turn to 8-bit unsigned so we can make a histogram
# use 0 - 255 to be -128 - +127 for a/b
# and 0 - 255 for 0 - 100 L
im += [0, 128, 128]
im *= [255.0 / 100, 1, 1]
im = im.cast("uchar")

# make a 3D histogram of the 8-bit LAB image 
hist = im.hist_find_ndim(bins = N_BINS)

# find the position of the maximum
v, x, y = hist.maxpos()

# get the pixel at (x, y)
pixel = hist(x, y)

# find the index of the max value in the pixel
band = pixel.index(v) 

# scale up for the number of bins
x = x * BIN_SIZE + BIN_SIZE / 2
y = y * BIN_SIZE + BIN_SIZE / 2
band = band * BIN_SIZE + BIN_SIZE / 2

# turn the index back into the LAB colour
L = x * (100.0 / 255)
a = y - 128
b = band - 128

print "dominant colour:"
print "   L = ", L
print "   a = ", a
print "   b = ", b

以上是关于python 使用libvips python在8位RGB图像中找到主色的主要内容,如果未能解决你的问题,请参考以下文章

支持的 libvips CLI 输出到 Windows 标准输出的格式

libvips从Ex40中提取NDPI区域而不是从地图中提取

Ubuntu 16.04 LTS 安装libvips出现”Package vips was not found in the pkg-config search path”

sh libvips 7.42.x跨平台简单安装程序脚本(支持OSX,Debian,Ubuntu,CentOS,Fedora,Amazon Linux)

sh libvips 7.42.x跨平台简单安装程序脚本(支持OSX,Debian,Ubuntu,CentOS,Fedora,Amazon Linux)

Python 3.8.0 教程 —— 2. 使用Python解释器