Created
August 5, 2021 08:17
-
-
Save kkolyan/edaeea449f08b51b4a6943a42fc4d124 to your computer and use it in GitHub Desktop.
Love2D Script to extract original from PKM files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function string.ends_with(str, ending) | |
return ending == "" or str:sub(-#ending) == ending | |
end | |
local index = 1 | |
local images = {} | |
local canvas | |
local function add(path, newname) | |
table.insert(images, { love.graphics.newImage(path), newname }) | |
print(("Added: %s as %s"):format(path, newname)) | |
end | |
local function visit(path) | |
local paths = {} | |
for i, v in ipairs(love.filesystem.getDirectoryItems(path)) do | |
local child = path .. "/" .. v | |
local info = love.filesystem.getInfo(child) | |
if info.type == "directory" then | |
visit(child) | |
elseif v:ends_with(".dds") or v:ends_with(".pkm") then | |
table.insert(paths, child) | |
end | |
end | |
for i, child in ipairs(paths) do | |
local newname = child:match("(.+)%..+") .. ".png" | |
local targetName = newname:gsub("%/", "__") | |
local fileInfo = love.filesystem.getInfo(targetName) | |
if fileInfo then | |
print(("Overwriting %s"):format(newname)) | |
else | |
print(("Adding %s"):format(newname)) | |
end | |
add(child, targetName) | |
end | |
end | |
function love.load() | |
love.filesystem.setIdentity("depkm") | |
visit("img") | |
print(("Tasks: %s"):format(#images)) | |
canvas = love.graphics.newCanvas(4096, 4096) | |
end | |
function love.draw() | |
if index > #images then | |
love.graphics.print("done") | |
print("done") | |
love.event.quit() | |
else | |
local image, filename = unpack(images[index]) | |
love.graphics.setCanvas(canvas) | |
love.graphics.clear(0, 0, 0, 0) | |
--love.window.setMode(image:getWidth(), image:getHeight(), { borderless = true }) | |
love.graphics.draw(image) | |
--love.graphics.captureScreenshot(targetName) | |
love.graphics.setCanvas() | |
local w = image:getWidth() | |
local h = image:getHeight() | |
print(("Saving %s. size: {%s; %s}"):format(filename, w, h)) | |
love.graphics.print(("Saving %s. size: {%s; %s}"):format(filename, w, h)) | |
local data = canvas:newImageData(nil, 1, 0, 0, w, h) | |
data:encode("png", filename) | |
index = index + 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment