【发布时间】:2022-12-05 02:47:05
【问题描述】:
首先,我将图像转换为 numpy 数组并将其写入文本文件,这部分工作正常
问题是当我复制 txt 内容并将其动态粘贴为矢量并显示图像时。颜色显示错误。
enter image description here enter image description here
`
import cv2
import sys
import numpy
from PIL import Image
numpy.set_printoptions(threshold=sys.maxsize)
def img_to_txt_array(img):
image = cv2.imread(img)
# print(image)
f = open("img_array.txt", "w")
f.write(str(image))
f.close()
meuArquivo = open('img_array.txt', 'r')
with open('img_array.txt', 'r') as fd:
txt = fd.read()
txt = txt.replace(" ", ",")
txt = txt.replace('\n',',\n')
txt = txt.replace("[,", "[")
txt = txt.replace(',[', '[')
txt = txt.replace(",,", ",")
txt = txt.replace(',[', '[')
txt = txt.replace("[,", "[")
txt = txt.replace(",,", ",")
with open('img_array.txt', 'w') as fd:
fd.write(txt)
with open('img_array.txt', 'r') as fr:
lines = fr.readlines()
with open('img_array.txt', 'w') as fw:
for line in lines:
if line.strip('\n') != ',':
fw.write(line)
def show_imagem(array):
# Create a NumPy array
arry = numpy.array(array)
# Create a PIL image from the NumPy array
image = Image.fromarray(arry.astype('uint8'), 'RGB')
# Save the image
#image.save('image.jpg')
# Show the image
image.show(image)
array = [] #paste here the txt
img_to_txt_array('mickey.png')
show_imagem(array)
`
我需要正确选择颜色
【问题讨论】:
标签: python python-3.x numpy python-imaging-library