-
Notifications
You must be signed in to change notification settings - Fork 10
Enhance image compression handling and error logging #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c6b9a05
c12d659
4cbed4a
76b7e16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||||||||||||
| using SixLabors.ImageSharp.Processing; | ||||||||||||||||||
| using SwarmUI.Media; | ||||||||||||||||||
| using SwarmUI.Utils; | ||||||||||||||||||
| using SwarmUI.Media; | ||||||||||||||||||
| using Image = SwarmUI.Utils.Image; | ||||||||||||||||||
|
|
@@ -67,10 +68,15 @@ public static string CompressImageForVision(MediaContent media, string targetFor | |||||||||||||||||
| } | ||||||||||||||||||
| try | ||||||||||||||||||
| { | ||||||||||||||||||
| ImageFile image = ImageFile.FromDataString($"data:{media.MediaType};base64,{media.Data}"); | ||||||||||||||||||
| // Skip compression for videos etc.. | ||||||||||||||||||
| if (image.Type.MetaType != MediaMetaType.Image) | ||||||||||||||||||
| Image image = Image.FromDataString($"data:{media.MediaType};base64,{media.Data}") as SwarmUI.Utils.Image; | ||||||||||||||||||
| if (image == null) | ||||||||||||||||||
| { | ||||||||||||||||||
| Logs.Error("Failed to parse image data."); | ||||||||||||||||||
| return media.Data; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (image.Type.MetaType != MediaMetaType.Image && image.Type.MetaType != MediaMetaType.Animation) | ||||||||||||||||||
| { | ||||||||||||||||||
| Logs.Error("Media type is not supported for compression."); | ||||||||||||||||||
| return media.Data; | ||||||||||||||||||
| } | ||||||||||||||||||
| ISImage img = image.ToIS; | ||||||||||||||||||
|
|
@@ -82,13 +88,18 @@ public static string CompressImageForVision(MediaContent media, string targetFor | |||||||||||||||||
| int newHeight = (int)(img.Height * scaleFactor); | ||||||||||||||||||
| img.Mutate(i => i.Resize(newWidth, newHeight)); | ||||||||||||||||||
| } | ||||||||||||||||||
| // Set compression quality based on format TODO: This needs to be tested and adjusted | ||||||||||||||||||
| int quality = targetFormat == "PNG" ? 60 : 40; | ||||||||||||||||||
| ImageFile tempImage = new Image(ImageFile.ISImgToPngBytes(img), image.Type); | ||||||||||||||||||
| ImageFile compressedImage = tempImage.ConvertTo(targetFormat, quality: quality); | ||||||||||||||||||
| // Return just the base64 data (without the data:image/webp;base64, prefix) | ||||||||||||||||||
| var imageFile = new Image(img) as SwarmUI.Media.ImageFile; | ||||||||||||||||||
| if (imageFile == null) | ||||||||||||||||||
| { | ||||||||||||||||||
| Logs.Error("Failed to convert image to ImageFile."); | ||||||||||||||||||
| return media.Data; | ||||||||||||||||||
| } | ||||||||||||||||||
| var compressedImage = imageFile.ConvertTo(targetFormat, quality: quality); | ||||||||||||||||||
|
Comment on lines
+92
to
+98
|
||||||||||||||||||
| var imageFile = new Image(img) as SwarmUI.Media.ImageFile; | |
| if (imageFile == null) | |
| { | |
| Logs.Error("Failed to convert image to ImageFile."); | |
| return media.Data; | |
| } | |
| var compressedImage = imageFile.ConvertTo(targetFormat, quality: quality); | |
| var compressedImage = image.ConvertTo(targetFormat, quality: quality); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The redundant cast
as SwarmUI.Utils.Imageis unnecessary sinceImage.FromDataString()already returns aSwarmUI.Utils.Imagetype (as defined by the using alias on line 4). This cast will always succeed or return null, making the null check on line 71 potentially misleading about the actual failure mode.