Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "pixfuck"
version = "0.1.1"
edition = "2021"

[dependencies]
image = "0.25"
rayon = "1.10"
palette = { version = "0.7", features = ["std"] }
clap = { version = "4.5", features = ["derive"] }
log = "0.4"
env_logger = "0.11"
dirs = "4.0"
56 changes: 0 additions & 56 deletions main.py

This file was deleted.

5 changes: 0 additions & 5 deletions requirements.txt

This file was deleted.

20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub mod sort;

use image::{ImageFormat, ImageReader};
use std::fs::File;
use std::path::Path;

pub use sort::{SortMode, sort_pixels};

pub fn sort_image(input_path: &str, output_path: &str, mode: SortMode) -> image::ImageResult<()> {
let img = ImageReader::open(input_path)?.decode()?.to_rgb8();
let sorted = sort_pixels(img, mode);
let ext = Path::new(output_path)
.extension()
.and_then(|s| s.to_str())
.unwrap_or("png");

let mut file = File::create(output_path)?;
let format = ImageFormat::from_extension(ext).unwrap_or(ImageFormat::Png);
sorted.write_to(&mut file, format)
}
33 changes: 33 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use pixfuck::{sort_image, SortMode};
use std::io::{self, Write};

fn main() {
println!("Welcome to Pixfuck! Please enter input file realpath:");
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let input = input.trim();

println!("Enter output file path:");
let mut output = String::new();
io::stdin().read_line(&mut output).unwrap();
let output = output.trim();

println!("Choose mode: 0=Hue, 1=Saturation, 2=Lightness, 3=Brightness");
let mut mode_str = String::new();
io::stdin().read_line(&mut mode_str).unwrap();
let mode = match mode_str.trim() {
"0" => SortMode::Hue,
"1" => SortMode::Saturation,
"2" => SortMode::Lightness,
"3" => SortMode::Brightness,
_ => {
eprintln!("Invalid mode, defaulting to Hue");
SortMode::Hue
}
};

match sort_image(input, output, mode) {
Ok(_) => println!("Image sorted and saved to this directory!"),
Err(e) => eprintln!("Error: {}", e),
}
}
26 changes: 26 additions & 0 deletions src/sort/brightness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use image::{Rgb, RgbImage};
use rayon::prelude::*;

pub fn sort(img: RgbImage) -> RgbImage {
let (w, h) = img.dimensions();
let mut rows: Vec<Vec<Rgb<u8>>> = (0..h)
.map(|y| (0..w).map(|x| *img.get_pixel(x, y)).collect())
.collect();

rows.par_iter_mut().for_each(|row| {
row.sort_by(|a, b| brightness(a).total_cmp(&brightness(b)));
});

let mut out = RgbImage::new(w, h);
for (y, row) in rows.iter().enumerate() {
for (x, px) in row.iter().enumerate() {
out.put_pixel(x as u32, y as u32, *px);
}
}

out
}

fn brightness(rgb: &Rgb<u8>) -> f32 {
0.299 * rgb[0] as f32 + 0.587 * rgb[1] as f32 + 0.114 * rgb[2] as f32
}
29 changes: 29 additions & 0 deletions src/sort/hue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use image::{Rgb, RgbImage};
use rayon::prelude::*;
use palette::{Srgb, Hsl, IntoColor};

pub fn sort(img: RgbImage) -> RgbImage {
let (w, h) = img.dimensions();
let mut rows: Vec<Vec<Rgb<u8>>> = (0..h)
.map(|y| (0..w).map(|x| *img.get_pixel(x, y)).collect())
.collect();

rows.par_iter_mut().for_each(|row| {
row.sort_by(|a, b| hue(a).partial_cmp(&hue(b)).unwrap());
});

let mut out = RgbImage::new(w, h);
for (y, row) in rows.iter().enumerate() {
for (x, px) in row.iter().enumerate() {
out.put_pixel(x as u32, y as u32, *px);
}
}

out
}

fn hue(rgb: &Rgb<u8>) -> f32 {
let Srgb { red, green, blue, .. } = Srgb::new(rgb[0], rgb[1], rgb[2]).into_format::<f32>();
let hsl: Hsl = Srgb::new(red, green, blue).into_color();
hsl.hue.into_degrees()
}
29 changes: 29 additions & 0 deletions src/sort/lightness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use image::{Rgb, RgbImage};
use rayon::prelude::*;
use palette::{Srgb, Hsl, IntoColor};

pub fn sort(img: RgbImage) -> RgbImage {
let (w, h) = img.dimensions();
let mut rows: Vec<Vec<Rgb<u8>>> = (0..h)
.map(|y| (0..w).map(|x| *img.get_pixel(x, y)).collect())
.collect();

rows.par_iter_mut().for_each(|row| {
row.sort_by(|a, b| light(a).partial_cmp(&light(b)).unwrap());
});

let mut out = RgbImage::new(w, h);
for (y, row) in rows.iter().enumerate() {
for (x, px) in row.iter().enumerate() {
out.put_pixel(x as u32, y as u32, *px);
}
}

out
}

fn light(rgb: &Rgb<u8>) -> f32 {
let Srgb { red, green, blue, .. } = Srgb::new(rgb[0], rgb[1], rgb[2]).into_format::<f32>();
let hsl: Hsl = Srgb::new(red, green, blue).into_color();
hsl.lightness
}
23 changes: 23 additions & 0 deletions src/sort/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub mod brightness;
pub mod hue;
pub mod lightness;
pub mod saturation;

use image::RgbImage;

#[derive(Clone, Copy)]
pub enum SortMode {
Hue,
Saturation,
Lightness,
Brightness,
}

pub fn sort_pixels(img: RgbImage, mode: SortMode) -> RgbImage {
match mode {
SortMode::Hue => hue::sort(img),
SortMode::Saturation => saturation::sort(img),
SortMode::Lightness => lightness::sort(img),
SortMode::Brightness => brightness::sort(img),
}
}
29 changes: 29 additions & 0 deletions src/sort/saturation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use image::{Rgb, RgbImage};
use rayon::prelude::*;
use palette::{Srgb, Hsl, IntoColor};

pub fn sort(img: RgbImage) -> RgbImage {
let (w, h) = img.dimensions();
let mut rows: Vec<Vec<Rgb<u8>>> = (0..h)
.map(|y| (0..w).map(|x| *img.get_pixel(x, y)).collect())
.collect();

rows.par_iter_mut().for_each(|row| {
row.sort_by(|a, b| sat(a).partial_cmp(&sat(b)).unwrap());
});

let mut out = RgbImage::new(w, h);
for (y, row) in rows.iter().enumerate() {
for (x, px) in row.iter().enumerate() {
out.put_pixel(x as u32, y as u32, *px);
}
}

out
}

fn sat(rgb: &Rgb<u8>) -> f32 {
let Srgb { red, green, blue, .. } = Srgb::new(rgb[0], rgb[1], rgb[2]).into_format::<f32>();
let hsl: Hsl = Srgb::new(red, green, blue).into_color();
hsl.saturation
}
4 changes: 0 additions & 4 deletions ui/__init__.py

This file was deleted.

Binary file removed ui/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
Binary file removed ui/__pycache__/pixel_sort_app.cpython-313.pyc
Binary file not shown.
Binary file removed ui/__pycache__/worker.cpython-313.pyc
Binary file not shown.
49 changes: 0 additions & 49 deletions ui/logger.py

This file was deleted.

Loading
Loading