values = traindf.rdd.map(lambda r: (r.id, r.image)).sortByKey().flatMap(lambda id_image: id_image[1]).collect()
ncol = len(traindf.rdd.map(lambda r: r.image).first())
nrow = traindf.count()
dm = DenseMatrix(nrow, ncol, values)
Apache Spark: How to create a matrix from a DataFrame?