- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个支持 4 色图形的设备(很像过去的 CGA)。
我想使用 PIL读取图像并使用我的 4 色调色板(红色、绿色、黄色、黑色)转换它,但我不知道它是否可能。我发现一些邮件列表存档帖子似乎暗示其他人已经尝试这样做但失败了。
一个简单的 python 示例将不胜感激!
如果你添加一些东西,然后将图像转换为字节字符串,其中每个字节代表 4 个像素的数据(每两位代表从 0 到 3 的颜色)
最佳答案
首先:您的四种调色板(黑色、绿色、红色、黄色)没有蓝色成分。因此,您必须接受您的输出图像将很难接近输入图像,除非没有蓝色组件开始。
试试这段代码:
import Image
def estimate_color(c, bit, c_error):
c_new= c - c_error
if c_new > 127:
c_bit= bit
c_error= 255 - c_new
else:
c_bit= 0
c_error= -c_new
return c_bit, c_error
def image2cga(im):
"Produce a sequence of CGA pixels from image im"
im_width= im.size[0]
for index, (r, g, b) in enumerate(im.getdata()):
if index % im_width == 0: # start of a line
r_error= g_error= 0
r_bit, r_error= estimate_color(r, 1, r_error)
g_bit, g_error= estimate_color(g, 2, g_error)
yield r_bit|g_bit
def cvt2cga(imgfn):
"Convert an RGB image to (K, R, G, Y) CGA image"
inp_im= Image.open(imgfn) # assume it's RGB
out_im= Image.new("P", inp_im.size, None)
out_im.putpalette( (
0, 0, 0,
255, 0, 0,
0, 255, 0,
255, 255, 0,
) )
out_im.putdata(list(image2cga(inp_im)))
return out_im
if __name__ == "__main__":
import sys, os
for imgfn in sys.argv[1:]:
im= cvt2cga(imgfn)
dirname, filename= os.path.split(imgfn)
name, ext= os.path.splitext(filename)
newpathname= os.path.join(dirname, "cga-%s.png" % name)
im.save(newpathname)
这将创建一个 PNG 调色板图像,其中只有前四个调色板条目设置为您的颜色。此示例图片:
成为
获取 image2cga
的输出(产生一个 0-3 值的序列)并将每四个值打包到一个字节是微不足道的。
如果您需要有关代码功能的帮助,请询问,我会解释。
当然,事实证明我太热情了——正如 Thomas 发现的那样——Image.quantize 方法可以将调色板图像作为参数并进行量化,结果比我上面的临时方法好得多:
def cga_quantize(image):
pal_image= Image.new("P", (1,1))
pal_image.putpalette( (0,0,0, 0,255,0, 255,0,0, 255,255,0) + (0,0,0)*252)
return image.convert("RGB").quantize(palette=pal_image)
对于“附加值”,下面是生成打包字符串的代码(每字节 4 个像素):
import itertools as it
# setup: create a map with tuples [(0,0,0,0)‥(3,3,3,3)] as keys
# and values [chr(0)‥chr(255)], because PIL does not yet support
# 4 colour palette images
TUPLE2CHAR= {}
# Assume (b7, b6) are pixel0, (b5, b4) are pixel1…
# Call it "big endian"
KEY_BUILDER= [
(0, 64, 128, 192), # pixel0 value used as index
(0, 16, 32, 48), # pixel1
(0, 4, 8, 12), # pixel2
(0, 1, 2, 3), # pixel3
]
# For "little endian", uncomment the following line
## KEY_BUILDER.reverse()
# python2.6 has itertools.product, but for compatibility purposes
# let's do it verbosely:
for ix0, px0 in enumerate(KEY_BUILDER[0]):
for ix1, px1 in enumerate(KEY_BUILDER[1]):
for ix2, px2 in enumerate(KEY_BUILDER[2]):
for ix3, px3 in enumerate(KEY_BUILDER[3]):
TUPLE2CHAR[ix0,ix1,ix2,ix3]= chr(px0+px1+px2+px3)
# Another helper function, copied almost verbatim from itertools docs
def grouper(n, iterable, padvalue=None):
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
return it.izip(*[it.chain(iterable, it.repeat(padvalue, n-1))]*n)
# now the functions
def seq2str(seq):
"""Takes a sequence of [0..3] values and packs them into bytes
using two bits per value"""
return ''.join(
TUPLE2CHAR[four_pixel]
for four_pixel in grouper(4, seq, 0))
# and the image related function
# Note that the following function is correct,
# but is not useful for Windows 16 colour bitmaps,
# which start at the *bottom* row…
def image2str(img):
return seq2str(img.getdata())
关于python - 如何使用 Python 图像库将任何图像转换为 4 色调色板图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/236692/
是否可以更改 Matplotlib 在为图形线条生成自己的颜色时循环使用的颜色?我正在使用 pylab 模块。 from pylab import * import matplotlib.cm as
R中是否有可以处理许多数据类的调色板的好包?我有 16 个连续数据类。我试过 RColorBrewer 但它最多有 9 个数据类,我也试过 Rainbow() 但它对顺序数据没有吸引力。谢谢 最佳答案
一些与 previous question 相关的内容 我想采用 gnuplot 的默认(pm3d 默认)调色板,并在 X 处放置一个白色值,并将任何 >=X 的内容设置为白色,但其余部分( 例如,我
我正在尝试使用调用 Web 服务的结果(这是一个分段列表)来填充 Tapestry 5 中的调色板组件。 Segment 是一个普通的类,具有一些属性,如标题...我想将标题显示为可用、选定的调色板列
我最近在 Eclipse 4.3 (Kepler) 上安装了 windowBuilder 工具。当我尝试在布局设计器上进行一些工作时,我的调色板窗口上不会出现任何组件。只有一些组件,如“布局 ->布局
我计划从我的 RecyclerView 中的每个 bitmap 中获取每一种鲜艳的颜色,并将其设置为我的文本的背景。所以我尝试了以下: Picasso.with(ctx)
如何使用D3 Palettes在 Bokeh ?我尝试以这种方式导入,但收到未解析的引用错误消息 from bokeh.palettes import Category20 Bokeh 版本: pri
Seaborn 允许定义包含多种颜色的调色板,这对具有多行的图表很有用。但是,当将调色板设置为具有多种颜色的调色板时,仅使用前六种颜色,之后颜色会循环使用,因此很难区分线条。这可以通过显式调用调色板来
实际上我正在开发一个包含 Material 设计抽屉导航的 android 应用程序,我想每 5 分钟动态更改状态栏、操作栏、文本颜色的颜色。实际上我正在使用支持库主题来支持所有 android手机。
我为一个项目编写了一些自定义的复合小部件,并将它们编译成一个单独的 jar 文件。我将这个 jar 文件添加到 GWT 项目的构建路径中。当我尝试将小部件添加到调色板时(通过按“选择组件”按钮),我可
我在缓冲区(类型 - 长型)中有一个来自 32 位扫描仪的图像数据。 例如buffer[0]对应的像素值为952即[184, 3, 0, 0] <-[R,G,B,A]; 我想在屏幕上显示/绘画/绘图;
我的公司有一个正式的调色板,所以我需要在我的 seaborn 图表中使用这些颜色。因此,我想设置默认的 seaborn 调色板,并为这些颜色提供易于使用的名称,例如“p”代表紫色,“g”代表绿色。 这
我试图通过一个主要是数字的因素为 ggplot 着色,如下所示: iris %>% ggplot(aes(Sepal.Length, Sepal.Width, color = cut(Petal.
我在 Kubuntu 12.04 上使用 Netbeans 7。我这样启动 Netbeans: netbeans --cp:a /usr/share/java/xercesImpl.jar & Net
我已经用我选择的颜色制作了颜色图,但是我想将其转换为可用于“调色”seaborn 绘图的调色板。这可能吗?如果可能的话,怎么可能? 我用过... cmap = pl.colors.LinearSegm
我需要动态生成自定义调色板来为我的 Angular 应用配置主题。 可用的默认 Material 调色板:https://material.io/guidelines/style/color.html
首先,我从未使用过 Palette,当我想开始使用它时,我看到所有教程和博客都在谈论 v21 而不是 v22。 我的问题是我有一个 Swatch(在 swatch 数组 [palette.getSwa
我如何使用此工具中没有的颜色构建自定义 MDL:https://getmdl.io/customize/index.html 我想要那些颜色: 原色:红色 400 (#EF5350) 二次色:琥珀色
如何知道调色板png是否带有alpha?我获取有关图像 png_get_IHDR 的信息 之后我查看color_type - PNG_COLOR_TYPE_PALETTE 但是我找不到如何知道这个 p
有没有一种简单的方法可以将真彩色图像(在本例中为 24 位,位图)转换为 1Bpp 图像(调色板,转换为位图)。 我知道 imagemagick convert 可以进行转换,但出于某种原因,当我尝试
我是一名优秀的程序员,十分优秀!