Skip to content

Instantly share code, notes, and snippets.

@artyom-poptsov
Created August 31, 2024 12:04
Show Gist options
  • Save artyom-poptsov/72fe27b7441f984262c99ee2b27082a8 to your computer and use it in GitHub Desktop.
Save artyom-poptsov/72fe27b7441f984262c99ee2b27082a8 to your computer and use it in GitHub Desktop.
Another Guile-PNG generative art.
#!/usr/bin/env -S guile -L modules -e main -s
!#
(use-modules (rnrs bytevectors)
(png)
(png image)
(png graphics pixel))
(define (main args)
(let* ((image (png->scm (current-input-port)))
(image-height (png-image-height image)))
(set! *random-state* (random-state-from-platform))
(for-each
(lambda (column-index)
(for-each (lambda (row-index)
(let* ((pixel (png-image-pixel-ref image
column-index
row-index))
(red (bytevector-u8-ref pixel 0))
(green (bytevector-u8-ref pixel 1))
(blue (bytevector-u8-ref pixel 2))
(gen-color
(lambda (color)
(euclidean-remainder (+ (abs (- 255 color))
(random (+ row-index 1)))
255))))
(bytevector-u8-set! pixel 0 (gen-color red))
(bytevector-u8-set! pixel 1 (gen-color green))
(bytevector-u8-set! pixel 2 (gen-color blue))
(png-image-pixel-set! image column-index row-index pixel)))
(iota (random image-height))))
(iota (- (png-image-width image) 1)))
(scm->png image)))
@artyom-poptsov
Copy link
Author

#!/usr/bin/env -S guile -L modules -e main -s
!#

(use-modules (rnrs bytevectors)
             (png)
             (png image)
             (png graphics pixel))

(define (main args)
  (let* ((image        (png->scm (current-input-port)))
         (image-height (png-image-height image))
         (block-size   10)
         (gen-color
          (lambda (color r)
            (euclidean-remainder (abs (- 255 color r))
                                 255))))
    (set! *random-state* (random-state-from-platform))
    (for-each
     (lambda (column-index)
       (let ((r (random (+ column-index 1)))
             (g (random (+ column-index 1)))
             (b (random (+ column-index 1)))
             (row-length (iota (random image-height))))
         (for-each (lambda (ci)
                     (for-each (lambda (row-index)
                                 (let* ((pixel (png-image-pixel-ref image
                                                                    ci
                                                                    row-index))
                                        (red   (bytevector-u8-ref pixel 0))
                                        (green (bytevector-u8-ref pixel 1))
                                        (blue  (bytevector-u8-ref pixel 2)))
                                   (bytevector-u8-set! pixel 0 (gen-color red r))
                                   (bytevector-u8-set! pixel 1 (gen-color green g))
                                   (bytevector-u8-set! pixel 2 (gen-color blue b))
                                   (png-image-pixel-set! image ci row-index pixel)))
                               row-length))
                   (iota block-size column-index))))
     (iota (/ (png-image-width image) block-size) 0 block-size))
    (scm->png image)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment