将文本块插入png图像

gcb*_*son 10 linux png

我正在寻找一个简单的命令行工具(在Linux上)将文本块(例如版权)插入到png文件中,从而生成一个新的png文件:

> png-insert-text-chunk "here's my text chunk" < in.png > out.png
Run Code Online (Sandbox Code Playgroud)

注意:通过"插入文本块",我的意思并不是"在图像上绘制一些文本".我的意思是:从技术意义上说,将文本作为块插入到png文件中.例如,这可以用于插入未在实际图像上显示的版权消息.

gcb*_*son 15

我一直在寻找公用事业来做这件事,还没找到任何真正符合我想做的事情.所以我决定建立自己的,结果并不太难.该实用程序png-text-dump显示PNG图像中的所有文本块.它只取决于libpng.该实用程序png-text-append将文本块插入PNG图像.它仅取决于标准C库 - 我最初尝试使用libpng实现它,但实际上发现仅使用PNG规范从头开始工作更容易.


gio*_*ele 12

使用ImageMagick convert-set选项:

convert IN.png \
        -set 'Copyright' 'CC-BY-SA 4.0' \
        -set 'Title' 'A wonderful day' \
        -set comment 'Photo taken while running' \
        OUT.png
Run Code Online (Sandbox Code Playgroud)

-set选项用于设置元数据元素.在PNG的情况下,这些经常进入tEXt大块.

  • 相反,要**剥离**图像_属性_,请使用 `+set "property"`。 (3认同)

Sus*_*Pal 5

注意:此答案是对问题原始修订版的回应,其中不清楚文本是否必须写入图像上,或者文本是否必须作为元数据嵌入到图像二进制文件中。这个答案假设是前者。然而,该问题经过编辑以澄清这意味着后者。这个答案保持不变,以防有人正在寻找前者的解决方案。


convert -draw "text 20,20 'hello, world'" input.png output.png
Run Code Online (Sandbox Code Playgroud)

上面示例20,20中的 是我要放置文本的坐标。

您需要使用 imagemagick 包来获取此命令。

在 Ubuntu 或 Debian 上,可以使用以下命令安装:apt-get install imagemagick

下面是该命令的更详细的用法:

convert -font Helvetica -pointsize 20 -draw "text 20,20 'hello, world'" input.png output.png
Run Code Online (Sandbox Code Playgroud)

  • 我正在寻找一个命令来插入 png“文本块”,即文件中嵌入的注释。但这是在图像上绘制文本的一种很酷的方法;我不知道“convert”可以做到这一点。 (2认同)

cfh*_*008 5

这可以用 python pypng module 来实现。python3示例代码如下:

import png

TEXT_CHUNK_FLAG = b'tEXt'


def generate_chunk_tuple(type_flag, content):
    return tuple([type_flag, content])


def generate_text_chunk_tuple(str_info):
    type_flag = TEXT_CHUNK_FLAG
    return generate_chunk_tuple(type_flag, bytes(str_info, 'utf-8'))


def insert_text_chunk(target, text, index=1):
    if index < 0:
        raise Exception('The index value {} less than 0!'.format(index))

    reader = png.Reader(filename=target)
    chunks = reader.chunks()
    chunk_list = list(chunks)
    print(chunk_list[0])
    print(chunk_list[1])
    print(chunk_list[2])
    chunk_item = generate_text_chunk_tuple(text)
    chunk_list.insert(index, chunk_item)

    with open(target, 'wb') as dst_file:
        png.write_chunks(dst_file, chunk_list)


def _insert_text_chunk_to_png_test():
    src = r'E:\temp\png\register_05.png'
    insert_text_chunk(src, 'just for test!')


if __name__ == '__main__':
    _insert_text_chunk_to_png_test()
Run Code Online (Sandbox Code Playgroud)