revers of GetPixelData( "RawBase64") array to img

108 views
Skip to first unread message

Matteo De vita

unread,
Feb 9, 2023, 8:34:58 AM2/9/23
to DroidScript
Good morning
my project aims to read a color image and, by applying a series of algorithms, to generate a printable Gcode on a B/W plotter.
I read an image from a jpg file.
I apply to the image
raw = img.GetPixelData( "RawBase64") 
var byteArray = atob(raw); ....
starting from this array, at the end of the process I get a two-dimensional array of the same size as the input image. 

 I would like  to do  the inverse operation of GetPixelData,  rather fron array of colors (RGB) to img. 
 I tried DrawPoin pixel by pixel on the source image using
 Img.SetAutoUpdate( onoff ) to increase speed but stay slow.
thank for the suggestions

Cemal

unread,
Feb 9, 2023, 8:53:26 AM2/9/23
to DroidScript
You can try to convert it to image again with img.setPixelData.

Right2TheV0id

unread,
Feb 9, 2023, 12:37:52 PM2/9/23
to DroidScript
Symbroson pointed out that the SetPixelData method doesn't work with rawbase64, but "the jimp plugin can convert it to png or jpgbase64" (src: https://groups.google.com/g/androidscript/c/tHbbfqe839Q/m/zFWvk8ovCQAJ).
If you're like me and never tried the Jimp plugin, here's a very slightly modified version of the Jimp demo as a starting point:

var imagePath = app.RealPath( "/Sys/Img/Hello.png" );

function OnStart()
{
    app.ShowProgress( "Loading Jimp..." );
    app.LoadPlugin( "Jimp" );
    app.HideProgress();
   
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
   
    img = app.CreateImage( imagePath, -1, 0.25 );
    lay.AddChild( img );
   
    btn = app.CreateButton( "Process", 0.3, 0.1 );
    btn.SetMargins( 0, 0.05, 0, 0 );
    btn.SetOnTouch( btn_OnTouch );
    lay.AddChild( btn );
   
    app.AddLayout( lay );
}

function btn_OnTouch()
{
    Jimp.read( imagePath, Process );
}

function Process( err, robot )
{
    if( typeof err === "string" )
        return alert( "Process():\n\n" + err );
   
    if( !robot )
        return alert( "Process():\n\n" + "robot is undefined" );
   
    app.ShowProgress();
    robot.resize( 300, 300 );
    robot.quality( 80 );
    robot.greyscale();
    robot.rotate( 45, false );
    robot.getBase64( Jimp.MIME_PNG, SetData );
    app.HideProgress();
}

function SetData( err, src )
{
    if( err )
        return alert( "SetData():\n\n" + err );
   
    img.SetPixelData( src );
}

Matteo De vita

unread,
Feb 10, 2023, 4:02:09 AM2/10/23
to DroidScript
thanks for the directions
  the example provided works perfectly.
now I study the "Jimp". 
Greetings
Matteo

Reply all
Reply to author
Forward
0 new messages