图片像素点自动转Excel

发布于 2020-05-18  182 次阅读


奇奇怪怪的操作,用Python写的自动将图片的像素点转换到Excel表中的单元格背景色,组成马赛克图片。

使用的模块

openpyxl
Pillow
xlwings

使用命令:

python test.py [图片文件路径] [Excel文件路径]

代码:

from PIL import Image
import argparse
import xlwings as xw

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "input_file",
        help="源图片文件"
    )
    parser.add_argument(
        "output_file",
        help="输出Excel文件"
    )
    parser.add_argument(
        "--mosaic",
        type=int,
        default=4,
        help="单元格像素大小"
    )
    parser.add_argument(
        "--max-width",
        type=int,
        default=256
    )

    args = parser.parse_args()
    original_image = Image.open(args.input_file)
    resize_image = original_image.resize(
        (args.max_width, int(float(original_image.size[1]) / original_image.size[0] * args.max_width)),
        Image.ANTIALIAS)

    wb = xw.Book(args.output_file)
    sht1 = wb.sheets['Sheet1']

    image_width = resize_image.size[0]
    image_height = resize_image.size[1]

    for row in range(1, image_height + 1):
        for col in range(1, image_width + 1):
            sht1.range((row,col)).column_width = 2.25
            color = resize_image.getpixel((col - 1, row - 1))
            print('position: ', row,',',col,'RGB: ', color)
            sht1.range((row,col)).color = color

Nothing is impossible!