Commit c363831d authored by John's avatar John

implemented image resizing.

parent c15cc83f
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas Template</title>
<style>
body {
margin: 0;
}
</style>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="jcanvas.min.js"></script>
<script>
var colors = []
$.getJSON( "./colors.json",function( data ){
console.log(data);
for (var i = 0; i < data.length; i++) {
colors.push(data[i]);
}
});
function draw(){
console.log("Drawing");
// console.log(json);
var $myCanvas = $('#myCanvas');
for (var i = 0; i < colors.length; i++) {
$myCanvas.drawRect({
fillStyle: "#"+colors[i],
strokeStyle: 'blue',
strokeWidth: 0,
x: (i*100)+200, y: 200,
fromCenter: false,
width: 100,
height: 100
});
}
}
$("document").ready(draw());
</script>
</head>
<body>
<canvas id="myCanvas" width="1000" height="1000">
</canvas>
</body>
</html>
\ No newline at end of file
["6b97c1",
"a3b5d2",
"2c2c3e",
"435c7f",
"517da7"
]
\ No newline at end of file
This diff is collapsed.
extern crate image;
extern crate rusty_machine;
use image::{FilterType, DynamicImage, GenericImage};
use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::{KMeansClassifier};
use rusty_machine::learning::UnSupModel;
//extern crate palette;
use image::{GenericImage, ImageBuffer};
fn get_color(px:image::Rgba<u8>) {
println!("{:?}",px);
}
fn modify(val:u8, percent:u32)-> u8{
let ret=((val as u32*percent)+50)/100;
ret.min(255) as u8
}
//fn get_color(px:image::Rgba<u8>) {
// println!("{:?}",px);
//
//}
//
//fn modify(val:u8, percent:u32)-> u8{
// let ret=((val as u32*percent)+50)/100;
// ret.min(255) as u8
//}
fn convert(centroids:&Matrix<f64>){
for i in 0..centroids.rows() {
println!("{:?}",centroids.get_row(i).unwrap())
let mut colorpalette = Vec::new();
for i in 0..centroids.rows() {
let colors = centroids.get_row(i).unwrap();
let hex = format!("#{:02x}{:02x}{:02x}",colors[0] as u8, colors[1] as u8, colors[2] as u8);
colorpalette.push(hex);
}
println!("{:?}", colorpalette)
}
fn main() {
let img = image::open("smalltest.jpg").unwrap();
fn resize_image(img:&DynamicImage, maxpix: f64) -> DynamicImage{
let dimensions = img.dimensions();
let mut buff:image::RgbaImage = ImageBuffer::new(dimensions.0,dimensions.1);
// Height = Ratio * Width, Area = Width * height, Width = sqrt(Area/Ratio)
// Width = sqrt(A*w0/h0)
let new_width = (maxpix * dimensions.0 as f64 / dimensions.1 as f64).sqrt();
let new_height = (maxpix * dimensions.1 as f64 / dimensions.0 as f64).sqrt();
let scaled = img.resize(new_width as u32, new_height as u32, FilterType::Gaussian);
scaled
}
let mut pixelData = Vec::new();
let pixeliter = img.pixels() ;
for pixel in pixeliter {
//get_color(pixel.2);
let mut newpix = pixel.clone();
pixelData.push(newpix.2[0] as f64);
pixelData.push(newpix.2[1] as f64);
pixelData.push(newpix.2[2] as f64);
fn main() {
let maxpix = 100000.0;
let orig_img = image::open("img.jpg").unwrap();
let orig_dimensions = orig_img.dimensions();
let mut img = orig_img;
if orig_dimensions.0 * orig_dimensions.1 > maxpix as u32 {
img = resize_image(&img, maxpix);
}
println!("{:?}",(dimensions.0*dimensions.1) as usize);
let a = Matrix::new((dimensions.0*dimensions.1) as usize,3,pixelData);
let mut model = KMeansClassifier::new(5);
let dimensions = img.dimensions();
println!("dimensions {:?}",dimensions);
let mut pixel_data = Vec::new();
let pixeliter = img.pixels();
for pixel in pixeliter {
pixel_data.push(pixel.2[0] as f64);
pixel_data.push(pixel.2[1] as f64);
pixel_data.push(pixel.2[2] as f64);
}
println!("{:?} Total Pixels",(dimensions.0*dimensions.1) as usize);
let a = Matrix::new((dimensions.0*dimensions.1) as usize,3,pixel_data);
let mut model = KMeansClassifier::new(5);
model.train(&a).unwrap();
println!("{:?}",model);
let centroids = model.centroids().as_ref().unwrap();
println!("Model Centroids:\n{:.0}", centroids);
// println!("Model Centroids:\n{:.0}", centroids);
convert(centroids);
// let slice = MatrixSlice::from_matrix(&centroids, [0,0], 1, 3);
let slice = centroids.get_row(0).unwrap();
println!("{:?}", slice);
// let a = model.predict(&test_inputs).unwrap();
// println!("{:?}", e);
// println!("{:?}", m.data());
//
// let pixeliter = img.pixels() ;
// for pixel in pixeliter{
// //get_color(pixel.2);
// let mut newpix = pixel.clone();
// let mut red = newpix.2[0];
// let mut green = newpix.2[1];
// let mut blue = newpix.2[2];
//
// newpix.2[0] = modify(green, 200);
// newpix.2[1]=blue;
// newpix.2[2]=red ;
//
// buff.put_pixel(newpix.0,newpix.1,newpix.2);
// }
// println!("test");
//
// let slice = centroids.get_row(0).unwrap();
//
// buff.save("test.png").unwrap();
// println!("{:?}", slice);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment