Log In  

Hi I have made a quick python script to turn an square picture to a string of text that can the be parsed by pico8 to make a nice splashscreen.


Pico8 code

function txt_to_pic(txt)
 local d={
 a=10,
 b=11,
 c=12,
 d=13,
 e=14,
 f=15
 }
 d['0']=0
 d['1']=1
 d['2']=2
 d['3']=3
 d['4']=4
 d['5']=5
 d['6']=6
 d['7']=7
 d['8']=8
 d['9']=9
 local x=0
 local y=0
 for i=0,#txt do
  if x>128 then 
   x=0
   y+=1
  end
  c=d[sub(txt,i,i)]
  pset(x,y,c)
  x+=1
 end
end

And here is the python script (just replace "me.jpg" by your image):

import numpy
import PIL
#%%
img = PIL.Image.open("me.jpg")
img = img.resize((128,128))
imgarr = numpy.array(img)
newimg=numpy.array(img)
img.resize((128*10,128*10))

p8colors=[
        (0,0,0),
        (29,43,83),
        (126,37,83),
        (0,135,81),
        (171, 82, 54),
        (95, 87, 79),
        (194, 195, 199),
        (255, 241, 232),
        (255, 0, 77),
        (255, 163, 0),
        (255, 236, 39),
        (0, 228, 54),
        (41, 173, 255),
        (131, 118, 156),
        (255, 119, 168),
        (255, 204, 170)]

col_to_letter='0123456789ABCDEF'

def color_dist(a,b):
    r1,g1,b1 = a[:3]
    r2,g2,b2 = b[:3]
    return int(((r1-r2)**2+(g1-g2)**2+(b1-b2)**2)**(0.5))

def best_col(c):
    distances=[color_dist(c,i) for i in p8colors]
    m=min(distances)
    return  distances.index(m)
STR = ''

for y,line in enumerate(imgarr):
    for x,c in enumerate(line):
        newc=best_col(c)
        STR+=col_to_letter[newc]
        newimg[y][x]=p8colors[newc]

    STR+='L'

p8image=(STR)
PIL.Image.fromarray(newimg).resize((1280,1280))

Cart #ganuhupawe-0 | 2019-11-01 | Code ▽ | Embed ▽ | No License
5

P#69552 2019-11-01 12:34 ( Edited 2019-11-01 12:35)

Hi, @Lahat:

I was checking quickly to see if Pico-8 could import .JPG as their spritesheet and sadly see they do not. Nor do they resize from a larger size. So your code is instrumental in that.

However, if you have the ability to already have your image in .PNG format (or can resize and save as PNG with Freeware IrfanVIEW), then you can skip Python completely with:

import me.png

And then run this code:

t=""
for i=0,8191 do
  t=t..sub(tostr(peek(i),1),5,6)
end
printh('pic="'..t..'"',"@clip")

to convert your picture to a single string variable pasted by pressing CTRL-V.

P#69562 2019-11-01 16:12 ( Edited 2019-11-01 22:40)

This doesn't work for me, I get the error:

Traceback (most recent call last):
File "/home/mauce/shared/./jpgtopico8.py", line 5, in <module>
img = PIL.Image.open("./me.jpg")
AttributeError: module 'PIL' has no attribute 'Image'

Any ideas hhow to proceed?

P#141146 2024-02-05 15:06
1

@zakhooi, Depict has similar functionality and is web based: https://bikibird.itch.io/depict

P#141171 2024-02-05 20:35

@zakooi, I cannot remember the version of PIL I used. I am trying now and I am getting the same error.

looks like instead of

import PIL
img = PIL.Image.open("me.jpg")

it should be:

from PIL import Image
img = Image.open("me.jpg")

By the end the "p8image" string variable contains the string needed to render the image in the cartridge

txt_to_pic("19823AFFBADC89...
P#141173 2024-02-05 21:00

@bikibird
Yes I've tried that too but there I have another problem.
It generated the output GFX just fine but I don't know how to use that in pico-8 to make that the background.
That would solve my problem.

So there is GFX and a lot of raw data.
How would the pico8 code look like to make that GFX raw data the background in my game?

P#141179 2024-02-05 21:40

You can use the download button to download a png and drag that into the sprite sheet. Or, you can open your p8 file in text editor and replace the data under GFX with data produced with Depict.

Also, you may be interested in PX9: https://www.lexaloffle.com/bbs/?tid=34058

P#141184 2024-02-05 22:17 ( Edited 2024-02-05 22:24)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 01:14:30 | 0.089s | Q:27