From 3ae737e20fffc090b2205ef97936d69024e35f88 Mon Sep 17 00:00:00 2001 From: Sakuhl <2012collector@gmail.com> Date: Fri, 12 Jan 2018 15:26:49 +0100 Subject: [PATCH] Initial commit --- .gitignore | 4 +++ Cargo.toml | 12 +++++++++ Procfile | 1 + RustConfig | 1 + src/bin/server.rs | 38 +++++++++++++++++++++++++++ src/lib.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 21 +++++++++++++++ 7 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 Procfile create mode 100644 RustConfig create mode 100644 src/bin/server.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..143b1ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +/target/ +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b365f34 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +authors = ["Sakuhl <2012collector@gmail.com>"] +name = "wynnautocrop" +version = "0.1.0" + +[dependencies] +image = "0.18.0" +imgur = "0.7.0" +palette = "0.2.1" +reqwest = "0.8.2" +rocket = "0.3.5" +rocket_codegen = "0.3.5" diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..7df4716 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: ROCKET_PORT=$PORT target/release/rust-server diff --git a/RustConfig b/RustConfig new file mode 100644 index 0000000..61a1c0e --- /dev/null +++ b/RustConfig @@ -0,0 +1 @@ +VERSION=nightly-2017-12-20 diff --git a/src/bin/server.rs b/src/bin/server.rs new file mode 100644 index 0000000..83af39f --- /dev/null +++ b/src/bin/server.rs @@ -0,0 +1,38 @@ +#![feature(plugin, custom_derive)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +extern crate reqwest; + +extern crate image; +use image::ImageFormat; + +extern crate imgur; + +extern crate wynnautocrop; +use wynnautocrop::*; + +use std::io::{Cursor, Read}; + +#[derive(FromForm)] +struct UrlForm { + url: String +} + +#[get("/crop?")] +fn crop_url(url: UrlForm) -> String { + let url = url.url; + let mut buf = Vec::new(); + reqwest::get(&url).unwrap().read_to_end(&mut buf).unwrap(); + let mut img = image::load(Cursor::new(&*buf), image::guess_format(&buf).unwrap()).unwrap(); + img = crop(img); + let mut buf = Vec::new(); + img.save(&mut buf, ImageFormat::PNG).unwrap(); + let handle = imgur::Handle::new("c819d6faf0219ac".to_owned()); + handle.upload(&buf).unwrap().link().unwrap().to_owned() +} + +fn main() { + rocket::ignite().mount("/", routes![crop_url]).launch(); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c71d173 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,67 @@ +extern crate image; +use image::{DynamicImage, GenericImage}; + +extern crate palette; +use palette::{Hsv, Rgb, FromColor}; + +use std::collections::HashSet; + +pub fn crop(mut img: DynamicImage) -> DynamicImage { + let mut minx = 0;//img.width(); + let mut maxx = 0; + let mut miny = 0;//img.height(); + let mut maxy = 0; + let mut points = HashSet::new(); + for x in 0..img.width() { + for y in 0..img.height() { + let rgb = img.get_pixel(x, y); + let hsv = Hsv::::from_rgb(Rgb::new_u8(rgb.data[0], rgb.data[1], rgb.data[2])); + let hue_diff = hsv.hue.to_positive_degrees() - 265.0; + if hue_diff > -16.0 && hue_diff < 10.0 && hsv.saturation > 0.7 && hsv.value > 0.18 { + //img.put_pixel(x, y, RED); + points.insert((x, y)); + /* + if x < minx { + minx = x; + } + if x > maxx { + maxx = x; + } + if y < miny { + miny = y; + } + if y > maxy { + maxy = y; + } + */ + } + } + } + for point in &points { + let x = point.0; + let y = point.1; + let mut x2 = x; + for next_x in x.. { + if !points.contains(&(next_x, y)) { + x2 = next_x; + break; + } + } + let mut y2 = y; + for next_y in y.. { + if !points.contains(&(x, next_y)) { + y2 = next_y; + break; + } + } + if (x2 - x) > (maxx - minx) { + maxx = x2; + minx = x; + } + if (y2 - y) > (maxy - miny) { + maxy = y2; + miny = y; + } + } + img.crop(minx, miny, maxx-minx, maxy-miny) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9854900 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,21 @@ +extern crate image; +use image::ImageFormat; + +extern crate wynnautocrop; +use wynnautocrop::*; + +use std::env; +use std::fs::File; + +fn main() { + let imgs = env::args().skip(1).collect::>(); + + for img in imgs { + let img_crop = img.clone() + ".crop.png"; + let mut img = image::open(img).unwrap(); + + img = crop(img); + + img.save(&mut File::create(img_crop).unwrap(), ImageFormat::PNG).unwrap(); + } +} \ No newline at end of file