Skip to content

Instantly share code, notes, and snippets.

@codePrincess
Created March 27, 2018 14:01
Show Gist options
  • Save codePrincess/4362913b534da785aeb74ed8dd0f3d78 to your computer and use it in GitHub Desktop.
Save codePrincess/4362913b534da785aeb74ed8dd0f3d78 to your computer and use it in GitHub Desktop.
func generateMultiArrayFrom(image: UIImage) -> MLMultiArray? {
guard let data = try? MLMultiArray(shape: [8,8], dataType: .double) else {
return nil
}
let hTileWidth = image.size.width / 8
let vTileWidth = image.size.height / 8
var xPos : CGFloat = 0
var yPos : CGFloat = 0
for rowIndex in 0...7 {
for colIndex in 0...7 {
//cut the image part at the certain coordinates
let imageRect = CGRect(x: xPos, y: yPos, width: hTileWidth, height: vTileWidth)
let cutImage = image.crop(rect: imageRect)
let avgColor = cutImage.areaAverage()
var grayscale: CGFloat = 0
var alpha: CGFloat = 0
avgColor.getWhite(&grayscale, alpha: &alpha)
xPos += hTileWidth
let alphaAsNumber = NSNumber(integerLiteral: Int(alpha * 16.0))
data[rowIndex*8 + colIndex] = alphaAsNumber
}
xPos = 0
yPos += vTileWidth
}
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment