Save sprite as png picture.

Hello, im working on character generator and i want to save sprite of character in png pictures for the further usege in actors.json. So is there any way to save a Sprite in rpg maker?
@Mavelyssa When you export the characters made in the character generator, they are automatically in the png format. And for the record, this belongs in the MV Support section, not the Javascript/Plugin Support.
@Mavelyssa When you export the characters made in the character generator, they are automatically in the png format. And for the record, this belongs in the MV Support section, not the Javascript/Plugin Support.
No-no-no, Im writing a plugin, which chreates own char generator when you start new game.
@Mavelyssa Then SumRndmDde already made one: http://sumrndm.site/character-creator-ex/ .
Firstly i want to learn how to code. And secondly, i dont like colors of hair,eyes etc. in his plugin.
you need study how work nwJS 🤘

here a copy past from my png map generator, take a look how it work,
it would be too long for me to explain, but take inspiration.
PHP:
            var fs = require('fs');
            var pathName = `img/parallaxes/${$dataMap.parallaxName}.png`;
            var snap = Bitmap.snap2(target);
            var urlData = snap._canvas.toDataURL();
            var base64Data = urlData.replace(/^data:image\/png;base64,/, "");
            fs.writeFile(pathName, base64Data, 'base64', function(error){
                if (error !== undefined && error !== null) {  console.error('An error occured while saving the screenshot', error); }
            });
            // PHOTOSHOP MODE
            if(option["c-save_psd"]){
                var newFolderPath = `img/parallaxes/_psd_${$dataMap.parallaxName}`;
                if(!fs.existsSync(newFolderPath)){ fs.mkdirSync(newFolderPath)}; // create folder if not exsit
                    
                for (let i = 0; i < target._spriteset.displayLayers.length; i++) {
                    let layer = target._spriteset.displayLayers[i];
                    let pathName = `${newFolderPath}/${$dataMap.parallaxName}_psd_layer${i}.png`;
                    let snap = Bitmap.snap2(layer);
                    let urlData = snap._canvas.toDataURL();
                    let base64Data = urlData.replace(/^data:image\/png;base64,/, "");
                    fs.writeFile(pathName, base64Data, 'base64', function(error){
                        if (error !== undefined && error !== null) {  console.error('An error occured while saving the screenshot', error); }
                    });
                }
            };

PHP:
    Bitmap.snap2 = function(stage) {
        var width =  $dataMap.width*48;
        var height = $dataMap.height*48;
        var bitmap = new Bitmap(width, height);
        var context = bitmap._context;
        var renderTexture = PIXI.RenderTexture.create(width, height);
        if (stage) {
            Graphics._renderer.render(stage, renderTexture);
            stage.worldTransform.identity();
            var canvas = null;
            if (Graphics.isWebGL()) {
                canvas = Graphics._renderer.extract.canvas(renderTexture);
            } else {
                canvas = renderTexture.baseTexture._canvasRenderTarget.canvas;
            }
            context.drawImage(canvas, 0, 0);
        } else {

        }
        renderTexture.destroy({ destroyBase: true });
        bitmap._setDirty();
        return bitmap;
    };
@Mavelyssa No need to snap- I was only trying to help...
Ideally, it would be nice if you could make a video of your code and how you do it for the comunauter guides you efficiently, especially considering your age. These very difficult to start with also can experience.
@Mavelyssa No need to snap- I was only trying to help...
Im not snapping, i just wanted to explain why i want to write my own char generator. No offence c:
Firstly i want to learn how to code. And secondly, i dont like colors of hair,eyes etc. in his plugin.
try inject Bitmap.snap in your console
and try understand how this work.

If you don't know, for open console, use F8 when you are in game.
You will can inject and test all your code.
upload_2018-4-3_16-43-54.png


