demo-04-20/demo.lua

74 lines
1.8 KiB
Lua

local cairo = require'cairo' --used to open the file
local bitmap = require'bitmap' --used to read its content
equivalents = dofile'equivalents.lua'
function to_chr(nb)
if equivalents[nb] then return equivalents[nb] end
if nb > 255 then print("uh", nb) end
if nb < 16 then add = "0"
else add = "" end
return [[\x]]..add..string.format("%x", nb)
end
file = io.open("imagination.py", "w")
first = true
pallet = {} --will store colors
pallet_last = 0
mem_pallet_last = 0
file:write([[from draw import *
def main():
black=(0,0,0)
for x in range(384):
for y in range(192):
set_pixel(x,y,black)
]])
for i = 1, 12, 1 do
print(i)
do --open image
path = "spongebob/resized_cropped_out-"
if i < 10 then path = path.."0" end
path = path..i.."0.png"
local image = cairo.load_png(path)
image_infos = {width = image:width(), height = image:height()}
image_bitmap = image:bitmap()
end
local getpixel, setpixel = bitmap.pixel_interface(image_bitmap)
file:write("\n", [[ data=b"]])
--end
for j = 1, image_infos.height, 1 do
for i = 1, image_infos.width, 1 do
r, g, b = getpixel(i, j)
if not pallet[r..","..g..","..b] then
pallet[r..","..g..","..b] = pallet_last
pallet_last = pallet_last + 1
end
file:write(to_chr(pallet[r..","..g..","..b]))
end
end
file:write('"')
--let's write the pallet
if pallet_last > mem_pallet_last then
mem_pallet_last = pallet_last
file:write("\n pallet={")
for i, v in pairs(pallet) do
file:write(v, ":(", i, "),")
end
file:write("}")
end
file:write("\n draw(", math.floor(192 - image_infos.width / 2), ",")
file:write(math.floor(96 - image_infos.height / 2), ",data,pallet,")
file:write(image_infos.width, ")")
first = false
end
file:write('\n wait_ac("Press AC/ON to continue...")')
file:close()