User:Eleanorg/1.2/Forbidden Pixels/storing RGB values in nested lists: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "A better method of associating the co-ordinate & colour of each pixel, as suggested by Timo. Stores rgb values and pixel co-ordinates in nested lists, thus: <br /> pixels[ [row1c...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
Stores rgb values and pixel co-ordinates in nested lists, thus: <br />
Stores rgb values and pixel co-ordinates in nested lists, thus: <br />
pixels[ [row1column1, row1column2, row1column3....] [row2column1, row2column2, row2column3...] [row3column1...] ]
pixels[ [row1column1, row1column2, row1column3....] [row2column1, row2column2, row2column3...] [row3column1...] ]
^^ each sub-list corresponds to a row in the image grid; each key within the sub-list to the column number. The value is the pixel's RGB (colour) data. ^^


<source lang="python">
<source lang="python">
Line 9: Line 10:
import os, re
import os, re


# stores rgb values and pixel co-ordinates in nested lists, thus:
# Converts an image into nested lists of RGB values.
# stores rgb values and pixel co-ordinates thus, where the value of each list item is the RGB info; its key the column number, and its list the row number:
# pixels[ [row1column1, row1column2, row1column3....] [row2column1, row2column2, row2column3...] [row3column1...] ]
# pixels[ [row1column1, row1column2, row1column3....] [row2column1, row2column2, row2column3...] [row3column1...] ]


Line 20: Line 22:
columns = int(width)
columns = int(width)
rows = int(height)
rows = int(height)
print columns
#print columns
print
#print rows
print rows


#--------- create hash with each pixel's position & colour ------------------#
#--------- assign pixel details to lists  ------------------#


pixels = [] # create empty list 'pixels'
pixels = [] # create empty list 'pixels'
Line 34: Line 35:
             colorResult = os.popen(colorQuery, 'r').read() # makes & captures output of system call
             colorResult = os.popen(colorQuery, 'r').read() # makes & captures output of system call
             color = re.sub("\n", "", colorResult) # strips newline character from end of sys call result
             color = re.sub("\n", "", colorResult) # strips newline character from end of sys call result
             pixels[row].append(color) # append color as a value to the 'row' sublist within 'pixels' - its key will be the same as the current column number
             pixels[row].append(color) # append color as a value to the 'row' sublist within 'pixels' - its is same as current column number
   
   
getcolors()
getcolors()


# test that colours & coordinates are assigned correctly by printing entire list:
# test that colours & coordinates are assigned correctly:
for key, value in sorted(pixels.keys()):
print pixels [0][0]
    print(key, value)
 
# or:
# print pixels
 
</source>
</source>

Latest revision as of 11:39, 20 February 2012

A better method of associating the co-ordinate & colour of each pixel, as suggested by Timo. Stores rgb values and pixel co-ordinates in nested lists, thus:
pixels[ [row1column1, row1column2, row1column3....] [row2column1, row2column2, row2column3...] [row3column1...] ] ^^ each sub-list corresponds to a row in the image grid; each key within the sub-list to the column number. The value is the pixel's RGB (colour) data. ^^

#!/usr/bin/python
#-*- coding:utf-8 -*-

import os, re

# Converts an image into nested lists of RGB values.
# stores rgb values and pixel co-ordinates thus, where the value of each list item is the RGB info; its key the column number, and its list the row number:
# pixels[ [row1column1, row1column2, row1column3....] [row2column1, row2column2, row2column3...] [row3column1...] ]

#--------- determine image dimensions----------------------------------------#	

image = "20by10PixelsSection1.png"
sizeQuery = "identify -format '%w %h' " + image		# system command gets image dimensions with ImageMagick
size = os.popen(sizeQuery, 'r').read()			# captures output of system call
(width, height) = size.split()				# splits ImageMagick output into separate column & row no's
columns = int(width)
rows = int(height)
#print columns
#print rows

#--------- assign pixel details to lists  ------------------#		

pixels = []							# create empty list 'pixels'
def getcolors ():
    for row in range (0, rows):					# for each row in the image...		
    	pixels.append([])					# ...append a new sub-list within list 'pixels'
        for column in range (0, columns):			# for each column in the row...
            colorQuery = "convert " + image + " -format '%[pixel:p{" + str(column) + "," + str(row) + "}]' info:-"
            colorResult = os.popen(colorQuery, 'r').read()	# makes & captures output of system call
            color = re.sub("\n", "", colorResult)		# strips newline character from end of sys call result
            pixels[row].append(color)			# append color as a value to the 'row' sublist within 'pixels' - its is same as current column number
 
getcolors()

# test that colours & coordinates are assigned correctly:
print pixels [0][0]

# or:
# print pixels