Initial commit

This commit is contained in:
Sakuhl 2018-01-12 15:26:49 +01:00
commit 3ae737e20f
7 changed files with 144 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target/
**/*.rs.bk
Cargo.lock

12
Cargo.toml Normal file
View File

@ -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"

1
Procfile Normal file
View File

@ -0,0 +1 @@
web: ROCKET_PORT=$PORT target/release/rust-server

1
RustConfig Normal file
View File

@ -0,0 +1 @@
VERSION=nightly-2017-12-20

38
src/bin/server.rs Normal file
View File

@ -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?<url>")]
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();
}

67
src/lib.rs Normal file
View File

@ -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::<f64>::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)
}

21
src/main.rs Normal file
View File

@ -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::<Vec<_>>();
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();
}
}