Page 1 of 1

Extending GP with Blocks to manipulate Pixels

Posted: Sep 15th, '15, 18:05
by mguzdial
In our Media Computation approach to teaching intro CS (see http://www.mediacomputation.org), we write Python code that looks like this:

Code: Select all

def decreaseRed(picture):
  for pixel in getPixels(picture):
    r = getRed(pixel)
    setRed(pixel, r * 0.5)
I wanted to be able to do similar things in GP, and with help from the team (and lots of rooting around in the code), I can now.

Here is my extension (attached). Enter developer mode, then "load source file" from the Stage menu. Load in the PixelExtension. You'll get a new set of blocks that will let you get all the pixels in an image, and get/set individual red, green, and blue channels on those pixels. The block to "copy costume" prevents you from messing with the image in the project. (Leave it out, and you can change the images attached to the project.) With these blocks, I can write code like this:
mediacomp-in-GP.png
Example blocks
I'm attaching an example project using these blocks.

The PixelExtension file is just plain text. I'll be happy to answer questions about it.

Re: Extending GP with Blocks to manipulate Pixels

Posted: Sep 15th, '15, 20:12
by JohnM
Thanks for sharing this, Mark!

Over the next year, the GP team will work on easier ways to create and share extensions -- ideally, constructing all the extension code in GP itself without using an external text editor -- and to encapsulate extensions in modules to prevent accidental conflicts between different extensions. Meanwhile, it's great to have a way for people to experiment with their own extensions right now. In addition, your pixel extension will provids a concrete, real-world example of an extension that will help inform the design of the module-based extension mechanism.