码迷,mamicode.com
首页 > 其他好文 > 详细

图片压缩

时间:2018-11-29 15:36:47      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:wan   class   dom   sts   import   fit   center   roi   sse   

直接上代码


import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import sklearn.cluster as skcluster


img_path = input(‘Enter the image path you want to compress: ‘)
if not os.path.exists(img_path):
   print(‘Invalid path‘)
   sys.exit(1)

A = mpimg.imread(img_path)
A = A.astype(float) / 255
img_shape = np.shape(A)
X = np.reshape(A, (img_shape[0] * img_shape[1], 3))
clf = skcluster.KMeans(n_clusters=10, random_state=0).fit(X)
print(‘Finish training!...‘)
idx, centroids = clf.labels_, clf.cluster_centers_
X_compressed = centroids[idx, :]
A_compressed = np.reshape(X_compressed, (img_shape[0], img_shape[1], img_shape[2]))
plt.imshow(A_compressed)
plt.show()

图片压缩

标签:wan   class   dom   sts   import   fit   center   roi   sse   

原文地址:https://www.cnblogs.com/megachen/p/10037353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!