RASTERS

 

This is the main code I use when experimenting with raster images (as described in these two posts [1 & 2]). I set the resolution by the size variable. A smaller number than 4 will probably result in a outOfMemory error (depending on the size of the image). Anyway, it is just a routine to iterate through the chosen image and reads the average color value of the square represented by the variable “rectangle” in line 20. Insert any desired function call on line 23 to perform any kind of operation of ones liking. I usually pass these four objects along to the function:

x, y, sampleSize, color

More arguments can be added if required.

mosaics(x, y, sampleSize, color, 1)

The function call above passes some arguments to the function below…

function mosaics(x_coord, y_coord, size, color, strokeWidthModifier){
		var path = new Path([
			new Point(x_coord + 0.5, y_coord) * size,
			new Point(x_coord + 0.5, y_coord + 1) * size ])

			path.strokeColor = color
			path.strokeWidth = size.width / strokeWidthModifier
			path.fillColor = null
}

…which will produce a result such as this.

But I digress, here is the code:

var getRasters = document.getItems([Raster, PlacedFile], {selected: true})
raster = getRasters.first
if(raster instanceof PlacedFile && !raster.eps) raster = raster.embed(false)

if(raster){
		var size = 5
		var sampleSize = new Size(size, size)
		var rasterWidth = raster.bounds.width
		var rasterHeight = raster.bounds.height
		var offset = raster.bounds.topLeft
		var columns = Math.round(rasterWidth / size)
		var rows = Math.round(rasterHeight / size)
		var draw = true

		if(draw){
			for(var y = 0; y < rows; y++){
				for(var x = 0; x < columns; x++){

					var topLeft = offset + new Point(x, y) * sampleSize
					var rectangle = new Rectangle(topLeft, sampleSize)
					var color = raster.getAverageColor(rectangle)

					// INSERT FUNCTION CALL HERE.
				}
			}
		}
}