Skip to content

Commit a819b46

Browse files
authored
Handle Indexed Pixel Format correctly in Image Resize transform. (#3601)
* Handle Indexed Pixel Format correctly in Image Resize transform. * PR feedback. * PR feedback. * PR feedback. * PR feedback.
1 parent c587f82 commit a819b46

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

src/Microsoft.ML.ImageAnalytics/ImageResizer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Drawing;
8+
using System.Drawing.Imaging;
89
using System.Linq;
910
using System.Text;
1011
using Microsoft.ML;
@@ -381,7 +382,13 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
381382
destHeight = info.ImageHeight;
382383
}
383384

384-
dst = new Bitmap(info.ImageWidth, info.ImageHeight, src.PixelFormat);
385+
// Graphics.DrawImage() does not support PixelFormat.Indexed. Hence convert the
386+
// pixel format to Format32bppArgb as described here https://stackoverflow.com/questions/17313285/graphics-on-indexed-image
387+
if ((src.PixelFormat & PixelFormat.Indexed) != 0)
388+
dst = new Bitmap(info.ImageWidth, info.ImageHeight);
389+
else
390+
dst = new Bitmap(info.ImageWidth, info.ImageHeight, src.PixelFormat);
391+
385392
var srcRectangle = new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
386393
var destRectangle = new Rectangle(destX, destY, destWidth, destHeight);
387394
using (var g = Graphics.FromImage(dst))

test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ public void TensorFlowTransformCifar()
845845
var imageHeight = type.Dimensions[0];
846846
var imageWidth = type.Dimensions[1];
847847

848-
var dataFile = GetDataPath("images/images.tsv");
848+
var dataFile = GetDataPath("images/imagesmixedpixelformat.tsv");
849849
var imageFolder = Path.GetDirectoryName(dataFile);
850850
var data = mlContext.Data.LoadFromTextFile(dataFile,
851851
columns: new[]
@@ -874,7 +874,7 @@ public void TensorFlowTransformCifar()
874874
Assert.Equal(10, buffer.Length);
875875
numRows += 1;
876876
}
877-
Assert.Equal(4, numRows);
877+
Assert.Equal(5, numRows);
878878
}
879879
}
880880

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tomato.bmp tomato
2+
banana.jpg banana
3+
hotdog.jpg hotdog
4+
tomato.jpg tomato
5+
tomato_indexedpixelformat.gif tomato
108 KB
Loading

0 commit comments

Comments
 (0)