[英]Using python how do I convert an RGB array to an encoded image suitable for transmission via HTTP?
[英]How do I convert a CSV file to an RGB image using python
我正在嘗試將csv文件轉換為RGB文件,如下所示:
from PIL import Image, ImageDraw
from numpy import genfromtxt
g = open('myFile.csv','r')
temp = genfromtxt(g, delimiter = ',')
im = Image.fromarray(temp).convert('RGB')
pix = im.load()
rows, cols = im.size
for x in range(cols):
for y in range(rows):
print str(x) + " " + str(y)
pix[x,y] = (int(temp[x,y] // 256 // 256 % 256),int(temp[x,y] // 256 % 256),int(temp[x,y] % 256))
im.save(g.name[0:-4] + '.jpeg')
問題是倒數第二行給我一個“ IndexError:圖像索引超出范圍”。 當我打印出x和y時,我發現只要它們中的任何一個達到5(即文件的寬度),但高度都不會發生,無論我如何使用它,無論x或y達到5,都會引發錯誤那就是我沒有弄清楚的。 也歡迎任何更好的實現。
如果有人想要有效的算法,請按以下步驟進行:
from PIL import Image, ImageDraw
from numpy import genfromtxt
g = open('myFile.csv','r')
temp = genfromtxt(g, delimiter = ',')
im = Image.fromarray(temp).convert('RGB')
pix = im.load()
rows, cols = im.size
for x in range(cols):
for y in range(rows):
print str(x) + " " + str(y)
pix[x,y] = (int(temp[y,x] // 256 // 256 % 256),int(temp[y,x] // 256 % 256),int(temp[y,x] % 256))
im.save(g.name[0:-4] + '.jpeg')
只是用temp [y,x]更改temp [x,y]並交換列和行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.