python - How do I change each pixel -


i new python , still don't know qimage's pixel returns (it seems tupel of rgb or rgba -the lack of type declaration doesn't help) want grab each each pixel , change it.

newqim = qimage(imwidth, imheight, qimage.format_argb32) xstep in range(0, imwidth - 1):     ystep in range(0, imheight - 1):          pixelvaluetuple = im.getpixel((xstep, ystep))         pixelr = pixelvaluetuple[0]         pixelg = pixelvaluetuple[1]         pixelb = pixelvaluetuple[2]         copiedvalue = qrgb(pixelr, pixelg, pixelb)          newqim.setpixel(xstep, ystep, copiedvalue) 

above provided code ,i thought iterate on newqim, can't handle on how in python.

for xstep in range(0, imwidth-1):     ystep in range(0, imheight -1): 

i'm not sure understood want, since new python, here go few tips...

unpacking

this code:

pixelr = pixelr[0] pixelg = pixelvaluetuple[1] pixelb = pixelvaluetuple[2] 

is same as:

pixelr, pixelr, pixelr = pixelvaluetuple[:3] 

if sure len(pixelvaluetuple) == 3, just:

pixelr, pixelr, pixelr = pixelvaluetuple 

pep-8

a bit of nitpick, python guys tend little nazy syntax. please read pep-8. on i'm naming variables according (camelcase instance variables hurt eyes %-).

range

you want range(width) instead of range(0, width-1).

>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  >>> range(0, 10 - 1) [0, 1, 2, 3, 4, 5, 6, 7, 8] 

now problem.

width, height = 300, 300 im = qimage(width, height, qimage.format_argb32)  x in range(im.width()):     y in range(im.height()):         r, g, b, = qcolor(im.pixel(x ,y)).getrgb()         # ... r, g, b, ...         im.setpixel(x, y, qcolor(r, g, b, a).rgb()) 

example

width, height = 100, 100 im = qimage(width, height, qimage.format_argb32)  x in range(im.width()):     y in range(im.height()):         im.setpixel(x, y, qcolor(255, x * 2.56, y * 2.56, 255).rgb())  im.save('sample.png') 

result:

result


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -