Thursday, 15 August 2013

Using a Python dictionary to edit image -



Using a Python dictionary to edit image -

first beginner of programming may have stupid questions ;)

i created dictionary this:

cell = {(1,2):200,(1,2):200,(1,2):200,(1,2):200,...}

and used image create new image. thought utilize cell.keys() illustration (1,2) access pixels coordinates of image , alter value value of key. how that? tried "unpacking", no success other "invalid syntax"

cell = {} in range (255): k in range (255): cell.update({(i,k):random.randrange(1,150,2)}) img = image.new('rgb',(255,255), 'white') pixels = img.load() # create pixel map k in cell.keys(): pixels[*k] = (*k, cell[k]) # here invalid syntax @ '*k'

just utilize k key:

pixels[k] = k + (cell[k],)

you have rgb image, need tuple 3 values. k tuple two values, guessing want (i, j, random_value) tuple pixel value.

*k works in function signatures, calls, , (in python 3) tuple assignments. has no special meaning in indexing or creating tuple literal.

note using dict.update() one-element dictionary is.. inefficient. utilize item assignment instead:

cell = {} in range(255): k in range(255): cell[(i,k)] = random.randrange(1,150,2)

a dictionary not best info storage format dense matrix however; improve utilize nested list:

cell = [[random.randrange(1,150,2) _ in range(255)] _ in range(255)]

and address with:

for i, row in enumerate(cell): j, value in enumerate(row): pixels[i, j] = (i, j, value)

python image dictionary

No comments:

Post a Comment