我有 txt 中的資料。我應該如何將資料轉換為灰度影像輸出?謝謝!行數為 2378,列數為 5362。

我是python的菜鳥。我試過這個,但沒有用。
from numpy import *
from PIL import Image
def rdnumpy(txtname):
f = open(txtname)
line = f.readlines()
lines = len(line)
for l in line:
le = l.strip('\n').split(' ')
columns = len(le)
A = zeros((lines, columns), dtype=int)
A_row = 0
for lin in line:
list = lin.strip('\n').split(' ')
A[A_row:] = list[0:columns]
A_row = 1
return A
A = rdnumpy('oop.txt')
im = Image.fromarray(array)
im = im.convert('L')
im.save('T7.png')
uj5u.com熱心網友回復:
您的代碼稍作改動(參見# <<<代碼):
from numpy import *
from PIL import Image
def rdnumpy(txtname):
f = open(txtname)
line = f.readlines()
lines = len(line)
for l in line:
le = l.strip('\n').split(' ')
columns = len(le)
A = zeros((lines, columns), dtype=uint8) # <<< uint8
A_row = 0
for lin in line:
list = lin.strip('\n').split(' ')
A[A_row:] = list[0:columns]
A_row = 1
return A
A = rdnumpy('PIL_imgFromText.txt')
im = Image.fromarray(A) # <<< A
im = im.convert('L')
im.save('PIL_imgFromText.png')
在“PIL_imgFromText.txt”的情況下創建
100 128 156
200 225 255
以下灰度影像(放大):

PS 下面建議如何進一步改進函式的代碼:
import numpy as np
def rdnumpy_improved(txtname):
lst = []
with open(txtname) as f:
lines = f.readlines()
imgSizeY = len(lines)
imgSizeX = len(lines[0].strip('\n').split(' '))
for line in lines:
lst_c = line.strip('\n').split(' ')
assert imgSizeX == len(lst_c)
lst = lst_c
A = np.array(lst, dtype=uint8).reshape((imgSizeY, imgSizeX))
return A
最后是如何將整個代碼縮短為使用函式的單行numpy.loadtxt()代碼(如另一個答案中所建議的)
import numpy as np
from PIL import Image
Image.fromarray(np.loadtxt('PIL_imgFromText.txt', dtype=np.uint8)).save('PIL_imgFromText.png')
uj5u.com熱心網友回復:
請幫助我,我需要將代碼從 JavaScript 更改為 Python,這是代碼
if(parsedData.handler == HANDLER_ROOM_ADMIN){
if(parsedData.type == "occupants_list"){
this.tempRoom = parsedData.room;
let msg = "";
while (this.user_list.length > 0) {
this.user_list.pop();
}
for(let i = 0; i < parsedData.occupants.length; i ){
this.user_list.push(parsedData.occupants[i].username);
var suffix = "";
if(i === parsedData.occupants.length-1){}
else{
suffix = "\n";
}
msg = (i 1) " " parsedData.occupants[i].username suffix;
}
if(msg.length > 0){
await send_group_msg(parsedData.room, msg);
}
}
}
}
uj5u.com熱心網友回復:
嘗試使用 cv2 !
cvtColor() 函式可以將創輝影像轉換為灰度影像。
第一步是將文本檔案轉換為影像檔案(嘗試使用 .jpg)。嘗試顯示您的影像以查看它是否有效,這必須是彩色圖片。
完成后,使用 cv2 將彩色影像轉換為灰色影像,然后再次顯示以查看結果。
您可以使用 cv2.imshow('windows_name', image) 來顯示影像。
import cv2
img = cv2.imread(im)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Scale Image', img)
您的影像“img”現在是灰色的。如果您未能將文本檔案轉換為影像,請告訴我。
希望它有所幫助:)
uj5u.com熱心網友回復:
首先,制作一個示例文本檔案,因為沒有提供問題。
import numpy as np
# Make a 10x8 pixel image of increasing numbers
im = np.arange(80).reshape((8,10))
# Save in same format as OP in file called "data.txt"
np.savetxt('data.txt', im, fmt="%d")
看起來像這樣:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
現在來回答這個問題。您可以使用 Numpy 非常簡單有效地加載影像,然后制作成影像并像這樣保存:
from PIL import Image
# Load image from text file, make into "PIL Image", save
im = np.loadtxt('data.txt', dtype=np.uint8)
pi = Image.fromarray(im)
pi.save('result.png')
結果如下:

或者,如果你喜歡單行:
Image.fromarray(np.loadtxt('data.txt', dtype=np.uint8)).save('result.png')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523535.html
標籤:Python图片灰度
下一篇:python影像統一變換
