【问题标题】:How to convert RGB values from .txt file to display an image in Python如何将 .txt 文件中的 RGB 值转换为在 Python 中显示图像
【发布时间】:2021-05-27 01:36:46
【问题描述】:

我有一个包含 RGB 值的 .txt 文件,当我打开并读取文件时,像素值是 str 格式。如何转换这些值以在 python 中显示图像。 image.

这是我尝试读取值时的。它们都是字符串格式。

编辑:你可以在这里找到文件的链接https://drive.google.com/file/d/1mAxlcMj_SVeK0axJhbPJqO4k_egJoYli/view?usp=sharing

【问题讨论】:

  • 请包含您的实际数据以供人们使用,而不是数据图片。谢谢你。 xkcd.com/2116 如果 StackOverflow 太大,可以使用 Dropbox 或 Google Drive。
  • 欢迎来到 Stack Overflow!您似乎在要求某人为您编写一些代码。 Stack Overflow 是一个问答网站,而不是代码编写服务。请see here学习如何写出有效的问题。
  • 你的一些RGB值没有用逗号分隔,你能进一步解释一下吗?每个像素都需要三个值,因此是 RGB。

标签: python opencv computer-vision rgb face-detection


【解决方案1】:

这很简单:

#!/usr/bin/env python3

import re
import numpy as np
from PIL import Image
from pathlib import Path

# Open image file, slurp the lot
contents = Path('image.txt').read_text()

# Make a list of anything that looks like numbers using a regex...
# ... taking first as height, second as width and remainder as pixels
h, w, *pixels = re.findall(r'[0-9]+', contents)

# Now make pixels into Numpy array of uint8 and reshape to correct height, width and depth
na = np.array(pixels, dtype=np.uint8).reshape((int(h),int(w),3))

# Now make the Numpy array into a PIL Image and save
Image.fromarray(na).save("result.png")


如果您想使用 OpenCV 而不是 PIL/Pillow 编写输出图像,请将上面的最后一行更改为以下内容,以便进行 RGB->BGR 重新排序并改用cv2.imwrite()

# Save with OpenCV instead
cv2.imwrite('result.png', na[...,::-1])

如果你想写一个 PPM 文件(兼容 PhotoshopGIMPOpenCVPIL/PillowImageMagick),没有使用 PIL/PillowOpenCV 或任何额外的库,并且大约 1 /4 原始文件的大小,您可以通过将上面的最后一行替换为以下内容非常简单地以二进制形式编写它:

# Save "na" as binary PPM image
with open('result.ppm','wb') as f:
   f.write(f'P6\n{w} {h}\n255\n'.encode())
   f.write(na.tobytes())

其实你不需要任何Python,直接在Terminal的命令行下写一个Photoshop可以读取的NetPBM文件就可以了, GIMPPIL/枕头

awk 'NR==1{$0="P3\n" $2 " " $1 "\n255"} {gsub(/,/,"\n")} 1' image.txt > result.ppm

那个脚本基本上是“massages”你的第一行,所以它是从这个开始的:

418 870
... rest of your data ...

到这里:

P3
870 418
255
... rest of your data ...

【讨论】:

  • 哇...非常感谢马克
  • 很高兴它对你有用。祝你的项目好运。如果您遇到困难再回来问另一个问题 - 它们是免费的,答案也是如此;-)
【解决方案2】:

有必要确定两个步骤来解决这个问题:

1.解析文件以获取像素以及图像的宽度和高度。在这一步中,您需要知道信息是如何 保存在文件中。

2。使用 OpenCV 显示图像。下面是一个将矩阵显示为图像的基本示例:

import numpy as np
import cv2
# image of 100 x 100 pixels , with 3 channels 
height=100 
width=100
channels=3 
color_bg=(0,0,0) 
imgdim = (height, width, channels ) 
blank_image = np.full(imgdim, color_bg, np.uint8)
#Simple way to change the pixel(x=1,y=2) color to the (B=255,G=0,R=0) tuple color 
blank_image[1,2]= (255,0,0) 

cv2.imshow("blank", blank_image)
cv2.waitKey(0)

请阅读Basics operations with opencv-python 的文档以了解有关 numpy 的概念和性能建议。

【讨论】:

  • 这很好解释。但是,当我尝试从 txt 文件中读取 t 值时,它们都是字符串,这意味着我必须将所有数字的类型从 str 更改为 int 类型。这太没有生产力了。我想知道是否有更好的方法来做到这一点。谢谢你。另外,我的图像是 418, 870
【解决方案3】:

读取txt文件需要这段代码

file= open("filename.txt")
lines = file.readlines()

然后你可以用函数获取每一行的数字

lines[0].split(",")

终于可以用枕头制作image with the data

    from PIL import Image
     
    img = Image.new('RGB', (60, 30), color = 'red')
    img.save('pil_red.png')

【讨论】:

  • 您仍然需要自己清理数据并理解每一行
猜你喜欢
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多