Bitmap.snap will generate a texture from your Scene or stage. and with node js your will can save as png to you directory.
If you take a look at this here https://gitlab.com/FeniXEngineMV/fenix-tools/blob/dev/src/Utils/extractCanvasImage.js
PHP:
function extractCanvasImage (quality = 0.1) {
  const renderer = window.Graphics._renderer
  const stage = renderer._lastObjectRendered
  renderer.render(stage)
  const data = renderer.view.toDataURL('image/png', quality)
  return data
}
Basically this extracts the entire stage, which will be a full screenshot of the game but if you want to be more specific.

extract a sprite(untested but should work)
PHP:
function extractSpriteImage (sprite, quality = 0.1) {
  const renderer = window.Graphics._renderer.extract.canvas(sprite)
  return renderer.toDataURL('image/png', quality)
}

This returns a base64Data which can be used to write the image to a file. If you're making a character generator I assume you know how to write a file, so I'll leave that out.
you can write your own but still look at what he did for the saving part. You don't have to do it exactly the same way, but at least you'll learn how he did it and be able to learn from it.
If you take a look at this here https://gitlab.com/FeniXEngineMV/fenix-tools/blob/dev/src/Utils/extractCanvasImage.js
PHP:
function extractCanvasImage (quality = 0.1) {
  const renderer = window.Graphics._renderer
  const stage = renderer._lastObjectRendered
  renderer.render(stage)
  const data = renderer.view.toDataURL('image/png', quality)
  return data
}
Basically this extracts the entire stage, which will be a full screenshot of the game but if you want to be more specific.

extract a sprite(untested but should work)
PHP:
function extractSpriteImage (sprite, quality = 0.1) {
  const renderer = window.Graphics._renderer.extract.canvas(sprite)
  return renderer.toDataURL('image/png', quality)
}

This returns a base64Data which can be used to write the image to a file. If you're making a character generator I assume you know how to write a file, so I'll leave that out.
nice libs easy thing with your fenix-tools lol nice jobs
you can write your own but still look at what he did for the saving part. You don't have to do it exactly the same way, but at least you'll learn how he did it and be able to learn from it.
Atm i cant understand all of his code, so i think i need to do my own.

you need study how work nwJS 🤘

here a copy past from my png map generator, take a look how it work,
it would be too long for me to explain, but take inspiration.
PHP:
            var fs = require('fs');
            var pathName = `img/parallaxes/${$dataMap.parallaxName}.png`;
            var snap = Bitmap.snap2(target);
            var urlData = snap._canvas.toDataURL();
            var base64Data = urlData.replace(/^data:image\/png;base64,/, "");
            fs.writeFile(pathName, base64Data, 'base64', function(error){
                if (error !== undefined && error !== null) {  console.error('An error occured while saving the screenshot', error); }
            });
            // PHOTOSHOP MODE
            if(option["c-save_psd"]){
                var newFolderPath = `img/parallaxes/_psd_${$dataMap.parallaxName}`;
                if(!fs.existsSync(newFolderPath)){ fs.mkdirSync(newFolderPath)}; // create folder if not exsit
                  
                for (let i = 0; i < target._spriteset.displayLayers.length; i++) {
                    let layer = target._spriteset.displayLayers[i];
                    let pathName = `${newFolderPath}/${$dataMap.parallaxName}_psd_layer${i}.png`;
                    let snap = Bitmap.snap2(layer);
                    let urlData = snap._canvas.toDataURL();
                    let base64Data = urlData.replace(/^data:image\/png;base64,/, "");
                    fs.writeFile(pathName, base64Data, 'base64', function(error){
                        if (error !== undefined && error !== null) {  console.error('An error occured while saving the screenshot', error); }
                    });
                }
            };

PHP:
    Bitmap.snap2 = function(stage) {
        var width =  $dataMap.width*48;
        var height = $dataMap.height*48;
        var bitmap = new Bitmap(width, height);
        var context = bitmap._context;
        var renderTexture = PIXI.RenderTexture.create(width, height);
        if (stage) {
            Graphics._renderer.render(stage, renderTexture);
            stage.worldTransform.identity();
            var canvas = null;
            if (Graphics.isWebGL()) {
                canvas = Graphics._renderer.extract.canvas(renderTexture);
            } else {
                canvas = renderTexture.baseTexture._canvasRenderTarget.canvas;
            }
            context.drawImage(canvas, 0, 0);
        } else {

        }
        renderTexture.destroy({ destroyBase: true });
        bitmap._setDirty();
        return bitmap;
    };
