Paste the text snippet into the png image

I am looking for a simple command line tool (on Linux) to insert a text fragment (e.g. copyright) into a png file, which creates a new png file:

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

Note: "Insert text fragment", I do not mean "draw text on the image." I mean: insert the text into the png file as a fragment in the technical sense . This can be used, for example, to insert a copyright message that does not appear on the actual image.

+9
source share
6 answers

I searched for a utility to do this, and have not yet found anything that really matches what I want to do. Therefore, I decided to create my own , which, it turns out, is not too complicated. The png-text-dump utility displays all text fragments in a PNG image. It depends on libpng only. The png-text-append utility inserts text fragments into a PNG image. It depends only on the standard C library - I initially tried to implement this with libpng, but it was actually easier to work from scratch using only the PNG specification.

+15
source

Use ImageMagick convert and -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 -set used to set metadata elements. In the case of PNG, they often come in tEXt chunks.

+10
source

This can be implemented using the python pypng module . An example python3 code is given below:

 import png TEXT_CHUNK_FLAG = '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() 
+3
source

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

+1
source

Note. This answer was the answer to the original version of the question, in which it is not clear whether the text should be written on the image or the text should be embedded in the binary image file as metadata. This answer suggested the first. However, the question has been edited to clarify what this means. This answer remains unchanged if someone is looking for a solution for the former.


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

20,20 in the example above is the coordinate in which I want to place the text.

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

On Ubuntu or Debian, you can install it using the command: aptitude install imagemagick .

Here is a slightly more complicated use of the command:

 convert -font Helvetica -pointsize 20 -draw "text 20,20 'hello, world'" Hypercube.png output.png 
+1
source

When adding to the comment made by @ susam-pal, use the parameter to change the color

-fill color

This parameter accepts a color name, hexadecimal color, or numerical values โ€‹โ€‹of RGB, RGBA, HSL, HSLA, CMYK, or CMYKA. See "Color Names" for a description of the correct color argument.

Include the color specification in quotation marks to prevent your shell from interpreting the "#" or parentheses. For instance,

-fill blue

-fill "#ddddff"

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


Retrieved from link

0
source

Source: https://habr.com/ru/post/907042/


All Articles