Linux 将文本块插入到 png 图像中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9036152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:15:35  来源:igfitidea点击:

Insert a text chunk into a png image

linuxpng

提问by gcbenison

I'm looking for a simple command-line tool (on Linux) to insert a text chunk (e.g. copyright) into a png file, resulting in a new png file:

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

> png-insert-text-chunk "here's my text chunk" < in.png > out.png

Note: by "insert a text chunk", I do not mean "draw some text on the image". I mean: insert the text into the png file as a chunk, in the technical sense. This can be used, for example, to insert a copyright message that isn't displayed on the actual image.

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

采纳答案by gcbenison

I have searched around for utilities to do this, and not yet found anything that really matches what I want to do. So I decided to build my own, which it turns out is not too hard. The utility png-text-dumpdisplays all text chunks in a PNG image. It depends only on libpng. The utility png-text-appendinserts text chunks into a PNG image. It depends only on the standard C library - I had initially tried to implement this using libpng, but actually found it easier to work from scratch using only the PNG specification.

我已经四处寻找实用程序来执行此操作,但还没有找到任何真正符合我想要做的事情。所以我决定建立自己的,事实证明这并不太难。该实用程序png-text-dump在 PNG 图像中显示所有文本块。它仅取决于 libpng。该实用程序png-text-append将文本块插入到 PNG 图像中。它仅依赖于标准 C 库——我最初尝试使用 libpng 来实现它,但实际上发现仅使用 PNG 规范从头开始工作更容易。

回答by Susam Pal

Note: This answer was a response to the original revision of the question where it was not clear if the text had to be written on the image or if the text had to be embedded within the image binary file as metadata. This answer assumed the former. However the question was edited to clarify that it meant the latter. This answer is left intact, in case someone is looking for a solution to the former.

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



convert -draw "text 20,20 'hello, world'" input.png output.png

The 20,20in the above example is the co-ordinate where I want to place the text.

20,20上面的例子就是统筹,我想放置文本。

You need to use the imagemagick package to get this command.

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

On Ubuntu or Debian, it can be installed with the command: apt-get install imagemagick.

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

Here is a slightly more elaborate usage of the command:

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

convert -font Helvetica -pointsize 20 -draw "text 20,20 'hello, world'" input.png output.png

回答by futureman

I believe that pngcrush has this ability: http://pwet.fr/man/linux/commandes/pngcrush

我相信 pngcrush 有这种能力:http: //pwet.fr/man/linux/commandes/pngcrush

回答by Bruce_Warrior

Adding to the comment made by @susam-pal, to change color use the option

添加到@susam-pal 的评论中,使用选项更改颜色

-fillcolor

-填充颜色

This option accepts a color name, a hex color, or a numerical RGB, RGBA, HSL, HSLA, CMYK, or CMYKA specification. See Color Names for a description of how to properly specify the color argument.

此选项接受颜色名称、十六进制颜色或数字 RGB、RGBA、HSL、HSLA、CMYK 或 CMYKA 规范。有关如何正确指定颜色参数的说明,请参阅颜色名称。

Enclose the color specification in quotation marks to prevent the "#" or the parentheses from being interpreted by your shell. For example,

将颜色规范括在引号中,以防止 shell 解释“#”或括号。例如,

-fill blue

-填充蓝色

-fill "#ddddff"

-填充“#ddddff”

-fill "rgb(255,255,255)"

-fill "rgb(255,255,255)"



Obtained from link

链接获得

回答by gioele

Use ImageMagick's convertand the -setoption:

使用 ImageMagickconvert-set选项:

convert IN.png \
        -set 'Copyright' 'CC-BY-SA 4.0' \
        -set 'Title' 'A wonderful day' \
        -set comment 'Photo taken while running' \
        OUT.png

The -setoption is used to set metadata elements. In the case of PNG these often go into tEXtchunks.

-set选项用于设置元数据元素。在 PNG 的情况下,这些通常分成tEXt块。

回答by cfh008

This can be implemented with python pypng module. The python3 example code is below:

这可以用 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()