Making an image out of a string in python

Question:

I remember seeing a tutorial on OpenCV on making an imagine out of a multiline string.

Example :

"""
001000000
001000000
001000000
001000000
001000000
001000000
"""

That would produce a black pixel for every 0 in the string, and a white pixel for the 1s.

like that

Please tell me if I can give you more information!

Asked By: null

||

Answers:

Put a NetPBM header on with width and height and you are pretty much there if you save this as image.pbmit will be viewable with GIMP, Photoshop, feh, eog etc:

P1
9 6
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0

enter image description here


Enlarge it to 100 pixels wide, invert and convert to PNG with ImageMagick like this:

magick image.pbm -negate -scale 100x result.png
Answered By: Mark Setchell
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.