Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert images into pixel dataframe into csv file using python
#1
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

Output:
pixel1 pixel. pixel158 pixel159 pixel160 pixel161 pixel162 pixel163 pixel164 pixel165 pixel166 pixel167 pixel168 pixel169 pixel170 pixel171 pixel172 1 0 … 0 191 250 253 93 0 0 0 0 0 0 0 0 0 0 2 0 … 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 pixel173 pixel174 pixel175 pixel176 1 0 0 0 0 2 0 0 16 179
how to do it correct?
buran wrote Feb-14-2019, 02:41 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Quote
#2
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.
Quote
#3
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python QtableWidget get text of all cells and headers to dataframe Mady 3 227 Dec-15-2018, 06:46 PM
Last Post: Axel_Erfurt
  Insert images in a folder into dataframe tofi 0 146 Dec-14-2018, 08:05 PM
Last Post: tofi
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 335 Oct-18-2018, 09:33 PM
Last Post: volcano63
  Assign image names in folder to images in other folder. ujjwalrathod007 15 792 Sep-03-2018, 05:27 PM
Last Post: snippsat
  Python read Password protected excel and convert to Pandas DataFrame FORTITUDE 2 1,219 Aug-30-2018, 01:08 PM
Last Post: FORTITUDE
  Trying to import JSON data into Python/Pandas DataFrame then edit then write CSV Rhubear 0 562 Jul-23-2018, 09:50 PM
Last Post: Rhubear
  How to create mask for different types of images using opencv Prince_Bhatia 0 1,481 Apr-26-2018, 07:38 AM
Last Post: Prince_Bhatia
  Button Press When Pixel Color Changes bak989 0 379 Apr-22-2018, 04:03 PM
Last Post: bak989
  How to modify video using pixel array or shader? jhovarie 0 449 Mar-27-2018, 11:42 AM
Last Post: jhovarie
  Change color pixel in an image louloudevinci 2 1,090 Mar-25-2018, 08:27 PM
Last Post: louloudevinci

Forum Jump:


Users browsing this thread: 1 Guest(s)