AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 783453
Accepted
Artifechs
Artifechs
Asked: 2024-09-14 01:36:21 +0800 CST2024-09-14 01:36:21 +0800 CST 2024-09-14 01:36:21 +0800 CST

ImageMagick:使用 -kmeans 时,如何将某种颜色定义为索引颜色图中的第一个颜色?

  • 772

我需要将特定颜色作为颜色图中的第一个颜色,以-kmeans减少图像中的颜色数量。定义遮罩颜色时,虚幻引擎 1 纹理需要它。

这是我目前得到的全部信息:

magick "$PNG_FILE" -kmeans 255 "$PCX_FILE"

我可以在某种程度上使用-define kmeans:seed-colors="#ff00ff"指令来实现这一点,但是它会使输出变得太糟糕,因为它会阻止种子颜色被自动采样。

imagemagick
  • 1 1 个回答
  • 24 Views

1 个回答

  • Voted
  1. Best Answer
    Mark Setchell
    2024-09-15T01:01:24+08:002024-09-15T01:01:24+08:00

    使用ImageMagick无法做到这一点,但可以使用NetPBM套件做到这一点。诀窍是使用带有强制调色板的ppmtopcx 。

    这里有一个脚本可以为您完成此操作 - 它带有详细的注释,并且以易于查看的格式创建所有中间文件:

    #!/bin/bash
    ################################################################################
    # Reorder palette of PNG file to put magenta first in palette and make into PCX
    ################################################################################
    
    # Pick up filename from first parameter, or use "start.png" if none given
    input=${1:-'start.png'}
    >&2 echo "Processing file: $input"
    
    # Extract palette from original image
    >&2 echo "Extracting original palette into palette0.txt"
    magick identify -verbose "$input" | awk '
       /Rendering intent:/ {exit}
       p==1                {print substr($3,2,6)}
       /Colormap:/         {p=1}
       ' > palette0.txt
    
    # Reorder palette with desired entry (FF00FF) first
    >&2 echo "Reordering palette into palette1.txt"
    { printf "FF00FF\n"; grep -v "FF00FF" palette0.txt; } > palette1.txt
    
    # Make PPM file with new palette
    # width will be number of palette entries, height will be 1
    w=$(wc -l < palette1.txt)
    h=1
    >&2 echo "Making ${w}x${h} PPM file with new palette in palette.ppm"
    printf "P6\n$w $h\n255\n" > palette.ppm
    
    # Convert textual palette (palette1.txt) to binary and append to PPM we just started above
    tr -d '#\n' < palette1.txt | xxd -r -p >> palette.ppm
    
    # Now convert original image to PPM and pass to "ppmtopcx" forcing the new palette as we go
    pngtopnm start.png | ppmtopcx -8bit -palette palette.ppm > result.pcx
    
    # Dump palette of created PCX file for fun - it's the final 768 bytes
    tail -c 768 result.pcx | xxd -c12 -g3
    

    我制作了一张测试图像,保证包含洋红色和其他 255 种颜色,如下所示:

    magick -size 256x256 gradient:black-magenta start.png
    

    最后转储的调色板如下所示:

    00000000: ff00ff 000000 010001 020002  ............
    0000000c: 030003 040004 050005 060006  ............
    00000018: 070007 080008 090009 0a000a  ............
    ...
    ...
    000002dc: f300f3 f400f4 f500f5 f600f6  ............
    000002e8: f700f7 f800f8 f900f9 fa00fa  ............
    000002f4: fb00fb fc00fc fd00fd fe00fe  ............
    

    希望您能看到洋红色 (ff00ff) 作为第一个条目,并且它缺少作为最后一个条目(已重新定位到开始处)。


    如果你不喜欢NetPBM,你可以使用 Python 中的 Pillow 非常简单地完成此操作,如下所示:

    #!/usr/bin/env python3
    
    from PIL import Image
    import numpy as np
    
    # Load input image
    im = Image.open('start.png')
    
    # Get palette
    p = im.getpalette()
    #print(f'{p=}')
    
    # Make new palette, by swapping first and last RGB entries
    firstEntry = p[:3]
    print(f'{firstEntry=}')
    lastEntry = p[-3:]
    print(f'{lastEntry=}')
    newPalette = [ *lastEntry, *p[3:765], *firstEntry ]
    print(f'{len(newPalette)=}')
    #print(f'{newPalette=}')
    
    # Make image into Numpy array of palette indices
    na = np.array(im)
    
    # Find all locations with palette entry 0, and all locations with palette entry 255
    mask0   = na==0
    mask255 = na==255
    
    # Swap 0 for 255 and 255 for 0
    na[mask0]   = 255
    na[mask255] = 0
    
    # Revert to PIL Image and push in new palette
    newIm = Image.fromarray(na)
    newIm.putpalette(newPalette)
    
    # Save result as PCX
    newIm.save('result.pcx')
    

    注意,我没有编写一般情况的代码,只是演示了交换调色板索引 0 和 255 的过程。另一种情况并不难,你当然可以从这个例子中编写代码。我不想陷入一般情况的泥潭,因为 Python 可能不适合你。

    • 0

相关问题

  • 如何将很多选项传递给 ImageMagick?

  • 安装 ImageMagick 后我没有工具 imgout

  • GNU 与 for 循环并行?

  • 自动恢复扫描照片边缘的垂直度

  • 使用 ImageMagick 创建具有多行的调色板图像

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve