/// <summary>
        /// Converts all images in a folder to JPG
        /// </summary>
        private static void ToJpg(IReadOnlyCollection <string> files, ConnectionViewModel cvm)
        {
            var encoder           = System.Drawing.Imaging.Encoder.Quality;
            var encoderParameters = new EncoderParameters(1)
            {
                Param = { [0] = new EncoderParameter(encoder, 85L) }
            };

            var jpgEncoder = GetEncoder(ImageFormat.Jpeg);

            foreach (var file in files)
            {
                var extension = System.IO.Path.GetExtension(file);
                if (extension != ".png")
                {
                    continue;
                }
                var name = System.IO.Path.GetFileNameWithoutExtension(file);
                var path = System.IO.Path.GetDirectoryName(file);
                lock (_locker)
                {
                    cvm.Status = $"Converting {++_globalCount}/{_allCount}";
                }

                var png = Image.FromFile(file);
                png.Save($@"{path}\{name}.jpg", jpgEncoder, encoderParameters);
                png.Dispose();
                File.Delete(file);
            }
        }
 public MainWindow()
 {
     InitializeComponent();
     _target     = "data_dst";
     Cvm         = new ConnectionViewModel();
     DataContext = Cvm;
 }
        public static void StartDissimilar(string dir, string _target, ConnectionViewModel cvm)
        {
            if (string.IsNullOrEmpty(dir))
            {
                MessageBox.Show("Select workspace directory first.", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            var path    = Path.Combine(dir, _target, "aligned");
            var dirInfo = new DirectoryInfo(path);

            if (!Directory.Exists(path))
            {
                MessageBox.Show("Aligned directory does not exist.", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            var target_path = Path.Combine(dir, _target, "aligned_most_dissimilar");

            if (!Directory.Exists(target_path))
            {
                Directory.CreateDirectory(target_path);
            }

            var info   = dirInfo.GetFiles("*.*");
            var source = new List <string>();

            foreach (var f in info.OrderBy(r => r.Name))
            {
                source.Add(f.Name);
            }

            var tsk =
                Task.Factory.StartNew(() =>
            {
                return(FoundDissimilar(source, path, cvm));
            });

            while (!tsk.IsCompleted)
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }
            cvm.Status = "Ready.";
            var most_dis = tsk.Result;

            int rcount = 0;

            foreach (var f in most_dis)
            {
                try
                {
                    File.Copy(Path.Combine(path, f), Path.Combine(target_path, f));
                    rcount++;
                }
                catch { }
            }
            MessageBox.Show($"{rcount} most dissimilar alignments found.", "Results", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        public static void ConvertAll_Old(string folder, ConnectionViewModel cvm)
        {
            _globalCount = 0;
            _locker      = new Mutex();

            var numCores = Math.Min(Environment.ProcessorCount, 10);

            var dirInfo = new DirectoryInfo(folder);
            var alFiles = dirInfo.GetFiles("*.png", SearchOption.TopDirectoryOnly);

            _allCount = alFiles.Length;

            if (_allCount == 0)
            {
                MessageBox.Show("Nothing to convert. Aborting.");
                return;
            }

            var batchSize = (_allCount / numCores) + 1;

            var count = 0;
            var tasks = new List <Task>();

            for (var i = 0; i < numCores; i++)
            {
                var list = new List <string>();
                for (var x = 0; x < batchSize; x++)
                {
                    list.Add(alFiles[count].FullName);
                    count++;
                    if (count >= _allCount)
                    {
                        break;
                    }
                }

                if (!list.Any())
                {
                    break;
                }
                var tsk =
                    Task.Factory.StartNew(() =>
                {
                    ToJpg(list, cvm);
                    return;
                });
                tasks.Add(tsk);
            }

            while (tasks.Any(r => !r.IsCompleted))
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }
            cvm.Status = "Ready.";
        }
        public static void ConvertAll(string folder, ConnectionViewModel cvm)
        {
            _globalCount = 0;
            _locker      = new Mutex();

            var numCores = Math.Min(Environment.ProcessorCount, 10);

            var dirInfo = new DirectoryInfo(folder);
            var alFiles = dirInfo.GetFiles("*.png", SearchOption.TopDirectoryOnly);

            _allCount = alFiles.Length;

            if (_allCount == 0)
            {
                MessageBox.Show("Nothing to convert. Aborting.");
                return;
            }
            var tsk = Task.Factory.StartNew(() =>
            {
                Parallel.ForEach(alFiles, file =>
                {
                    var encoder           = System.Drawing.Imaging.Encoder.Quality;
                    var encoderParameters = new EncoderParameters(1)
                    {
                        Param = { [0] = new EncoderParameter(encoder, 85L) }
                    };

                    var jpgEncoder = GetEncoder(ImageFormat.Jpeg);
                    var extension  = System.IO.Path.GetExtension(file.FullName);
                    if (extension != ".png")
                    {
                        return;
                    }
                    var name = System.IO.Path.GetFileNameWithoutExtension(file.FullName);
                    var path = System.IO.Path.GetDirectoryName(file.FullName);
                    lock (_locker)
                    {
                        cvm.Status = $"Converting {++_globalCount}/{_allCount}";
                    }

                    var png = Image.FromFile(file.FullName);
                    png.Save($@"{path}\{name}.jpg", jpgEncoder, encoderParameters);
                    png.Dispose();
                    File.Delete(file.FullName);
                });
            });

            while (!tsk.IsCompleted)
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }
            cvm.Status = "Ready.";
        }
Exemple #6
0
        public FindIdentical(List <string> files)
        {
            InitializeComponent();
            cvm                 = new ConnectionViewModel();
            listIdentical       = new List <List <string> >();
            DataContext         = cvm;
            this.files          = files;
            Aborted             = false;
            current_image_index = 0;
            analysis_complete   = false;
            thrash              = FS_Helper.Properties.Resources.thrash;

            IdenticalReviewed = new List <string>();

            identical = new List <System.Windows.Controls.Image>()
            {
                DImage_0, DImage_1, DImage_2, DImage_3, DImage_4, DImage_5
            };
        }
Exemple #7
0
        public static void ArrangeImagePack(ConnectionViewModel cvm)
        {
            var alphabet = "abcdefghijklmnopqrstuvwxyz";

            var dir = string.Empty;

            using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                var result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    dir = dialog.SelectedPath;
                }
                else
                {
                    return;
                }
            }

            var directories = CustomSearcher.GetDirectories(dir);

            if (!directories.Any())
            {
                MessageBox.Show($"No subdirectories are found in {dir}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            var ind_1st_char = 0;
            var ind_2nd_char = 0;
            var ren_dict     = new Dictionary <string, Dictionary <string, string> >();
            var global_cnt   = 0;

            foreach (var pack in directories)
            {
                var d     = new DirectoryInfo(pack);
                var files = d.GetFiles("*.jp*", SearchOption.TopDirectoryOnly).ToList();
                if (!files.Any())
                {
                    continue;
                }
                var char1 = alphabet[ind_1st_char];
                var char2 = alphabet[ind_2nd_char];
                if (ind_2nd_char == alphabet.Count() - 1)
                {
                    ind_1st_char++;
                    ind_2nd_char = 0;
                }
                else
                {
                    ind_2nd_char++;
                }

                var cnt = 0;
                ren_dict.Add(pack, new Dictionary <string, string>());
                foreach (var file in files.OrderBy(r => r.FullName))
                {
                    var new_fn_name = $"{file.DirectoryName}\\{char1}{char2}{cnt.ToString("D5")}{file.Extension}";
                    cnt++; global_cnt++;
                    ren_dict[pack].Add(file.FullName, new_fn_name);
                }
            }
            var res = MessageBox.Show($"You are about to rename {global_cnt} files in {ren_dict.Count} folders. Confirm?", "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

            if (res != MessageBoxResult.Yes)
            {
                return;
            }

            var arranged_folder = $"{dir}\\Arranged";

            if (!Directory.Exists(arranged_folder))
            {
                Directory.CreateDirectory(arranged_folder);
            }
            var i   = 0;
            var tsk =
                Task.Factory.StartNew(() =>
            {
                try
                {
                    foreach (var pack in ren_dict)
                    {
                        foreach (var f in pack.Value)
                        {
                            File.Move(f.Key, f.Value);
                            File.Copy(f.Value, $"{arranged_folder}\\{Path.GetFileName(f.Value)}", true);
                            cvm.Status = $"Arranging file {++i}/{global_cnt}";
                        }
                    }
                }

                catch (Exception e)
                {
                    MessageBox.Show($"Problem renaming {e.Message}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                }
                finally
                {
                    MessageBox.Show($"Done!", "Results", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                return;
            });

            while (!tsk.IsCompleted)
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }

            cvm.Status = "Ready.";
        }
Exemple #8
0
        public static void ReCreateImagePack(ConnectionViewModel cvm)
        {
            var dirArranged  = string.Empty;
            var dirImagePack = string.Empty;

            using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                dialog.Description = "Select arranged images folder";
                var result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    dirArranged = dialog.SelectedPath;
                }
                else
                {
                    return;
                }
                dialog.Description = "Select image pack root folder";
                result             = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    dirImagePack = dialog.SelectedPath;
                }
                else
                {
                    return;
                }
            }
            var d             = new DirectoryInfo(dirArranged);
            var arrangedFiles = d.GetFiles("*.jp*", SearchOption.TopDirectoryOnly).ToList();

            if (!arrangedFiles.Any())
            {
                MessageBox.Show($"No images found in arranged folder {dirArranged}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            var directories = CustomSearcher.GetDirectories(dirImagePack);

            if (!directories.Any())
            {
                MessageBox.Show($"No subdirectories are found in {dirImagePack}", "Problem", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }
            var imagePackHashes = new Dictionary <string, Dictionary <string, List <string> > >();
            int i   = 0;
            var tsk =
                Task.Run(() =>
            {
                using (var md5 = MD5.Create())
                {
                    foreach (var pack in directories)
                    {
                        try
                        {
                            var dp    = new DirectoryInfo(pack);
                            var files = dp.GetFiles("*.jp*", SearchOption.TopDirectoryOnly).ToList();
                            if (!files.Any())
                            {
                                continue;
                            }
                            foreach (var file in files)
                            {
                                using (var stream = File.OpenRead(file.FullName))
                                {
                                    var hash = GetHash(md5, stream);
                                    if (!imagePackHashes.TryGetValue(hash, out Dictionary <string, List <string> > packFolders))
                                    {
                                        imagePackHashes.Add(hash, new Dictionary <string, List <string> >()
                                        {
                                            { dp.Name, new List <string>()
                                              {
                                                  file.FullName
                                              } }
                                        });
                                    }
                                    else
                                    {
                                        if (!packFolders.TryGetValue(dp.Name, out List <string> packFiles))
                                        {
                                            packFolders.Add(dp.Name, new List <string>()
                                            {
                                                file.FullName
                                            });
                                        }
                                        else
                                        {
                                            packFiles.Add(file.FullName);
                                        }
                                    }
                                }
                                cvm.Status = $"Getting MD5 checksum file {++i}";
                            }
                        }
                        catch (Exception e) { MessageBox.Show(e.Message); }
                    }

                    i = 0;
                    foreach (var file in arrangedFiles)
                    {
                        bool found = false;
                        Dictionary <string, List <string> > folderFile = null;
                        using (var stream = File.OpenRead(file.FullName))
                        {
                            var hash = GetHash(md5, stream);
                            if (imagePackHashes.TryGetValue(hash, out folderFile))
                            {
                                found = true;
                            }
                        }
                        if (found)
                        {
                            var fileMoved = false;
                            foreach (var pathName in folderFile.Keys)
                            {
                                var newPath = Path.Combine(dirArranged, pathName);
                                if (!Directory.Exists(newPath))
                                {
                                    Directory.CreateDirectory(newPath);
                                }
                                if (!fileMoved)
                                {
                                    File.Move(file.FullName, Path.Combine(newPath, Path.GetFileName(file.FullName)));
                                    fileMoved = true;
                                }
                            }
                        }
                        cvm.Status = $"Processing MD5 checksum of arranged file {++i}/{arrangedFiles.Count}";
                    }
                }
            });

            while (!tsk.IsCompleted)
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
            }

            cvm.Status = "Ready.";
        }
        private static List <string> FoundDissimilar(List <string> source, string path, ConnectionViewModel cvm)
        {
            var most_dis = new List <string>();

            most_dis.Add(source.First());
            for (int i = 1; i < source.Count; i++)
            {
                int difference = (int)(100.0 * ImageTool.GetPercentageDifference(System.IO.Path.Combine(path, source[i - 1]), System.IO.Path.Combine(path, source[i])));
                if (difference > 55)
                {
                    most_dis.Add(source[i]);
                }
                cvm.Status = $"Comparing {i}/{source.Count}, found {most_dis.Count}";
            }
            return(most_dis);
        }