Skip to content

Commit c06aa7d

Browse files
committed
Adds individual image crop validation
Images with invalid crop parameters are now ignored and the crop still occurs for the other, rather than canceling all cropping.
1 parent 68e3d72 commit c06aa7d

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ Dataset Crop was built using [Avalonia UI](https://avaloniaui.net/), so it is cr
5454
5. Use the preview pane to adjust crop areas on each image. They can be positioned with the mouse.
5555
6. Checking the **Selection mode** option and clicking on the crop area of images will toggle their "selected" state. This allows you to exclude images from the crop process.
5656
Excluded images will turn their crop area preview's background to a semi-transparent red.
57-
7. For the **Output Path** field, browse and select a directory where the cropped images will be saved, or manually paste the path in the text box.
58-
8. Execute the batch crop process by clicking the **Crop** button.
59-
9. The cropped images should be ready to be used in the training process. The input images are not modified in any way.
57+
7. When defining invalid parameters (for example, when setting crop width to 100 pixels, but the image width is just 84 pixels), the preview panes will be disabled and their background set to a semi-transparent red. These images cannot be selected, and they will not be cropped.
58+
8. For the **Output Path** field, browse and select a directory where the cropped images will be saved, or manually paste the path in the text box.
59+
9. Execute the batch crop process by clicking the **Crop** button.
60+
10. The cropped images should be ready to be used in the training process. The input images are not modified in any way.
6061

6162
## Contributing
6263

src/DatasetCrop/MainWindow.axaml.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ await Task.Run(async () =>
187187
});
188188

189189
var loadedImages = await Task.WhenAll(loadTasks);
190-
await Dispatcher.UIThread.InvokeAsync(async () =>
190+
await Dispatcher.UIThread.InvokeAsync(() =>
191191
{
192192
foreach (var loadedImage in loadedImages)
193193
{
@@ -199,15 +199,7 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
199199
container.HorizontalAlignment = HorizontalAlignment.Left;
200200
container.VerticalAlignment = VerticalAlignment.Top;
201201
container.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(0, 0, 0), 0.1);
202-
// for each image, ensure the drag panel is not in any way bigger than the visible area of the scaled down image preview
203-
if (!ValidateCropPanelSize(loadedImage.OriginalSize.Width, loadedImage.OriginalSize.Height, cropWidth, cropHeight, previewWidth, previewHeight))
204-
{
205-
await MessageBoxManager.GetMessageBoxStandardWindow("Error!", "Specified crop size exceeds the bounds of the scaled image!" + Environment.NewLine + loadedImage.FilePath, ButtonEnum.Ok, MessageBox.Avalonia.Enums.Icon.Error).ShowDialog(this);
206-
ClearImagePreviews();
207-
return;
208-
}
209202
Avalonia.Controls.Image image = new();
210-
//image.Source = bitmap;
211203
image.Width = previewWidth;
212204
image.Height = previewHeight;
213205
image.Source = loadedImage.Image;
@@ -234,14 +226,24 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
234226
dragPanel.Height = cropHeight;
235227
dragPanel.Margin = new Thickness(cropX, cropY, 0, 0);
236228
}
229+
// for each image, ensure the drag panel is not in any way bigger than the visible area of the scaled down image preview
230+
if (!ValidateCropPanelSize(loadedImage.OriginalSize.Width, loadedImage.OriginalSize.Height, cropWidth, cropHeight, previewWidth, previewHeight))
231+
{
232+
dragPanel.Tag = false; // do not select the image, if its crop parameters are not ok
233+
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, 0, 0), 0.3);
234+
dragPanel.IsEnabled = false; // don't allow dragging, when the image is not valid for cropping
235+
}
236+
else
237+
{
238+
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, 255, 255), 0.3);
239+
dragPanel.Tag = true; // true = "selected" (will be cropped), false = "deselected" (will be ignored when cropping)
240+
}
237241
dragPanel.HorizontalAlignment = HorizontalAlignment.Left;
238242
dragPanel.VerticalAlignment = VerticalAlignment.Top;
239-
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, 255, 255), 0.3);
240243
dragPanel.Cursor = new Cursor(StandardCursorType.SizeAll);
241244
dragPanel.PointerMoved += DragPanel_PointerMoved; // subscribe the event handlers used for dragging
242245
dragPanel.PointerPressed += DragPanel_PointerPressed;
243246
dragPanel.PointerReleased += DragPanel_PointerReleased;
244-
dragPanel.Tag = true; // true = "selected" (will be cropped), false = "deselected" (will be ignored when cropping)
245247

246248
container.Children.Add(dragPanel);
247249
grdImages.Children.Add(container);
@@ -391,6 +393,7 @@ private static async Task<byte[]> CropImageAsync(SixLabors.ImageSharp.Image imag
391393
return outputMemoryStream.ToArray();
392394
}
393395

396+
#region Validation
394397
/// <summary>
395398
/// Validates the required information for setting the input path
396399
/// </summary>
@@ -647,6 +650,7 @@ private async Task<bool> ValidateImageFiles()
647650
else
648651
return true;
649652
}
653+
#endregion
650654

651655
/// <summary>
652656
/// Notifies subscribers about a property's value being changed
@@ -674,14 +678,14 @@ private void DragPanel_PointerPressed(object? sender, PointerPressedEventArgs e)
674678
{
675679
if (IsSelectionMode)
676680
{
677-
if (sender is Panel dragPanel) // when Control is pressed, toggle the "is selected" state
681+
if (sender is Panel dragPanel)
678682
{
679683
bool isSelected = (bool)dragPanel.Tag!;
680684
dragPanel.Tag = !isSelected;
681685
dragPanel.Background = new SolidColorBrush(Avalonia.Media.Color.FromRgb(255, (byte)(!isSelected ? 255 : 0), (byte)(!isSelected ? 255 : 0)), 0.3);
682686
}
683687
}
684-
else // Control is not pressed, just enable dragging
688+
else // not in selection mode, just enable dragging
685689
isDragStarted = true;
686690
}
687691

0 commit comments

Comments
 (0)