Python将图像转换为使用更少的颜色 [英] Python converting an image to use less colors

查看:337
本文介绍了Python将图像转换为使用更少的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拍摄一张图像,并(以某种方式)将其读取为像素阵列。这意味着2d数组的每个元素将是代表该像素颜色的十六进制代码或RGB 3元组。



我研究了图像处理,发现了Pillow或SciPy之类的东西,但我只发现过分简单的事情,例如添加滤镜或更改图像的常规颜色属性(将每个像素的读取值和蓝色值乘以0.3-0.5,同时将绿色值乘以1,则通常使绿色更绿(实际上保持不变)。我需要做的是能够根据颜色分别检查每个像素。



之后,我需要将图像转换为使用较少颜色的图像,(像4或8)。我认为最好的方法是为这些所需的颜色定义一些阈值,并且当像素的颜色在所定义的颜色的某个范围内时,像素将获得相应的颜色。



我也没有从实际信息或试图做同样事情的人身上找到太多有关的东西。



<我想询问有关此类问题的任何信息或资源:
-要使用的图书馆(图书馆)?
-方法?(是否存在针对这种问题的广泛使用的算法?)
-我使用的编程语言是否错误?(是否有提供这种功能但更易于使用的语言?)



任何种类的帮助或信息将不胜感激,谢谢您!



编辑:我有找到



以下代码将颜色数量从+ 500K减少到仅6种:

 从skimage import io导入numpy as np 
sklearn.cluster import KMeans

原始= io.imread('https://i.stack.imgur.com/QCl8D.jpg')
n_colors = 6

arr = original.reshape((-1,3 ))
kmeans = KMeans(n_clusters = n_colors,random_state = 42).fit(arr)
标签= kmeans.labels_
中心= kmeans.cluster_centers_
less_colors =中心[标签] .reshape(original.shape).astype('uint8')

io.imshow(less_colors)

这是彩色量化图像的外观:




I want to take an image and (somehow) read it as an array of pixels. Meaning each element of the 2d array would be either a hex code or RGB 3-tuple that represent the color of that pixel.

I have looked into image processing and found things like Pillow or SciPy but I only found overly simple things such as adding a filter or changing the general color properties of the image(making it generally greener by multiplying the read and blue values of every pixel by something like 0.3 - 0.5 while multiplying the green value by 1, effectively keeping it the same). What I need to do is to be able to examine every pixel individually based on its color.

After that I need to convert the image into one that uses less colors, (something like 4 or 8). I think the best way to do this would be to define some "thresholds" for these desired colors and when the color of a pixel is within the range of a certain color from those defined, then the pixel gets that respective color.

I have also not found too much about this, both in the way of actual information or people trying to do the same.

I would like to ask for any information or resources regarding to this kind of problem: - Library(libraries) to use? - Methods?(are there any widely used algorithms for this kind of problem?) - Am I using the wrong programming language?(is there one that offers this kind of functionality but easier to use?)

Any kind of help or information would be greatly appreciated, thank you in advance!

EDIT: I have found this question that refers to getting the values of a pixel, but I still need to split them into less colors.

解决方案

Library (libraries) to use?

scikit-image or OpenCV would be my preferred choices.

Methods? (Are there any widely used algorithms for this kind of problem?)

K-means clustering is a popular approach to color quantization.

Am I using the wrong programming language? (Is there one that offers this kind of functionality but easier to use?)

Python is arguably the "easiest" language for this task.

DEMO

Consider this image:

The following code reduces the number of colors from +500K to only 6:

import numpy as np
from skimage import io
from sklearn.cluster import KMeans

original = io.imread('https://i.stack.imgur.com/QCl8D.jpg')
n_colors = 6

arr = original.reshape((-1, 3))
kmeans = KMeans(n_clusters=n_colors, random_state=42).fit(arr)
labels = kmeans.labels_
centers = kmeans.cluster_centers_
less_colors = centers[labels].reshape(original.shape).astype('uint8')

io.imshow(less_colors)

And this is how the color quantized image looks:

这篇关于Python将图像转换为使用更少的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