使用 PPM 文件在 python 中将图像转换为 ASCII。不允许使用 PIL

发布于 2024-12-04 01:27:09 字数 329 浏览 2 评论 0原文

项目需要一些帮助/指导。我们的任务是接收一个 .ppm 文件(我们需要测试的文件可以在这里找到:http://beastie.cs.ua.edu/cs250/projects/asciiart/tux.ppm)并使用 ascii 字符在屏幕上重新打印出来。我们需要将像素转换为灰度。这确实是我被困住的地方。无法弄清楚如何读取每三个元素(因为 PPM 文件中每三个元素都是一个像素),将它们转换为灰度并继续。再次强调 PIL 是不允许的。任何有关阅读内容的帮助或链接都很棒!

Needing a little help/direction with a project. Our task is to take in a .ppm file (the one we are required to test with is found here: http://beastie.cs.ua.edu/cs250/projects/asciiart/tux.ppm) and reprint it out on the screen using ascii characters. We are required to convert the pixels to greyscale. This is really where I am stuck. Cannot figure out how to read in every three elements (because every three is a pixel in PPM files), convert them to greyscale and move on. Again PIL is not allowed. Any help or links on what to read up on would be awesome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-12-11 01:27:09

PPM 并不难解析。

标题:

P3
50 50
255
  • P3 表示图像是 ASCII 像素图(颜色)。
  • 50 50 是宽度和高度。
  • 255 是最大颜色值。

正文:

254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 
254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 
254 254 252 254 254 252 254 254 252 253 255 250 239 244 237 251 255 248 
234 236 231 255 255 251 252 251 249 255 254 251 253 248 242 255 255 244 
...

只需删除所有换行符:

body.replace('\n', ' ')

并将其解析为三元组(不太优雅):

raw = body.split(' ')

for i in range(0, len(raw), 3):
  red = raw[i]
  green = raw[i + 1]
  blue = raw[i + 2]

The PPM isn't hard to parse.

The header:

P3
50 50
255
  • P3 means that the image is an ASCII pixmap (color).
  • 50 50 is the width and height.
  • 255 is the max color value.

The body:

254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 
254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 254 254 252 
254 254 252 254 254 252 254 254 252 253 255 250 239 244 237 251 255 248 
234 236 231 255 255 251 252 251 249 255 254 251 253 248 242 255 255 244 
...

Just remove all newlines:

body.replace('\n', ' ')

And parse it in triplets (not too elegant):

raw = body.split(' ')

for i in range(0, len(raw), 3):
  red = raw[i]
  green = raw[i + 1]
  blue = raw[i + 2]
落花随流水 2024-12-11 01:27:09

读取 ppm 文件,您可以执行以下操作:

# Open the PPM file and process the 3 first lines
f = open("tux.ppm")
color = f.readline().splitlines()
size_x, size_y = f.readline().split()
max = f.readline().splitlines()

您实际上不需要了解文件的前 3 行。您只需知道您正在处理 RGB 图像,这意味着每个像素有 3 个值 (0-255)。

要将图像转换为灰度,您有两种选择:您可以生成另一个 PPM 文件(每个像素有 3 个值),也可以生成一个与 PPM 格式相同但第一行是 P2< 的 PGM 文件。 /code> 而不是 P3 并且每个像素只有一个值(这是一种很酷的方式)。

要将 RGB 颜色值 (r,g,b) 转换为一个灰度强度值,您可以应用以下公式(比简单应用平均值更好):

0.21*r + 0.71*g + 0.07*b

生成每个像素一个值的灰度图像(如果您想要 3 个值)对于 r、g、b),您只需重复 3 次:

# Getting the image data (if you have read the 3 first lines...)
data = f.read().split()

# Generate a new array of pixels with grayscale values (1 per pixel)
gray_data = [0.21*data[i] + 0.71*data[i+1] + 0.07*data[i+2] for i in range(0,len(data),3)]

Reading the ppm file you can do this:

# Open the PPM file and process the 3 first lines
f = open("tux.ppm")
color = f.readline().splitlines()
size_x, size_y = f.readline().split()
max = f.readline().splitlines()

You really don't need to know about the 3 first lines of the file. You only must know that you are working with a RGB image, which means you have 3 values (0-255) for each pixel.

To convert the image to grayscale you have two options: you can either generate another PPM file (with 3 values per pixel) or you can generate a PGM file which has the same format as the PPM but the first line is P2 instead of P3 and you will have only one value per pixel (that's the cool way).

To convert a RGB color value (r,g,b) in one grayscale intensity value you can apply this formula (better than simply apply the average):

0.21*r + 0.71*g + 0.07*b

Generate the grayscale image with one value per pixel (if you want it in 3-values you only have to repeat 3 times it for r,g,b):

# Getting the image data (if you have read the 3 first lines...)
data = f.read().split()

# Generate a new array of pixels with grayscale values (1 per pixel)
gray_data = [0.21*data[i] + 0.71*data[i+1] + 0.07*data[i+2] for i in range(0,len(data),3)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文