【问题标题】:colors are wrong numpy array for pillow image when I use txt当我使用 txt 时,枕头图像的颜色是错误的 numpy 数组
【发布时间】: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


    【解决方案1】:

    看起来问题在于图像数组作为字符串数组而不是整数数组保存在文本文件中。当您读取文本文件并将其转换回数组时,这些值将被解释为字符串,从而导致显示图像时出现错误的颜色。

    此问题的一种解决方案是将文本文件中的数组保存为整数数组而不是字符串数组。您可以使用 numpy.savetxt() 函数执行此操作,该函数将数组以指定格式保存到文本文件中。下面是一个示例,说明如何修改代码以将数组保存为文本文件中的整数:

    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)
        # Save the array as integers in the text file
        numpy.savetxt('img_array.txt', image, fmt='%d')
    
    def show_imagem(array):
        # Create a NumPy array from the text file
        arry = numpy.loadtxt('img_array.txt', dtype=int)
        
        # 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)
    

    在此代码中,img_to_txt_array() 函数使用 numpy.savetxt() 函数将图像数组作为整数保存在文本文件中。 show_imagem() 函数使用 numpy.loadtxt() 函数从文本文件中读取数组,然后创建并显示数组中的图像。这应该可以解决图像中颜色错误的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多