You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从图像还原IVIS设备调色板并保留色阶范围

解决方案:从PDF图例还原并生成彩虹调色板

一、提取PDF中的调色板颜色

首先将PDF指定页面(第1、3页)导出为高清图像,避免低分辨率导致的颜色混淆:

  • 使用Poppler工具的pdftoppm命令导出300DPI的PNG:
    pdftoppm -f 1 -l 3 -r 300 input.pdf output
    
    高清图像能让调色板的色块边界更清晰,减少取色误差。

Python 取色实现(Pillow + numpy)

from PIL import Image
import numpy as np

# 打开导出的高清页面图像
img = Image.open("output-1.png")
# 手动框选调色板区域坐标(替换为实际位置的x1,y1,x2,y2)
palette_region = img.crop((100, 200, 500, 250))
# 提取区域内所有唯一颜色,按像素数量排序(高频色对应标准色块)
color_counts = np.array(palette_region.getcolors(maxcolors=1000))
sorted_colors = sorted(color_counts, key=lambda x: -x[0])
# 提取前N个核心色阶(按需调整数量)
rgb_colors = [tuple(c[1]) for c in sorted_colors[:8]]

R 取色实现(magick + dplyr)

library(magick)
library(dplyr)

# 读取PDF页面并转为高清图像
img <- image_read_pdf("input.pdf", pages = c(1,3), density = 300)
# 裁剪调色板区域(替换为实际坐标范围)
palette_img <- image_crop(img[1], "100x50+200+200")
# 统计区域内颜色出现频率
color_data <- image_data(palette_img, "rgb") %>%
  as.data.frame() %>%
  mutate(color = paste(V1, V2, V3, sep = ",")) %>%
  count(color, sort = TRUE)
# 转换为0-1范围的RGB向量
rgb_colors <- strsplit(color_data$color, ",") %>%
  lapply(function(x) as.integer(x)/255) %>%
  do.call(rbind, .)

二、生成匹配色阶的渐变调色板

用提取的核心色阶,生成平滑的渐变调色板,还原原图例的色阶范围:

Python 生成渐变(matplotlib)

import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

# 创建自定义彩虹色图
custom_cmap = LinearSegmentedColormap.from_list("custom_rainbow", rgb_colors, N=256)
# 可视化生成的图例
plt.figure(figsize=(8, 1))
color_bar = plt.imshow([np.linspace(0, 1, 256)], cmap=custom_cmap, aspect='auto')
plt.xticks([])
plt.yticks([])
plt.colorbar(color_bar, orientation='horizontal', ticks=np.linspace(0,1,len(rgb_colors)))
plt.show()

R 生成渐变(ggplot2 + scales)

library(ggplot2)
library(scales)

# 创建自定义渐变调色板
custom_palette <- gradient_n_pal(rgb_colors, values = seq(0,1,length.out = nrow(rgb_colors)))
# 绘制匹配原图例的渐变条
ggplot(data.frame(x=1:256, y=1)) +
  geom_tile(aes(x=x, fill=x)) +
  scale_fill_gradientn(colors = rgb_colors, breaks = seq(1,256, length.out = nrow(rgb_colors))) +
  theme_void()

三、验证与调整

  • 若提取颜色有偏差,可提高导出图像的DPI(如600DPI)或微调裁剪区域,确保色块完整。
  • 若原调色板是非线性渐变,可调整渐变节点的values参数(R)或色图的分段点(Python),匹配原色阶的过渡趋势。

内容的提问来源于stack exchange,提问作者William Wong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 11:55:21