Snap! image vectorizer collab

this is a collab with @sathvikrias anyone is welcome to join and help out tho.

so far we have a sobel operator Sobel Operator by avi_shor | Snap! Build Your Own Blocks

we plan on then drawing the image with pen without pixels and exporting the pen vectors

for anyone helping: also see Better lists custom blocks (collab) - #25 by avi_shor for further context

I plan on trying to replicate the system scan2cad uses

um what does it use. (also i may not respond for a while) i dont want to pay

to do this we want to group each color into its own object (max amount being 255). this will allow for the user to change how many colors they want. we can use the distance from rgba to rgba block I created. we can use Color quantization for this

pay?

we can also use a contrast system to further aid this

once this is done we will write a line follower to go around the parameter of each group and use tolerance to generate smooth lines instead of pixels

chat gpt made this for color quantization but we still got to figure out how to then draw this as vector

// Step 1: Load the image
when green flag clicked
clear
set image to <your-image>

define load image
set [image v] to (import [image-url v])
go to (0) (0)
stamp (image)

define extract colors
set [pixel-list v] to []
repeat (width of (image)) [
  repeat (height of (image)) [
    set [color v] to (color at (x) (y) of (image))
    add (color) to [pixel-list v]
  ]
]

define k-means clustering [k]
set [clusters v] to (empty list)
set [cluster-centers v] to (empty list)
repeat (k) [
  add (item (pick random (1) to (length of (pixel-list))) of (pixel-list)) to [cluster-centers v]
]
repeat (iterations) [
  set [clusters v] to (empty list)
  repeat (k) [
    add (empty list) to [clusters v]
  ]
  repeat (length of (pixel-list)) [
    set [color v] to (item (item #) of (pixel-list))
    set [nearest-cluster v] to (find nearest cluster to (color) in (cluster-centers))
    add (color) to (item (nearest-cluster) of (clusters))
  ]
  set [cluster-centers v] to (recalculate centers of (clusters))
]

define find nearest cluster to [color] in [centers]
set [nearest v] to (1)
set [min-distance v] to (distance between (color) and (item (1) of (centers)))
repeat (length of (centers)) [
  set [distance v] to (distance between (color) and (item (item #) of (centers)))
  if <(distance) < (min-distance)> then
    set [nearest v] to (item #)
    set [min-distance v] to (distance)
  end
]
report (nearest)

define distance between [color1] and [color2]
set [r1 v] to (item (1) of (color1))
set [g1 v] to (item (2) of (color1))
set [b1 v] to (item (3) of (color1))
set [r2 v] to (item (1) of (color2))
set [g2 v] to (item (2) of (color2))
set [b2 v] to (item (3) of (color2))
report ((sqrt (((r1) - (r2)) ^ (2) + ((g1) - (g2)) ^ (2) + ((b1) - (b2)) ^ (2))))

define recalculate centers of [clusters]
set [new-centers v] to (empty list)
repeat (length of (clusters)) [
  set [cluster v] to (item (item #) of (clusters))
  set [r-sum v] to (0)
  set [g-sum v] to (0)
  set [b-sum v] to (0)
  set [n v] to (length of (cluster))
  repeat (length of (cluster)) [
    set [r-sum v] to ((r-sum) + (item (1) of (item (item #) of (cluster))))
    set [g-sum v] to ((g-sum) + (item (2) of (item (item #) of (cluster))))
    set [b-sum v] to ((b-sum) + (item (3) of (item (item #) of (cluster))))
  ]
  add ([r-sum / n], [g-sum / n], [b-sum / n]) to [new-centers v]
]
report (new-centers)

define draw image
go to (0) (0)
repeat (width of (image)) [
  repeat (height of (image)) [
    set [color v] to (item (x * (width of (image)) + y) of (pixel-list))
    set [nearest-cluster v] to (find nearest cluster to (color) in (cluster-centers))
    set pen color to (item (nearest-cluster) of (cluster-centers))
    go to (x) (y)
    pen down
    pen up
  ]
]

// Main script
load image
extract colors
k-means clustering (5)  // 5 is the number of colors
draw image

why random?

i assume this is a variable, where is it coming from

idk chat gpt made it. u wanna try it out?

ik why. it isnt choosing k based on the image its picking a random hoping it will turn out good

vector1 script pic
using this we have to cluster the most similar colors for a better outcome

pretty much need to simplify an image to x amount of colors

updates?

Sleeping…

oh