将像素数据以txt格式保存在PIL中 [英] Save pixel data in txt format in PIL

查看:69
本文介绍了将像素数据以txt格式保存在PIL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是从图像中提取像素并将像素数据保存在文本文件中进行分析.我的图片是一个二进制图像,只给出 255 和 0

My program is to extract the pixel from an image and to save the pixel data in the text file for analysis. My picture is a binary image that gives only 255 and 0 's

这是程序:

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 数据保存在一个文本文件中.建议我一些保存数据的技巧.

I want to save the pixel data in a text file. Suggest me some techniques to save the data.

推荐答案

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

Just create a file writer object and write the value of the variable pixel to that.

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()

这篇关于将像素数据以txt格式保存在PIL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