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

如何将Google表格导出的CSV格式HEX色值转换为BMP/JPEG图片

CSV转图片实现方案

前期准备

你需要先安装Python环境,然后安装图像处理依赖Pillow,执行以下命令:
pip install pillow

转换代码

将下方代码保存为csv2img.py,修改配置项里的输入输出路径即可使用:

from PIL import Image
import csv

# 自定义配置区域
INPUT_CSV = "替换为你的CSV文件路径,例如./test.csv"
# 输出后缀可自由替换为.bmp/.jpg/.jpeg,程序自动适配格式
OUTPUT_IMG = "替换为输出路径,例如./result.jpg"

if __name__ == "__main__":
    pixel_matrix = []
    # 读取CSV并将HEX转RGB格式
    with open(INPUT_CSV, "r", encoding="utf-8") as f:
        for row in csv.reader(f):
            if not row:
                continue
            current_row = []
            for hex_code in row:
                hex_code = hex_code.strip().lstrip("#")
                r = int(hex_code[0:2], 16)
                g = int(hex_code[2:4], 16)
                b = int(hex_code[4:6], 16)
                current_row.append((r, g, b))
            pixel_matrix.append(current_row)
    
    # 生成图片
    img_height = len(pixel_matrix)
    img_width = len(pixel_matrix[0])
    img = Image.new("RGB", (img_width, img_height))
    for y in range(img_height):
        for x in range(img_width):
            img.putpixel((x, y), pixel_matrix[y][x])
    
    # 保存文件
    img.save(OUTPUT_IMG)
    print("转换完成")

使用说明

  • 把你从Google表格导出的CSV路径填到配置区域的INPUT_CSV位置
  • 需要输出BMP格式就把OUTPUT_IMG的后缀改成.bmp,需要JPEG就改成.jpg或.jpeg
  • 确保你的CSV每一行的单元格数量一致,否则会出现尺寸错误
  • 如果使用带透明度通道的8位HEX色码,把Image.new("RGB")改成Image.new("RGBA"),转换HEX时多取最后两位作为透明度值即可

内容的提问来源于stack exchange,提问作者Chandler Catron

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 06:06:09