根据 (x,y) 位置绘制点
Draw dots based on (x,y) locations
所以我得到了大约 200 行的 .txt 文件,每行包含一个从 0 到 255 的 (x,y) 位置。
我想在每个位置绘制点或“,”。所以 (0,5) 将在第一行绘制 5 个空格,然后是“,”。
使用python,有没有办法以这种方式将其打印到终端?如果没有,有没有办法用“图像”或任何其他方式创建 .txt 文件来查看生成的“图像”(它只是一堆“,”)
谢谢。
编辑:
.txt 文件是我从挑战中提取的内容。挑战是解码给定二进制文件的输出。这是原始文件:
https://elbitcareer.hunterhrms.com/wp-content/uploads/elbitsystems.elbit
这是我设法从文件中提取的 .txt 坐标:
https://easyupload.io/imtbdn
这是我将它打印到终端时的样子:
看起来方向正确(SYSCO..?)但有些地方不对劲..
有什么问题吗?
EDIT2:所以我的 .txt 文件缺少一些点,我的终端 window 需要调整大小.. 现在它有点工作了!谢谢大家
这里有一个使用 ANSI escape codes 的小函数。
def print_at(x, y, txt):
"""
Print txt on a specific coordinate of the terminal screen.
"""
print(f"3[{y};{x}H{txt}")
我写了一些代码,输出似乎和你的一样,但多了一点。所以问题一定出在控制台尺寸上。我使用了 out.txt 文件和这段代码:
import numpy as np
xmax = 0
ymax = 0
with open('out.txt', 'r') as f:
while f.readline() !='':
line = f.readline()
if line and "," in line:
x, y = line.split(',')
x = int(x)
y = int(y)
if x > xmax:
xmax = x
if y > ymax:
ymax = y
f.close()
pic = np.zeros((xmax+1,ymax+1))
with open('out.txt', 'r') as f:
while f.readline() !='':
line = f.readline()
if line and "," in line:
x, y = line.split(',')
x = int(x)
y = int(y)
pic[x][y] = 1
print(xmax,ymax)
with open('picture.txt', 'w') as f:
for y in range(ymax):
for x in range(xmax):
if pic[x][y] == 1:
f.write('.')
else:
f.write(' ')
f.write('\n')
此代码分 4 个步骤运行:
- 正在寻找图片的最大值。
- 创建用此大小的零填充的矩阵。
- 根据out.txt
的x,y坐标用1填充矩阵
- 遍历矩阵并写入 picture.txt 点 '.'如果有 1 和空格如果有 0.
输出文件是picture.txt
看起来像这样:
尝试更改终端控制台的大小,这会很好:)
更新答案
现在您的文件格式为:
colourIndex, x, y
你可以像这样制作 PNG 图片:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Unpack CSV into three Numpy arrays
colour, x, y = np.loadtxt('out.txt', dtype=np.int, delimiter=',', unpack=True)
# Find height and width of "image", i.e. maximum y and x
h = np.max(y)
w = np.max(x)
print(f'Width: {w}, height: {h}')
# Make empty (black) 3-channel RGB image right size
im = np.zeros((h,w,3), dtype=np.uint8)
# Choose some colours for the colour indices
colmap = {0:[255,0,255], # 0 -> magenta
1:[128,128,128]} # 1 -> grey
# Fill in points in colour
for col,thisx,thisy in zip(colour,x,y):
im[thisy-1, thisx-1] = colmap[col]
# Make into "PIL Image" and save
Image.fromarray(im).save('result.png')
原答案
正如你的问题提到的“或其他方式”,我想我会根据你的数据制作一个 PNG 图像:
#!/usr/bin/env python3
import re
import numpy as np
from PIL import Image
# Open file with rather pesky parentheses, carriage returns and linefeeds
with open('out.txt', 'r') as f:
c = f.read()
# Find all things that look like numbers and make into Numnpy array of 2 columns
points = np.array(re.findall('\d+',c), dtype=np.int).reshape(-1,2)
# Find height and width of "image", i.e. maximum x and y
w, h = np.max(points, axis=0)
print(f'Width: {w}, height: {h}')
# Make empty (black) image right size
im = np.zeros((h,w), dtype=np.uint8)
# Fill in white points
im[points[...,1]-1,points[...,0]-1] = 255
# Make into "PIL Image" and save
Image.fromarray(im).save('result.png')
请注意,由于所有不必要的括号和行尾的 Windows-y CR+LF(回车 Return 和换行符),代码的长度是所需的两倍) 在你的输入文件中。
请注意,我垂直缩放了图像,因为终端中的字符通常比宽度高 - 与像素为正方形的位图图像不同。
所以我得到了大约 200 行的 .txt 文件,每行包含一个从 0 到 255 的 (x,y) 位置。 我想在每个位置绘制点或“,”。所以 (0,5) 将在第一行绘制 5 个空格,然后是“,”。
使用python,有没有办法以这种方式将其打印到终端?如果没有,有没有办法用“图像”或任何其他方式创建 .txt 文件来查看生成的“图像”(它只是一堆“,”) 谢谢。
编辑:
.txt 文件是我从挑战中提取的内容。挑战是解码给定二进制文件的输出。这是原始文件: https://elbitcareer.hunterhrms.com/wp-content/uploads/elbitsystems.elbit
这是我设法从文件中提取的 .txt 坐标: https://easyupload.io/imtbdn
这是我将它打印到终端时的样子:
看起来方向正确(SYSCO..?)但有些地方不对劲.. 有什么问题吗?
EDIT2:所以我的 .txt 文件缺少一些点,我的终端 window 需要调整大小.. 现在它有点工作了!谢谢大家
这里有一个使用 ANSI escape codes 的小函数。
def print_at(x, y, txt):
"""
Print txt on a specific coordinate of the terminal screen.
"""
print(f"3[{y};{x}H{txt}")
我写了一些代码,输出似乎和你的一样,但多了一点。所以问题一定出在控制台尺寸上。我使用了 out.txt 文件和这段代码:
import numpy as np
xmax = 0
ymax = 0
with open('out.txt', 'r') as f:
while f.readline() !='':
line = f.readline()
if line and "," in line:
x, y = line.split(',')
x = int(x)
y = int(y)
if x > xmax:
xmax = x
if y > ymax:
ymax = y
f.close()
pic = np.zeros((xmax+1,ymax+1))
with open('out.txt', 'r') as f:
while f.readline() !='':
line = f.readline()
if line and "," in line:
x, y = line.split(',')
x = int(x)
y = int(y)
pic[x][y] = 1
print(xmax,ymax)
with open('picture.txt', 'w') as f:
for y in range(ymax):
for x in range(xmax):
if pic[x][y] == 1:
f.write('.')
else:
f.write(' ')
f.write('\n')
此代码分 4 个步骤运行:
- 正在寻找图片的最大值。
- 创建用此大小的零填充的矩阵。
- 根据out.txt 的x,y坐标用1填充矩阵
- 遍历矩阵并写入 picture.txt 点 '.'如果有 1 和空格如果有 0.
输出文件是picture.txt
看起来像这样:
尝试更改终端控制台的大小,这会很好:)
更新答案
现在您的文件格式为:
colourIndex, x, y
你可以像这样制作 PNG 图片:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Unpack CSV into three Numpy arrays
colour, x, y = np.loadtxt('out.txt', dtype=np.int, delimiter=',', unpack=True)
# Find height and width of "image", i.e. maximum y and x
h = np.max(y)
w = np.max(x)
print(f'Width: {w}, height: {h}')
# Make empty (black) 3-channel RGB image right size
im = np.zeros((h,w,3), dtype=np.uint8)
# Choose some colours for the colour indices
colmap = {0:[255,0,255], # 0 -> magenta
1:[128,128,128]} # 1 -> grey
# Fill in points in colour
for col,thisx,thisy in zip(colour,x,y):
im[thisy-1, thisx-1] = colmap[col]
# Make into "PIL Image" and save
Image.fromarray(im).save('result.png')
原答案
正如你的问题提到的“或其他方式”,我想我会根据你的数据制作一个 PNG 图像:
#!/usr/bin/env python3
import re
import numpy as np
from PIL import Image
# Open file with rather pesky parentheses, carriage returns and linefeeds
with open('out.txt', 'r') as f:
c = f.read()
# Find all things that look like numbers and make into Numnpy array of 2 columns
points = np.array(re.findall('\d+',c), dtype=np.int).reshape(-1,2)
# Find height and width of "image", i.e. maximum x and y
w, h = np.max(points, axis=0)
print(f'Width: {w}, height: {h}')
# Make empty (black) image right size
im = np.zeros((h,w), dtype=np.uint8)
# Fill in white points
im[points[...,1]-1,points[...,0]-1] = 255
# Make into "PIL Image" and save
Image.fromarray(im).save('result.png')
请注意,由于所有不必要的括号和行尾的 Windows-y CR+LF(回车 Return 和换行符),代码的长度是所需的两倍) 在你的输入文件中。
请注意,我垂直缩放了图像,因为终端中的字符通常比宽度高 - 与像素为正方形的位图图像不同。