Extending GP with Blocks to manipulate Pixels

Extending or modifying the GP system itself (advanced!)

Moderator: MSandro

Post Reply
mguzdial
Posts: 70
Joined: Sep 15th, '15, 11:21

Extending GP with Blocks to manipulate Pixels

Post by mguzdial » Sep 15th, '15, 18:05

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.
Attachments
mediaComp.gpp
Project that uses pixel blocks
(782.73 KiB) Downloaded 359 times
pixelExtensionv2.gp
Extension that provides pixel blocks
(4.01 KiB) Downloaded 383 times

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

Re: Extending GP with Blocks to manipulate Pixels

Post by JohnM » Sep 15th, '15, 20:12

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.

Post Reply