计算机视觉必知 - 用Python将任一图片转换为向量矩阵 image2vector
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了计算机视觉必知 - 用Python将任一图片转换为向量矩阵 image2vector,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1504字,纯文字阅读大概需要3分钟。
内容图文
![计算机视觉必知 - 用Python将任一图片转换为向量矩阵 image2vector](/upload/InfoBanner/zyjiaocheng/620/c1c065067940480583140808fd63e5ec.jpg)
需求:将我本地任意张图片转换为向量形式
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
内容总结
以上是互联网集市为您收集整理的计算机视觉必知 - 用Python将任一图片转换为向量矩阵 image2vector全部内容,希望文章能够帮你解决计算机视觉必知 - 用Python将任一图片转换为向量矩阵 image2vector所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。