Skip to content

Commit

Permalink
Handle Indexed Pixel Format correctly in Image Resize transform. (#3601)
Browse files Browse the repository at this point in the history
* Handle Indexed Pixel Format correctly in Image Resize transform.

* PR feedback.

* PR feedback.

* PR feedback.

* PR feedback.
  • Loading branch information
codemzs authored Apr 26, 2019
1 parent c587f82 commit a819b46
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Microsoft.ML.ImageAnalytics/ImageResizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using Microsoft.ML;
Expand Down Expand Up @@ -381,7 +382,13 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
destHeight = info.ImageHeight;
}
dst = new Bitmap(info.ImageWidth, info.ImageHeight, src.PixelFormat);
// Graphics.DrawImage() does not support PixelFormat.Indexed. Hence convert the
// pixel format to Format32bppArgb as described here https://stackoverflow.com/questions/17313285/graphics-on-indexed-image
if ((src.PixelFormat & PixelFormat.Indexed) != 0)
dst = new Bitmap(info.ImageWidth, info.ImageHeight);
else
dst = new Bitmap(info.ImageWidth, info.ImageHeight, src.PixelFormat);
var srcRectangle = new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
var destRectangle = new Rectangle(destX, destY, destWidth, destHeight);
using (var g = Graphics.FromImage(dst))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ public void TensorFlowTransformCifar()
var imageHeight = type.Dimensions[0];
var imageWidth = type.Dimensions[1];

var dataFile = GetDataPath("images/images.tsv");
var dataFile = GetDataPath("images/imagesmixedpixelformat.tsv");
var imageFolder = Path.GetDirectoryName(dataFile);
var data = mlContext.Data.LoadFromTextFile(dataFile,
columns: new[]
Expand Down Expand Up @@ -874,7 +874,7 @@ public void TensorFlowTransformCifar()
Assert.Equal(10, buffer.Length);
numRows += 1;
}
Assert.Equal(4, numRows);
Assert.Equal(5, numRows);
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/data/images/imagesmixedpixelformat.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tomato.bmp tomato
banana.jpg banana
hotdog.jpg hotdog
tomato.jpg tomato
tomato_indexedpixelformat.gif tomato
Binary file added test/data/images/tomato_indexedpixelformat.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a819b46

Please sign in to comment.