Loading & Displaying

Pixels

Rendering

create-image

resize

create-image

Arguments
[w h format]clj[w h]cljs
Docstring

Creates a new datatype for storing images (PImage for clj and Image for cljs). This provides a fresh buffer of pixels to play with. Set the size of the buffer with the width and height parameters.

In clj the format parameter defines how the pixels are stored. See the PImage reference for more information. Possible formats: :rgb, :argb, :alpha (grayscale alpha channel)

Prefer using create-image over initialising new PImage (or Image) instances directly.

Works only inside sketch functions?
Yes
Original Processing method
createImage()
Original p5js method
createImage()
Example
clojure specific
  • (q/background 255)
    ; create image and draw gradient on it
    (let [im (q/create-image 100 100 :rgb)]
      (dotimes [x 100]
        (dotimes [y 100] (q/set-pixel im x y (q/color (* 2 x) (* 2 y) (+ x y)))))
      ; draw image twice
      (q/image im 0 0)
      (q/image im 50 50))
clojurescript specific
  • (q/background 255)
    ; create image and draw gradient on it
    (let [im (q/create-image 100 100)]
      (dotimes [x 100]
        (dotimes [y 100] (q/set-pixel im x y (q/color (* 2 x) (* 2 y) (+ x y)))))
      (q/update-pixels im)
      ; draw image twice
      (q/image im 0 0)
      (q/image im 50 50))
    try example

resize

Arguments
[img w h]
Docstring

Resize the image to a new width and height. To make the image scale proportionally, use 0 as the value for the wide or high parameter. For instance, to make the width of an image 150 pixels, and change the height using the same proportion, use (resize 150 0).

Even though a PGraphics is technically a PImage, it is not possible to rescale the image data found in a PGraphics. (It's simply not possible to do this consistently across renderers: technically infeasible with P3D, or what would it even do with PDF?) If you want to resize PGraphics content, first get a copy of its image data using the get() method, and call resize() on the PImage that is returned.

Works only inside sketch functions?
Yes
Original Processing method
resize()
Original p5js method
Image.resize()
Example
clojure specific
  • ; create image and draw gradient on it
    (let [im (q/create-image 100 100 :rgb)]
      (dotimes [x 100]
        (dotimes [y 100] (q/set-pixel im x y (q/color (* 2 x) (* 2 y) (+ x y)))))
      (q/image im 0 0)
      ; resize image from 100x100 to 50x50 and draw again
      (q/resize im 50 50)
      (q/image im 100 100))
clojurescript specific
  • ; create image and draw gradient on it
    (let [im (q/create-image 100 100)]
      (dotimes [x 100]
        (dotimes [y 100] (q/set-pixel im x y (q/color (* 2 x) (* 2 y) (+ x y)))))
      (q/update-pixels im)
      (q/image im 0 0)
      ; resize image from 100x100 to 50x50 and draw again
      (q/resize im 50 50)
      (q/image im 100 100))
    try example