Commit 3598ee4e authored by John's avatar John

Initial commit

parents
/target
**/*.rs.bk
/.idea
/.idea/*
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
[package]
name = "image_analyzer"
version = "0.1.0"
authors = ["TheBestJohn <[email protected]>"]
[[bin]]
name = "change"
path = "src/main.rs"
[[bin]]
name = "pi"
path = "src/pi.rs"
[dependencies]
palette = "0.4.1"
image = "0.19.0"
https://pythonprogramming.net/k-means-from-scratch-machine-learning-tutorial/
https://buzzrobot.com/dominant-colors-in-an-image-using-k-means-clustering-3c7af4622036
\ No newline at end of file
img.jpg

12.4 KB

This diff is collapsed.
This diff is collapsed.
extern crate image;
//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 main() {
let img = image::open("img.jpg").unwrap();
let dimensions = img.dimensions();
let mut buff:image::RgbaImage = ImageBuffer::new(dimensions.0,dimensions.1);
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);
}
buff.save("test.png").unwrap();
}
\ No newline at end of file
extern crate image;
extern crate palette;
use image::{ImageBuffer};
use std::time::{Instant};
use std::io::{BufRead, BufReader};
use std::fs::File;
use palette::{Srgb, Shade, Pixel};
//We're cheating here. To do better, see https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
//https://stackoverflow.com/questions/35385703/read-file-character-by-character-in-rust
fn draw_dat_boi(target:u32) -> [u8;3]{
let base = Srgb::new(0.62, 0.3, 0.3).into_linear();
let lighter = base.lighten(0.04*target as f32);
let pixel: [u8; 3] = Srgb::from_linear(lighter).into_format().into_raw();
pixel
// let colors:[[_; 3];10] = [[0,0,0],[255,0,0],[0,255,0],pixel,[255,255,0],[255,0,255],[0,255,255],[255, 165, 0],[127,127,127],[255,255,255]];
// println!("{:?}", colors[target as usize]);
// colors[target as usize]
}
fn main() {
let now = Instant::now();
let mut buff:image::RgbImage = ImageBuffer::new(1000,1001);
let mut row = 0;
let mut col = 0;
let mut f = BufReader::new(File::open("pi.txt").expect("open failed"));
let mut buf = Vec::<u8>::new();
while f.read_until(b'\n', &mut buf).expect("read_until failed") != 0 {
// this moves the ownership of the read data to s
// there is no allocation
let s = String::from_utf8(buf).expect("from_utf8 failed");
for c in s.chars() {
let color = draw_dat_boi(c.to_digit(10).unwrap());
buff.put_pixel(col,row,image::Rgb(color));
col += 1;
if col > 999 { col=0; row += 1; };
}
// this returns the ownership of the read data to buf
// there is no allocation
buf = s.into_bytes();
buf.clear();
buff.save("pi.png").unwrap();
};
println!("{}", now.elapsed().as_secs());
}
\ No newline at end of file
test.png

114 KB

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