Apologies for the necromancy on an old thread.
I have a trivial function I used to peek at images in the simulator console:
--- Takes a playdate image and returns a multiline string for printing.
function image_str(img)
local black <const> = playdate.graphics.kColorBlack
local white <const> = playdate.graphics.kColorClear
local out = {}
local w, h = img:getSize()
for y = 0,h-1 do
local s = {}
for x = 0,w-1 do
local p = img:sample(x,y)
if p == black then
s[#s+1] = "⬛"
elseif p == white then
s[#s+1] = "⬜"
else
s[#s+1] = "🟨"
end
end
out[#out+1] = table.concat(s)
end
return table.concat(out, "\n")
end
This generates output like the following (112x96pixel)
For fun, tonight I wrote a version that dithers to unicode block characters:
--- Dithers a playdate image to a string half the size of the original
function img_str4(img)
local black <const> = playdate.graphics.kColorBlack
local map = {
["⬛⬜⬜⬜"] = "▘", ["⬜⬛⬜⬜"] = "▝", ["⬜⬜⬛⬜"] = "▖", ["⬜⬜⬜⬛"] = "▗",
["⬛⬛⬜⬜"] = "▀", ["⬜⬛⬜⬛"] = "▐", ["⬜⬜⬛⬛"] = "▄", ["⬛⬜⬛⬜"] = "▌",
["⬜⬜⬜⬜"] = " ", ["⬛⬛⬛⬛"] = "█", ["⬛⬜⬜⬛"] = "▚", ["⬜⬛⬛⬜"] = "▞",
["⬛⬛⬛⬜"] = "▛", ["⬛⬛⬜⬛"] = "▜", ["⬛⬜⬛⬛"] = "▙", ["⬜⬛⬛⬛"] = "▟",
}
local out = {}
local w, h = img:getSize()
for y = 0,h-1,2 do
local l = {}
for x = 0,w-1,2 do
local s = table.concat({
img:sample(x,y) == black and "⬛" or "⬜",
img:sample(x+1,y) == black and "⬛" or "⬜",
img:sample(x,y+1) == black and "⬛" or "⬜",
img:sample(x+1,y+1) == black and "⬛" or "⬜",
})
l[#l+1] = map[s]
end
out[#out+1] = table.concat(l)
end
return table.concat(out, "\n")
end
Alternatively you can directly launch file:// urls of images you write to the Playdate disk which will open the gif file in Preview.
function preview_image(img, username, filename)
filename = filename or "img.gif"
username = username or "peter"
playdate.datastore.writeImage(img, filename)
local file_path = string.format(
"/Users/%s/Developer/PlaydateSDK/Disk/Data/%s/images/%s",
username, playdate.metadata.bundleID, filename
)
playdate.simulator.openURL(string.format("file://%s", file_path))
print("Preview image at", file_path)
end