I’ve been looking at Nifty Assignments. I thought it would be fun to look at the first assignment and see what it’d look like in Racket.
#lang racket ;; The Iron Image Puzzle ;; Part of the Nifty Assignments page by Nick Parlante. ;; http://nifty.stanford.edu/2011/parlante-image-puzzle/ ;; We’ll use the 2htdp/image library for this one. (require 2htdp/image) ;; First, grab the image by its url. (define distorted-image (bitmap/url “http://nifty.stanford.edu/2011/parlante-image-puzzle/iron-puzzle.png”)) ;; We will want to decompose the image into its individual pixels. We can use ;; image->color-list to do this. ;; To make experimenting with the pixels an easy thing to do, let’s write ;; a function that will let us “map” across the pixels of an image ;; to get another image. (define (image-transform an-image a-pixel-transform) (define colors (image->color-list an-image)) (define new-colors (map a-pixel-transform colors)) (color-list->bitmap new-colors (image-width an-image) (image-height an-image))) ;; With image-transform in hand, we can apply a filter, ;; such as zeroing out the blue and green components like this: (define (zero-blue-green a-color) (color (color-red a-color) 0 0)) (define dark-image (image-transform distorted-image zero-blue-green)) ;; The image is still a bit darkened. Let’s bump up the red component ;; by a factor of ten. (define (red*10 a-color) (color (* (color-red a-color) 10) (color-green a-color) (color-blue a-color))) (define red-eye-image (image-transform dark-image red*10)) ;; And now we should be able to look at red-eye-image and recognize ;; the landmark.