Page 1 of 1

read image files

Posted: Aug 15th, '17, 00:31
by Gosha
It would be nice if you could read image files the same way you read text files. It looks like the read file block is already supposed to do that, but when I try, it returns an empty string.

Re: read image files

Posted: Oct 10th, '17, 00:16
by JohnM
To read binary data, such as an image file, you need to pass 'true' as the second parameter. However, the block in the palette doesn't have a second parameter, although it should.

Here's a work-around for advanced users. First, enter developer mode using the right-click menu on the stage. Then, open a workspace using the "workspace..." command in the stage menu and enter:

Code: Select all

readFile 'myFile' true
like this:
Screen Shot 2017-10-09 at 8.03.06 PM.png
Select that text and use alt-B (or cmd-B on a Mac) to turn the text into a block. You can also right-click on the selected text and select "blockify it..." from the menu. That will give you a block with a boolean second parameter set to true:
Screen Shot 2017-10-09 at 8.02.00 PM.png
the result of this block is a "BinaryData" object. To access individual bytes in a BInaryData object, you need to use "byteAt". That's another block that is not in the palette, so you'll need to "blockify" that, too.

Finally, to decode PNG into a bitmap, you'll to blockify:

Code: Select all

(readFrom (new 'PNGReader') data)
This might be more technical detail than you wanted, but I hope some of it is helpful.