Home | Mμse | Docs | Art | Music | Videos | Projects | Forum | Shop


[ Click to visit TheOuterLinux's 'Support Me' page ] [ QRCode ] [ RSS ]

img2qb Python Script

Last updated: 2021/03/27


Using the img2qb Python script, you can convert an image into a QuickBASIC (QB45, QBX, and QB64) friendly DATA format. A preview file and a plain text *.QBD (QBASIC DATA) file are created in which you can either copy/paste the inside text to a QB project or use it some other way. However, this script is only meant to help with creating small sprites. QB45 and QBX will run the created array just fine as long as it's 160x100 or less but it will not compile to an EXE if the lines in the array are too long.

Download img2qb

Run via 'python /path/to/img2qb.py' and follow the instructions from there.


An example usage after conversion:

DATA ... 'If you chose to already include DATA line-by-line in the script,
DATA ... 'you can copy and paste the *.qbd file contents here.
DATA ...

SCREEN 12 'Use SCREEN number you picked in img2py script
CLS
SPRITEW = 56 'Sprite width
SPRITEH = 55 'Sprite height
SPRITEX = 170 'Initial x position of sprite
SPRITEY = 100 'Initial y position of sprite
SCREENW = 640 'Screen width
SCREENH = 480 'Screen height
FOR SPRITEPIXELY = 1 TO SPRITEH
  FOR SPRITEPIXELX =  TO SPRITEW
    READ SPRITEPIXELCOLOR%
    PSET (SPRITEPIXELX, SPRITEPIXELY), SPRITEPIXELCOLOR%
  NEXT SPRITEPIXELX
NEXT SPRITEPIXELY

' You can also do the following for transparent pixels. The Python 
' script uses variables like "X" and "Y" instead of "SPRITE..."

FOR SPRITEPIXELY = 1 TO SPRITEH ' For each row; image height
  SPRITEPIXELX =  TO SPRITEW ' For each column; image width
    READ DotColor
      IF DotColor > 0 THEN                           'We can then use this IF-THEN statement to
        PSET (SPRITEPIXELX, SPRITEPIXELY), DotColor  'make COLOR 00 as a transparent color.
      END IF
  NEXT SPRITEPIXELX
NEXT SPRITEPIXELY

'Anything from here down is for sprite movement only and is not required
'if you just want to view the image, but anything over 160x100 will probably
'not load on anything older than QB64.

DIM SPRITE%(SPRITEW * SPRITEH)
GET (1, 1)-(SPRITEW, SPRITEH), SPRITE%
CLS
ONE:
PUT (SPRITEX, SPRITEY), SPRITE%
WAIT &H3DA, 8
PUT (SPRITEX, SPRITEY), SPRITE%

'Use classic WASD keys to move sprite and prevent out-of-bounds errors
io$ = INKEY$
IF io$ = "w" THEN SPRITEY = SPRITEY - 5: IF SPRITEY < 0 THEN SPRITEY = 0
IF io$ = "a" THEN SPRITEX = SPRITEX - 5: IF SPRITEX < 0 THEN SPRITEX = 0
IF io$ = "s" THEN SPRITEY = SPRITEY + 5: IF SPRITEY > (SCREENH - SPRITEH) THEN SPRITEY = SCREENH - SPRITEH
IF io$ = "d" THEN SPRITEX = SPRITEX + 5: IF SPRITEX > (SCREENW - SPRITEW) THEN SPRITEX = SCREENW - SPRITEW
IF io$ = "q" THEN END

GOTO ONE:
    

If using FreeBasic...

The advantage of using FreeBasic is fewer memory issues and larger arrays on DOS and therefore larger graphics when using img2qb.py. However, if using the DOS version of FBC, you will need to ship csdpmi*b.zip with your compiled EXE.

You can use the following or perhaps try running fbc -lang qb /path/to/file.bas instead:

DATA...
DATA...
SCREEN 13 'Use SCREEN number you picked
CLS
DIM DotColor as byte
DIM Pause as string
FOR Y as integer = 1 TO 200 ' For each row; image height
  FOR X as integer = 1 TO 320 ' For each column; image width
    READ DotColor
    IF DotColor > 0 THEN     'We can then use this IF-THEN statement to
       PSET (X, Y), DotColor  'make COLOR 00 as a transparent color.
     END IF
   NEXT X
 NEXT Y
LOCATE 20, 1
INPUT "Pause", Pause

Disclaimer

QRCode for current page:

[ QRCode ]