python-3.x - 如何使用python创建具有每个点的RGB值的NumPy图像数组?
问题描述
我正在尝试将图像分成两部分并将这两个图像合并回来。我尝试使用 NumPy 从图像上的每个点减去一个随机值并存储该值以使其成为新图像。然后我收到“照片无法打开此文件,因为当前不支持格式化,或者文件已损坏。” 在第二张图片上。那么如何创建具有这些值的图像呢?
import numpy
import random
from PIL import Image
a = Image.new('RGB', [1280, 720], (255, 255, 255))
a = numpy.array(a)
value = []
for x in a:
for y in range(0, len(x)):
temp = [random.randrange(0, 255), random.randrange(
0, 255), random.randrange(0, 255)]
b = numpy.array(temp)
x[y] = x[y]-b
value.append(temp)
a = Image.fromarray(a)
a.show()
#print(value)
#print(len(value)) # 1280*720=921600
c = numpy.array(value)
d = Image.fromarray(c)
d.show()
解决方案
我想到了。我试图创建一个相同大小的新白色图像并循环通过它来添加值。
import numpy
import random
from PIL import Image
a = Image.open('test.jpg')
a = numpy.array(a)
value = []
for x in a:
for y in range(0, len(x)):
temp = [random.randrange(0,255),random.randrange(0,255),random.randrange(0,255)]
b = numpy.array(temp)
x[y] = x[y]-b
value.append(temp)
a = Image.fromarray(a)
b=Image.new('RGB',a.size,(255,255,255))
b=numpy.array(b)
counter=0
for x in b:
for y in range(0,len(x)):
x[y]=tuple(value[counter])
counter+=1
b=Image.fromarray(b)
a=numpy.array(a)
b=numpy.array(b)
a=numpy.add(a,b)
a=Image.fromarray(a)
a.show()
推荐阅读
- angular - 嵌入在 Angular 6 组件中的 iframe 中 Rocketchat 用户的自动登录
- excel - 删除 TEXTJOIN 和 IF 函数中的重复值
- c++ - 在两个 lambda 之间共享对象
- html - 如何在 Angular 7 上有一些延迟显示点击表单?
- ruby-on-rails - ActionController::UnknownFormat 在 CommentsController#index
- multithreading - GUI中的tIdHttp内部线程和IdTCPServer
- sql - 如何使用由日期时间值组成的 PIVOT 表
- javascript - 如何将值从 javascript 传递到控制器
- c# - 在运行时重定向汇编方法
- php - 从一个 SQL 数据库中提取并插入外部 SQL 数据库