Save pixel data in txt format in PIL

2024/10/8 2:25:08

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

Here is the program:

from PIL import Imageim = 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]

Question:

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

Answer

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

from PIL import Imageim = 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()
https://en.xdnf.cn/q/118748.html

Related Q&A

ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value

Ive been given the python script where matplotlib is used , when running the script it opens the window and display graph. its working perfectly on my laptop. But this error occurs when I upload the fi…

Python win32com Outlook Stores not iterable

Trying to list all Outllook stores (and finally all e-mails in those stores):import win32com.clientoutlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") sto…

Using a users input to search dictionary keys and print that keys matching value

I am working with tkinter, as I have my gui set up with and entry, label, and button. Im trying to search my dictionarys keys with the users input from the entry, and print the value of the key that wa…

How to write CSV into the next column

I have output that I can write into a CSV. However, because of how i setup my XML to text, the output iterates itself incorrectly. Ive tried a lot to fix my XML output, but I dont see any way to fix it…

Comparing date from pandas dataframe to current date

Im currently trying to write a script that does a specific action on a certain day. So for example, if today is the 6/30/2019 and in my dataframe there is a 6/30/2019 entry, xyz proceeds to happen. How…

How to divide a binary file to 6-byte blocks in C++ or Python with fast speed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Selenium, python dynamic table

Im creating a robot with selenium that get all info from agencies in Brasil, ive alredy done the permutation click between all States and counties, all i have to do nows click in all agencies and get i…

How to split string into column

I got a csv file with some data, and I want to split this data.My column one contains a title, my column 2 contains some dates, and my column 3 contains some text linked to the dates.I want to tran…

remove whitespaces with new line characters

I have a string that looks like that:"\n My name is John\n and I like to go.\n blahblahblah.\n \n\n ".Note - In this string example, there are 5 white-spaces after…

Python tkinter GUI freezing/crashing

from Tkinter import * import tkFileDialog import tkMessageBox import os import ttkimport serial import timeit import time################################################################################…