标签:183 必知 77 Python image2vector 64 128 166 255
需求:将我本地任意张图片转换为向量形式
girl.jpg
import numpy as np
from PIL import Image
img = Image.open('girl.jpg').convert('RGBA')
arr = np.array(img)
# record the original shape
shape = arr.shape
# make a 1-dimensional view of arr
flat_arr = arr.ravel()
# convert it to a matrix
vector = np.matrix(flat_arr)
# do something to the vector
vector[:,::10] = 128
# reform a numpy array of the original shape
arr2 = np.asarray(vector).reshape(shape) # 把图片转换为向量形式
# make a PIL image
img2 = Image.fromarray(arr2, 'RGBA')
# img2 = Image.fromarray(arr2 * 3, 'RGBA')
# print(246 * 3 - 256 * 2) # 超过255会自动减去256
img2.show()
img2.save('out.png') # 保存图片
arr2
arr2为图片的向量形式
array([[[128, 182, 175, 255],
[167, 182, 175, 255],
[167, 182, 128, 255],
...,
[166, 183, 128, 255],
[166, 183, 177, 255],
[166, 183, 177, 255]],
[[128, 182, 175, 255],
[167, 182, 175, 255],
[167, 182, 128, 255],
...,
[166, 183, 128, 255],
[166, 183, 177, 255],
[166, 183, 177, 255]],
[[128, 181, 174, 255],
[166, 181, 176, 255],
[166, 181, 128, 255],
...,
[166, 183, 128, 255],
[166, 183, 177, 255],
[166, 183, 177, 255]],
...,
[[128, 73, 80, 255],
[ 96, 75, 82, 255],
[ 98, 77, 128, 255],
...,
[ 67, 77, 128, 255],
[ 64, 77, 86, 255],
[ 64, 77, 86, 255]],
[[128, 59, 66, 255],
[ 83, 62, 69, 255],
[ 85, 64, 128, 255],
...,
[ 66, 76, 128, 255],
[ 64, 77, 86, 255],
[ 64, 77, 86, 255]],
[[128, 53, 60, 255],
[ 76, 55, 62, 255],
[ 78, 57, 128, 255],
...,
[ 65, 75, 128, 255],
[ 64, 77, 86, 255],
[ 64, 77, 86, 255]]], dtype=uint8)
out.jpg
标签:183,必知,77,Python,image2vector,64,128,166,255 来源: https://blog.csdn.net/qq_43827595/article/details/110341326
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。