【问题标题】:Save pixel data in txt format in PIL将像素数据以txt格式保存在PIL中
【发布时间】:2019-10-22 09:06:57
【问题描述】:

我的程序是从图像中提取像素并将像素数据保存在文本文件中以供分析。我的图片是只给出 255 和 0 的二值图像

程序如下:

from PIL import Image

im = Image.open("thresh.jpg")
pixel = im.load()
row, column = im.size
for y in range(column)
    for x in range(row)
        pixel = pix[x, y]

问题:

我想将pixel 数据保存在一个文本文件中。建议我一些技巧来保存数据。

【问题讨论】:

    标签: python python-imaging-library getpixel


    【解决方案1】:

    只需创建一个文件写入器对象并将变量像素的值写入该对象即可。

    from PIL import Image
    
    im = Image.open("thresh.jpg")
    fil = open('file', 'w')
    pixel = im.load()
    row, column = im.size
    for y in range(column):
        for x in range(row):
            pixel = pix[x, y]
            fil.write(str(pixel) + '\n')
    fil.close()
    

    【讨论】:

    • 感谢您的回复,但我收到了type error: expected a character buffer object
    • 我在file.write(pixel)这一行收到了类似type error: expected a character buffer object的错误
    • 现在我必须保存“像素”及其位置 x 和 y 例如:x=100 y=100 像素 = 255....如何使用file.write保存>
    • 我知道了fil.write(str(x)+str(y)+str(pixel)+'\n')
    • 是的,x 和 y 是整数。你不能像 int + str 那样做。它不会进行串联。为此,您需要输入转换 x 和 y。
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2016-05-18
    • 2021-09-25
    • 2015-07-31
    • 2012-03-25
    • 1970-01-01
    相关资源
    最近更新 更多