Author Topic: A Simple Graphics To Text Converter  (Read 4485 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
A Simple Graphics To Text Converter
« on: July 11, 2020, 02:00:22 am »
This is an example where I spent all night making a program, thinking I have probably made something similar in the past, finishing it, then finding it on this forum from almost exactly a year ago (July 31) LOL. But that's OK, I just looked over that old code and wow, I have changed a lot in knowing how to reduce the length of programs and only making what I need. I'll post the URL to the old one after I post the code to this one.
This is a tiny little program that can convert any graphics to make using graphics commands, to a Notepad .txt file that you can see in Notepad as ASCII text. Like I did a year ago, I found a list online of the brightest text to the least brightest and made that into a string to use POINT with. Other people have probably made things like this that look better, but this is an improvement for me. Try it out, you can use it as a tool or a toy to copy/paste text to all kinds of stuff or print it out on the printer. Read my Comment Section at the start of the code to know more about it. It saves the text file as TextPicture.txt automatically. I made a bunch of boxes as an example.

Code: QB64: [Select]
  1. 'This tiny program converts your screen graphic to a Notepad TextPicture.txt file automatically.
  2. 'I use a tiny 200 x 200 graphic window but feel free to change it. You will also need to change the loops below.
  3. 'Just remember, the larger the graphic, the bigger the text file. You can use the zoom feature on Notepad to zoom in and out to see it better.
  4. 'I don't suggest using a camera photo with this because they don't come out good I believe. I tried it.
  5. 'But feel free to do what you wish with this.
  6. 'Notice that ch$ has characters that go from brightest to least brightest. I found this list on the Internet.
  7. 'Also, since the vertical length is twice the length of the horizontal on Notepad, there may be parts of a graphic missing.
  8.  
  9. _TITLE "Graphic To Text Converter by SierraKen"
  10.  
  11. SCREEN _NEWIMAGE(200, 200, 32)
  12. _LIMIT 100
  13. '---------------------------------------------------
  14. 'Draw anything you want here with graphic commands.
  15. 'Here are some boxes.
  16. FOR t = 0 TO 100 STEP 10
  17.     c1 = INT(RND * 254) + 1
  18.     c2 = INT(RND * 254) + 1
  19.     c3 = INT(RND * 254) + 1
  20.     LINE (100 - t, 100 - t)-(100 + t, 100 + t), _RGB32(c1, c2, c3), B
  21. '---------------------------------------------------
  22.  
  23. ch$ = "'$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,^`'. "
  24. OPEN "TextPicture.txt" FOR OUTPUT AS #1
  25. FOR y = 0 TO 200 STEP 2
  26.     FOR x = 0 TO 200
  27.         c& = POINT(x, y)
  28.         red& = _RED(c&)
  29.         green& = _GREEN(c&)
  30.         blue& = _BLUE(c&)
  31.         colors& = red& + green& + blue&
  32.         IF colors& < 10 THEN colors& = 10
  33.         letter$ = MID$(ch$, INT(colors& / 10), 1)
  34.         PRINT #1, letter$;
  35.     NEXT x
  36.     PRINT #1, ""
  37. PRINT "TextPicture.txt saved."
  38.  

And here is the old thread from July 31, 2019. If you look at the old code, you can see how much longer that is. The old one also lets you open a .bmp file to convert it to text. I tried using the new JPG, etc. Open/Save on this and it opens it fine, but the text output is pretty bad on camera pictures because of the detail I believe. Here is the old one:
https://www.qb64.org/forum/index.php?topic=1551.msg107688#msg107688

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #1 on: July 11, 2020, 03:23:28 am »
I remember using a program similar to this one... Where I used to work, the powers that be, did not permit the transmission of images on our local network. You guessed it. The image was converted to text... A little rough looking back then but a nice work-a-round...
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #2 on: July 11, 2020, 11:53:05 am »
LOL awesome Johno. Or the old email signatures. lol

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #3 on: July 11, 2020, 12:49:01 pm »
Nice work, and very compact.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #4 on: July 11, 2020, 12:51:35 pm »
Thanks OldMoses. Well, I decided that showing the graphics is more important than keeping the vertical correct, so I did a small change to it here.

Code: QB64: [Select]
  1. 'This tiny program converts your screen graphic to a Notepad TextPicture.txt file automatically.
  2. 'I use a tiny 200 x 200 graphic window but feel free to change it. You will also need to change the loops below.
  3. 'Just remember, the larger the graphic, the bigger the text file. You can use the zoom feature on Notepad to zoom in and out to see it better.
  4. 'I don't suggest using a camera photo with this because they don't come out good I believe. I tried it.
  5. 'But feel free to do what you wish with this.
  6. 'Notice that ch$ has characters that go from brightest to least brightest. I found this list on the Internet.
  7.  
  8. _TITLE "Graphic To Text Converter by SierraKen"
  9.  
  10. SCREEN _NEWIMAGE(200, 200, 32)
  11. _LIMIT 100
  12. '---------------------------------------------------
  13. 'Draw anything you want here with graphic commands.
  14. 'Here are some boxes.
  15. FOR t = 0 TO 100 STEP 10
  16.     c1 = INT(RND * 255) + 1
  17.     c2 = INT(RND * 255) + 1
  18.     c3 = INT(RND * 255) + 1
  19.     LINE (100 - t, 100 - t)-(100 + t, 100 + t), _RGB32(c1, c2, c3), B
  20. '---------------------------------------------------
  21.  
  22. ch$ = "'$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,^`'. "
  23. OPEN "TextPicture.txt" FOR OUTPUT AS #1
  24. FOR y = 0 TO 200
  25.     FOR x = 0 TO 200
  26.         c& = POINT(x, y)
  27.         red& = _RED(c&)
  28.         green& = _GREEN(c&)
  29.         blue& = _BLUE(c&)
  30.         colors& = red& + green& + blue&
  31.         IF colors& < 11 THEN colors& = 11
  32.         letter$ = MID$(ch$, INT(colors& / 11), 1)
  33.         PRINT #1, letter$;
  34.     NEXT x
  35.     PRINT #1, ""
  36. PRINT "TextPicture.txt saved."
  37.  

« Last Edit: July 11, 2020, 01:34:51 pm by SierraKen »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #5 on: July 11, 2020, 07:08:18 pm »
I have been trying to find the program that I used years ago... It wasn't mine... But, back then, the image used was black and white (greyscale) and the "alphabet" was arranged from "darkest" to "lightest" eg: "B" may be the darkest and space the lightest. I think the program would 'grab' the first 8x8 (or whatever size) of the image; average the dark to light ratio of the pixels; Compare the average to a corresponding character and add that character to the file.

But the whole thing hinged on the correct order of the "alphabet".

Before I forget, and that happens a lot, to help reduce the overall physical size (width and height) of the finished document, set the font size to as small as possible... just a thought... haven't tested that yet... lol
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #6 on: July 11, 2020, 07:25:37 pm »
Thanks Johno, I haven't used fonts with it yet. 
Today I have dived into making it into a graphics making program that saves as a text file. I am SOOO dang close. But I am stuck and been stuck for hours. And it's always the same old SCREEN/PUTIMAGE/etc. stuff that I always get stuck on. What's happening is, when someone tries to change the color after drawing first, it won't load the color wheel again. It loads fine the first time when you are setting up to draw, but not any time after that. I've tried everything I know how to do. Can someone help me? The problem is under the "paintcolor:" section at the bottom. It goes to that section and displays the _TITLE words but then your painting screen doesn't change to the color wheel. I'll zip up the .bas and the color wheel picture and put it in the attachments if anybody wants to help. Thanks.

* Graphic Maker To Text Converter.zip (Filesize: 151.38 KB, Downloads: 157)

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #7 on: July 11, 2020, 10:04:44 pm »
There seems to be a slight problem with your 'alphabet'.

The first character of the ch$ string should not be there. It is "too light" to be on the "dark end" of the string.

Try this one: ch$ = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:," + CHR$(34) + "^`'. "

See attached for result.
* TextPicture.txt (Filesize: 39.85 KB, Downloads: 165)
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #8 on: July 11, 2020, 10:19:55 pm »
Johno, that's the black background. The black text gets in the way of the eyes when trying to make out the graphic. That is why I reversed it, black as the smallest symbol, so it won't get in the way of what you drew.
« Last Edit: July 11, 2020, 10:23:28 pm by SierraKen »

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #9 on: July 12, 2020, 12:07:17 am »
Understood.

But, before I changed ch$ back to your original, I figured I might try it on an image of 100x120... Guess who?
* TextPicture.txt (Filesize: 12.17 KB, Downloads: 176)
Logic is the beginning of wisdom.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #10 on: July 12, 2020, 12:19:28 am »
ROFL AWESOME!! LOL

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #11 on: July 12, 2020, 03:28:54 am »
I modified the img2txt program. At the moment, I was just trying to sort out a more unified "shading".

As you will notice, the image is much "clearer" and is only using one ascii character, "@". As a result, outputting to text file is completely useless. The "printed" image does not display the "shades" of the "@", only the "@" characters... W.I.P.

This example is based on a greyscale image. I will work on a colour version... after a coffee of course... lol

One thing I did notice that was strange... Scanning with point(), on the grey-scaled image, then using "color yahda", produces an almost sepia colour. Not a deal breaker. Just an observation. If I can get a colour image to happen then this won't be a problem.
* spock-img2txt.zip (Filesize: 13.77 KB, Downloads: 166)
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #12 on: July 12, 2020, 01:33:27 pm »
Quote
Today I have dived into making it into a graphics making program that saves as a text file. I am SOOO dang close. But I am stuck and been stuck for hours. And it's always the same old SCREEN/PUTIMAGE/etc. stuff that I always get stuck on. What's happening is, when someone tries to change the color after drawing first, it won't load the color wheel again. It loads fine the first time when you are setting up to draw, but not any time after that. I've tried everything I know how to do. Can someone help me? The problem is under the "paintcolor:" section at the bottom. It goes to that section and displays the _TITLE words but then your painting screen doesn't change to the color wheel. I'll zip up the .bas and the color wheel picture and put it in the attachments if anybody wants to help. Thanks.

OH !@#~&^%$ It got me too!

This is a groaner:
Code: QB64: [Select]
  1. paintcolor:
  2. _LIMIT 100
  3. a$ = ""
  4. SCREEN _NEWIMAGE(800, 600, 32)
  5. _TITLE "Click a paint color."
  6. i& = _NEWIMAGE(800, 600, 32)
  7. i& = _LOADIMAGE("PaintColor.png", 32)
  8.     _LIMIT 100
  9.         mousex = _MOUSEX
  10.         mousey = _MOUSEY
  11.         mouseLeftButton = _MOUSEBUTTON(1)
  12.     LOOP
  13.     IF mouseLeftButton THEN
  14.         clr& = POINT(mousex, mousey)
  15.         mouseLeftButton = 0
  16.         CLS
  17.         IF s& THEN _PUTIMAGE , s&: SCREEN s&
  18.         RETURN
  19.     END IF
  20.     _DISPLAY '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  here is the fix
  21.  

Once you use _DISPLAY, you always have to use it or turn off with _AUTODISPLAY
« Last Edit: July 12, 2020, 01:39:45 pm by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #13 on: July 12, 2020, 04:56:32 pm »
WOW Johno!!! That's incredible!!! It reminds me of when Nimoy passed away and I sent my picture with my blue shirt on doing the Vulcan Salute to a company that took 1000's of those pictures and made it into a giant picture of Spock. I would use shades and colors like that but mine is specifically to make .txt files that don't have that. Please edit your post with the code though and add your name to mine in the _TITLE, thanks. Great job!

B+, I don't know what I would do without you sometimes lol. I most likely would have given up on this program if you hadn't fixed it for me. THANK YOU!

Here is the program. You set a picture from 25 to 300 pixels horizontal and vertical and then you can draw what you want on it. The Instructions are on the welcome page. It doesn't have everything that Paint Pixels has, in fact I tried adding this to Paint Pixels and ran into memory problems so I'm sticking with this instead. But you can choose colors and draw and erase. The background is always black because it's easier to see in the text file. Added below is the updated zip file and also a screen shot of the text file I quickly drew with it. I like how you can zoom out in Notepad to see larger text pictures better. Check it out. :)

* Graphic Maker To Text Converter.zip (Filesize: 151.49 KB, Downloads: 134)
Ken-Text.jpg
* Ken-Text.jpg (Filesize: 313.2 KB, Dimensions: 595x990, Views: 206)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: A Simple Graphics To Text Converter
« Reply #14 on: July 12, 2020, 07:13:04 pm »
Johnno, here is a little update I made on your version. It randomly chooses an ASCII character from all the characters that can be displayed. Then it automatically changes to a different one in 1/4 of a second. It keeps going until you press a key on your keyboard. Some ASCII characters are very close to a whole brick of color. If you want, you can add a number in the corner showing you which chr$ (ASCII) number that it's showing. It's just the variable ch.

Code: QB64: [Select]
  1. _TITLE "Graphic To Text Converter by SierraKen and Johnno"
  2.  
  3. SCREEN _NEWIMAGE(800, 1000, 32)
  4. _LIMIT 100
  5. '---------------------------------------------------
  6. 'Draw anything you want here with graphic commands.
  7. 'Here are some boxes.
  8.  
  9. spock& = _LOADIMAGE("spock100x120.png")
  10. _PUTIMAGE (0, 0), spock&
  11. _DELAY 1 '   short delay to view image source
  12. '---------------------------------------------------
  13. DIM pixel(100, 120)
  14.  
  15. '   Selected a small font to remove vertical "stretching"
  16.  
  17. '   Separate loops for 'reading' and 'writing' - could be combined?
  18.  
  19. FOR y = 0 TO 119
  20.     FOR x = 0 TO 99
  21.         pixel(x, y) = POINT(x, y)
  22.     NEXT x
  23.  
  24. '   Although the source image is "grey", "point()" does not think so...
  25.     cc = INT(RND * 2) + 1
  26.     IF cc = 1 THEN ch = INT(RND * 93) + 33
  27.     IF cc = 2 THEN ch = INT(RND * 126) + 128
  28.     FOR y = 0 TO 119
  29.         FOR x = 0 TO 99
  30.             COLOR pixel(x, y)
  31.             letter$ = CHR$(ch)
  32.             PRINT letter$;
  33.         NEXT
  34.         PRINT ""
  35.     NEXT
  36.     _DELAY .25
  37.     _DISPLAY
  38.