首页 > 其他 > 详细

图片压缩

时间:2018-11-29 13:14:53      阅读:121      评论:0      收藏:0      [点我收藏+]

直接上代码


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()

图片压缩

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!