mirror of
https://github.com/Geckon01/Watermark.Net.git
synced 2026-06-17 08:37:48 +00:00
First commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Watrmark.Net_CLI
|
||||
{
|
||||
internal class Extensions
|
||||
{
|
||||
private static void ClearRow(int row)
|
||||
{
|
||||
Console.SetCursorPosition(0, row);
|
||||
Console.Write(new String(' ', Console.WindowWidth));
|
||||
Console.SetCursorPosition(0, row);
|
||||
}
|
||||
|
||||
private static void DrawTextProgressBar(double percentComplite)
|
||||
{
|
||||
int totalChunks = Console.WindowWidth / 2;
|
||||
|
||||
//draw empty progress bar
|
||||
Console.CursorLeft = 0;
|
||||
Console.Write("["); //start
|
||||
Console.CursorLeft = totalChunks + 1;
|
||||
Console.Write("]"); //end
|
||||
Console.CursorLeft = 1;
|
||||
|
||||
int numChunksComplete = Convert.ToInt16(totalChunks * percentComplite);
|
||||
|
||||
//draw completed chunks
|
||||
Console.BackgroundColor = ConsoleColor.Green;
|
||||
Console.Write("".PadRight(numChunksComplete));
|
||||
|
||||
//draw incomplete chunks
|
||||
Console.BackgroundColor = ConsoleColor.Gray;
|
||||
Console.Write("".PadRight(totalChunks - numChunksComplete));
|
||||
|
||||
//draw totals
|
||||
Console.CursorLeft = totalChunks + 5;
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
}
|
||||
|
||||
public static void DrawStats(string imagePath, int filesComplite, int filesTotal, Stopwatch stopwatch)
|
||||
{
|
||||
var complitePercent = Convert.ToDouble(filesComplite) / filesTotal;
|
||||
var operationsPerSecond = Convert.ToDouble(filesComplite) / stopwatch.Elapsed.TotalSeconds;
|
||||
|
||||
Console.CursorVisible = false;
|
||||
ClearRow(0);
|
||||
Console.WriteLine($"Processed file: {imagePath}");
|
||||
DrawTextProgressBar(complitePercent);
|
||||
Console.Write($"{Math.Round(complitePercent * 100, 0)}% \t");
|
||||
Console.WriteLine($"{filesComplite} of {filesTotal}");
|
||||
ClearRow(2);
|
||||
Console.WriteLine($"{Math.Round(operationsPerSecond, 0)} per second");
|
||||
}
|
||||
|
||||
public static void DrawCompliteStats(TimeSpan elapsedTime)
|
||||
{
|
||||
Console.Clear();
|
||||
Console.SetCursorPosition(0, 0);
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"Work complite in {elapsedTime.Minutes} min. {elapsedTime.TotalSeconds} sec.");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using CommandLine;
|
||||
using Watermark.Net.src.WatermarkNet.Types;
|
||||
using System.Diagnostics;
|
||||
using Watermark.Net.src.WatermarkNet.Core;
|
||||
using Watrmark.Net_CLI.Watermakr.Net.CLI.Enums;
|
||||
using Watrmark.Net_CLI.Watermark.Net.CLI.Models;
|
||||
using Watrmark.Net_CLI.Watermak.Net.CLI.Constants;
|
||||
using Watrmark.Net_CLI;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
Parser.Default.ParseArguments<ConsoleOptions>(args)
|
||||
.WithParsed<ConsoleOptions>(option => {
|
||||
switch (option.WatermarkType)
|
||||
{
|
||||
case WatermarkType.Image:
|
||||
if (option.FilePath != null)
|
||||
ProccessSingleFile(option);
|
||||
if (option.DirectoryPath != null)
|
||||
ProccessDirectory(option);
|
||||
break;
|
||||
case WatermarkType.Text:
|
||||
if (option.FilePath != null)
|
||||
ProccessSingleText(option);
|
||||
else
|
||||
ProccessDirectoryText(option);
|
||||
break;
|
||||
}
|
||||
|
||||
})
|
||||
.WithNotParsed(error => { });
|
||||
|
||||
static void ProccessSingleFile(ConsoleOptions options)
|
||||
{
|
||||
|
||||
}
|
||||
static void ProccessDirectory(ConsoleOptions options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void ProccessSingleText(ConsoleOptions options)
|
||||
{
|
||||
|
||||
}
|
||||
static void ProccessDirectoryText(ConsoleOptions options)
|
||||
{
|
||||
if (options.WatermarkText == null || options.WatermarkText == string.Empty)
|
||||
throw new ArgumentNullException("Watermark text can not be null");
|
||||
if(options.DirectoryPath == null || !Directory.Exists(options.DirectoryPath))
|
||||
throw new ArgumentNullException("Specified files directory not found");
|
||||
if (options.OutputPath == null || !Directory.Exists(options.OutputPath))
|
||||
throw new ArgumentNullException("Specified output directory not found");
|
||||
|
||||
var directoryFiles = Directory.GetFiles(options.DirectoryPath);
|
||||
|
||||
var filesTotal = directoryFiles.Length;
|
||||
var chunkSize = filesTotal / (options.ThreadsNumber ?? Environment.ProcessorCount);
|
||||
var filesChunks = directoryFiles.ToList().Chunk(chunkSize < 1 ? 1: chunkSize);
|
||||
var filesComplite = 0;
|
||||
|
||||
var watermark = new TextWatermark{
|
||||
Text = options.WatermarkText,
|
||||
Color = options.WatermarkColor ?? Constans.DefaultTextColor,
|
||||
Position = options.WatermarkPositon ?? Constans.DefaultWatermarkPosition,
|
||||
BackroundColor = options.WatermarkBackround ?? Constans.DefaultBackroundColor,
|
||||
Font = Constans.DefaultWatermarkFont,
|
||||
Scale = options.WatermarkScale ?? Constans.DefaultWatermarkScale
|
||||
};
|
||||
var watermarker = new Watermarker();
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
Console.Clear();
|
||||
|
||||
Parallel.ForEach(filesChunks, new ParallelOptions { MaxDegreeOfParallelism = options.ThreadsNumber ?? Environment.ProcessorCount }, chunk =>
|
||||
{
|
||||
foreach (var imagePath in chunk)
|
||||
{
|
||||
var resultedImage = watermarker.ProcessImage(imagePath, options.OutputPath, watermark);
|
||||
|
||||
lock (stopwatch)
|
||||
{
|
||||
Extensions.DrawStats(resultedImage.Path, ++filesComplite, filesTotal, stopwatch);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
stopwatch.Stop();
|
||||
Extensions.DrawCompliteStats(stopwatch.Elapsed);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Watrmark.Net CLI": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "--type Text --text Lorem -p Center -o \"Z:\\Загрузки\\testout\" -d \"Z:\\Загрузки\\test\\1\" --scale 2 --threads 8"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using SixLabors.Fonts;
|
||||
using Watermark.Net.src.WatermarkNet.Enums;
|
||||
|
||||
namespace Watrmark.Net_CLI.Watermak.Net.CLI.Constants
|
||||
{
|
||||
internal class Constans
|
||||
{
|
||||
public static readonly SixLabors.ImageSharp.Color DefaultTextColor = SixLabors.ImageSharp.Color.LightGray;
|
||||
public static readonly SixLabors.ImageSharp.Color DefaultBackroundColor = SixLabors.ImageSharp.Color.White;
|
||||
public static readonly ImagePosition DefaultWatermarkPosition = ImagePosition.Center;
|
||||
public static readonly Font DefaultWatermarkFont = SystemFonts.CreateFont("Tahoma", 14);
|
||||
public static readonly float DefaultWatermarkScale = 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Watrmark.Net_CLI.Watermark.Net.CLI.Models;
|
||||
using Watermark.Net.src.WatermarkNet.Enums;
|
||||
using CommandLine;
|
||||
|
||||
namespace Watrmark.Net_CLI.Watermakr.Net.CLI.Enums
|
||||
{
|
||||
internal class ConsoleOptions
|
||||
{
|
||||
[Option("type", Required = true, HelpText = "")]
|
||||
public WatermarkType WatermarkType { get; set; }
|
||||
|
||||
[Option('f', "file", Required = false, HelpText = "")]
|
||||
public string? FilePath { get; set; }
|
||||
|
||||
[Option('d', "directory", Group = "wmoptions", HelpText = "")]
|
||||
public string? DirectoryPath { get; set; }
|
||||
|
||||
[Option('w', "watermark", Group = "wmoptions", HelpText = "")]
|
||||
public string? WatermarkPath { get; set; }
|
||||
|
||||
[Option('o', "output", Required = true, HelpText = "")]
|
||||
public string? OutputPath { get; set; }
|
||||
|
||||
[Option("text", Group = "wmoptions", HelpText = "")]
|
||||
public string? WatermarkText { get; set; }
|
||||
|
||||
[Option('c', "color", Required = false, HelpText = "")]
|
||||
public SixLabors.ImageSharp.Color? WatermarkColor { get; set; }
|
||||
|
||||
[Option('b', "wmbackroud", Required = false, HelpText = "")]
|
||||
public SixLabors.ImageSharp.Color? WatermarkBackround { get; set; }
|
||||
|
||||
[Option('s', "scale", Required = false, HelpText = "")]
|
||||
public float? WatermarkScale { get; set; }
|
||||
|
||||
[Option('p', "position", Required = false, HelpText = "")]
|
||||
public ImagePosition? WatermarkPositon { get; set; }
|
||||
|
||||
[Option("threads", Required = false, HelpText = "")]
|
||||
public int? ThreadsNumber { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Watrmark.Net_CLI.Watermark.Net.CLI.Models
|
||||
{
|
||||
internal enum WatermarkType
|
||||
{
|
||||
Image,
|
||||
Text
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Watrmark.Net_CLI</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Copyright>Geckon01</Copyright>
|
||||
<AssemblyVersion>0.24.7.1</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Watermark.NET\Watermark.Net.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user