read image files

Request a new command, feature, or improvement

Moderator: MSandro

Post Reply
Gosha
Posts: 16
Joined: Mar 29th, '17, 15:26

read image files

Post by Gosha » Aug 15th, '17, 00:31

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.

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: read image files

Post by JohnM » Oct 10th, '17, 00:16

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.

Post Reply