convert images into pixel dataframe into csv file using python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: convert images into pixel dataframe into csv file using python (/thread-16108.html) |
convert images into pixel dataframe into csv file using python - synthex - Feb-14-2019 i want convert images to pixels i do so from PIL import Image im = Image.open('image.jpg') pixels = list(im.getdata()) result = [] counter = 0 for pixel in pixels: counter += 1 result.append(['pixel'+ str(counter), pixel[1]]) return (result) and get the result ['pixel1', 72], ['pixel2', 50], ['pixel3', 0], ['pixel4', 11], ['pixel5', 30], ['pixel6', 42], ['pixel7', 107], ['pixel8', 123] here one picture but there are many pictures, i want covert it all to pixels the path so i want take all picture from here im = Image.open('C:\Users\Admin\Downloads\mypicture)output in csv file? each row it is one picture how to do it correct?
RE: convert images into pixel dataframe into csv file using python - scidam - Feb-15-2019 Note, If you image is not a gray-scale one, each pixel will likely be presented as a triple (r, g, b), where r, g, b are integer values from 0 to 255 (or floats in [0,1]); So, you would be needed to store that triple for every pixel. Given a PIL-image img , you can convert it to the numpy array:import numpy as np img_converted = np.array(img) # Now you can flatten you array row = img_converted.ravel() # and convert to list row_as_list = row.tolist()Finally, I am not clear with underlying problem of this. What are you trying to do? But I am sure, that you don't need to use pixel-wise loops in pure Python, they are very slow.Typical workflow with images in Python assumes using scikit-image, numpy and scipy packages, which computationally expensive parts are implemented in C/Cython. This allows to perform image manipulations reasonably faster in comparison with pure Python implementation of image processing algorithms. RE: convert images into pixel dataframe into csv file using python - synthex - Feb-15-2019 scidam, hello, i decided do it in another way from PIL import Image import glob,os paths = glob.glob(os.path.join(os.environ['userprofile'],'Downloads','mypicture','*.jpg')) im = list(map(Image.open, paths)) for obj in im: pixels = list(obj.getdata())and i get pixels so my question. this path were pictures ('C:\Users\Admin\Downloads\mypicture), i want attache only two picture in forum https://imgur.com/a/LiRdmvU https://imgur.com/a/KWtqSdr how these pixelsimport in csv ? output picname pix1 pix2 pix3 img_248.jpg 0 0 0 img_350.jpg 0 0 0 RE: convert images into pixel dataframe into csv file using python - scidam - Feb-17-2019 So, you have a matrix filled with either 0 or 1. If I understood you correctly, you need to save it to a file. You can do it using numpy: import numpy as np sample_matrix = np.random.randint(0, 2, size=(10,10)).tolist() np.savetxt('output.csv', sample_matrix, delimiter=',')Additinally, you can pass header keyword to .savetxt to set column names.
|