使用Kmeans聚类压缩多个图像

时间:2018-10-02 14:36:51

标签: python k-means image-compression

我的错误:

let body = "Lorem Ipsum <strong>\(dateString)</strong> dolor sit amet. <br><br><p><strong>\(detailText).</strong></p><br>Lorem ipsum dolor sit amet.<br><br><p> <strong>\(descriptionText)</strong></p>"

我正在尝试使用K-means算法压缩文件夹中存在的多个图像。但是,我遇到了内存错误,因此我使用了MiniBatchKMeans,但又出现了IndexError。

  File "C:/Users/hero/PycharmProjects/project/CompressMe.py", line 14, in 

<module>
    image = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
IndexError: tuple index out of range

1 个答案:

答案 0 :(得分:0)

我对png格式的理解是,不同的png文件可以具有不同数量的通道(请参见https://en.wikipedia.org/wiki/Portable_Network_Graphics#Pixel_format)。如果您要处理灰度图像,io.imread将返回一个二维图像(请参阅文档:http://scikit-image.org/docs/dev/api/skimage.io.html#skimage.io.imread

可能尝试使用以下方法断言图像的尺寸始终为3:

image = io.imread(f)
if len(image.shape) == 2: # image is grayscale
    image = image[:,:,np.newaxis] # Expand to a third dimension
相关问题