There is en error when i try to run your code, even if i tried to use srict.
GvUHsRQ.png
Atm i cant understand all of his code, so i think i need to do my own.
There is en error when i try to run your code, even if i tried to use srict.
Yep, it's normal it's just an example for a custom project. There is another module and variable necessaire are function.
Am sorry i don't have working compatible code with basic rmmv, but take a look on what @LTN Games give to you.
you need study how work nwJS 🤘

here a copy past from my png map generator, take a look how it work,
it would be too long for me to explain, but take inspiration.
PHP:
            var fs = require('fs');
            var pathName = `img/parallaxes/${$dataMap.parallaxName}.png`;
            var snap = Bitmap.snap2(target);
            var urlData = snap._canvas.toDataURL();
            var base64Data = urlData.replace(/^data:image\/png;base64,/, "");
            fs.writeFile(pathName, base64Data, 'base64', function(error){
                if (error !== undefined && error !== null) {  console.error('An error occured while saving the screenshot', error); }
            });
            // PHOTOSHOP MODE
            if(option["c-save_psd"]){
                var newFolderPath = `img/parallaxes/_psd_${$dataMap.parallaxName}`;
                if(!fs.existsSync(newFolderPath)){ fs.mkdirSync(newFolderPath)}; // create folder if not exsit
                  
                for (let i = 0; i < target._spriteset.displayLayers.length; i++) {
                    let layer = target._spriteset.displayLayers[i];
                    let pathName = `${newFolderPath}/${$dataMap.parallaxName}_psd_layer${i}.png`;
                    let snap = Bitmap.snap2(layer);
                    let urlData = snap._canvas.toDataURL();
                    let base64Data = urlData.replace(/^data:image\/png;base64,/, "");
                    fs.writeFile(pathName, base64Data, 'base64', function(error){
                        if (error !== undefined && error !== null) {  console.error('An error occured while saving the screenshot', error); }
                    });
                }
            };

PHP:
    Bitmap.snap2 = function(stage) {
        var width =  $dataMap.width*48;
        var height = $dataMap.height*48;
        var bitmap = new Bitmap(width, height);
        var context = bitmap._context;
        var renderTexture = PIXI.RenderTexture.create(width, height);
        if (stage) {
            Graphics._renderer.render(stage, renderTexture);
            stage.worldTransform.identity();
            var canvas = null;
            if (Graphics.isWebGL()) {
                canvas = Graphics._renderer.extract.canvas(renderTexture);
            } else {
                canvas = renderTexture.baseTexture._canvasRenderTarget.canvas;
            }
            context.drawImage(canvas, 0, 0);
        } else {

        }
        renderTexture.destroy({ destroyBase: true });
        bitmap._setDirty();
        return bitmap;
    };
If you take a look at this here https://gitlab.com/FeniXEngineMV/fenix-tools/blob/dev/src/Utils/extractCanvasImage.js
PHP:
function extractCanvasImage (quality = 0.1) {
  const renderer = window.Graphics._renderer
  const stage = renderer._lastObjectRendered
  renderer.render(stage)
  const data = renderer.view.toDataURL('image/png', quality)
  return data
}
Basically this extracts the entire stage, which will be a full screenshot of the game but if you want to be more specific.

extract a sprite(untested but should work)
PHP:
function extractSpriteImage (sprite, quality = 0.1) {
  const renderer = window.Graphics._renderer.extract.canvas(sprite)
  return renderer.toDataURL('image/png', quality)
}

This returns a base64Data which can be used to write the image to a file. If you're making a character generator I assume you know how to write a file, so I'll leave that out.
Hmm, idk but function extractSpriteImage is giving an error with canvas TypeError: Cannot read property 'canvas' of undefined.

Original thread: https://forums.rpgmakerweb.com/threads/save-sprite-as-png-picture.93560/