1

I am new to Python and I still don't know what exactly Qimage's pixel returns (it seems to a tupel of rgb or rgba -the lack of type declaration doesn't help) I want to grab each each pixel and change it.

newqim = QImage(imWidth, imHeight, QImage.Format_ARGB32)
for xstep in range(0, imWidth - 1):
    for 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 is the provided code ,I thought I then iterate over that newqim, but I can't get a handle on how exactly I would do that in Python.

for xstep in range(0, imWidth-1):
    for ystep in range(0, imHeight -1):
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • from http://stackoverflow.com/q/18406149/566035, people convert it to either numpy array or opencv mat. But opencv python binding (cv2) plays very well with numpy now so most people would use just numpy. – otterb Apr 24 '15 at 22:28

1 Answers1

0

I'm not sure I understood what you want, but since you are new to Python, here go a few tips...

Unpacking

This code:

pixelR = pixelR[0]
pixelG = pixelValueTuple[1]
pixelB = pixelValueTuple[2]

Is the same as:

pixelR, pixelG, pixelB = pixelValueTuple[:3]

If you are sure len(pixelValueTuple) == 3, then it is just:

pixelR, pixelG, pixelB = pixelValueTuple

PEP-8

A bit of nitpick, but python guys tend to be a little nazy about syntax. Please read PEP-8. From now on I'm naming variables according to it (camelCase for instance variables just hurt my eyes %-).

Range

You probably 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 back to your problem.

width, height = 300, 300
im = QImage(width, height, QImage.Format_ARGB32)

for x in range(im.width()):
    for y in range(im.height()):
        r, g, b, a = QColor(im.pixel(x ,y)).getRgb()
        # ... do something to r, g, b, a ...
        im.setPixel(x, y, QColor(r, g, b, a).rgb())

Example

width, height = 100, 100
im = QImage(width, height, QImage.Format_ARGB32)

for x in range(im.width()):
    for 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

melMass
  • 3,813
  • 1
  • 31
  • 30
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • Thank you, as I said I have no clue about Python syntax/semantics , so I am not sure what the range(0, width-1) is doing. I assumed it was the starting point for the iteration like a Java "for loop" I got that code stub as a starting point, I have to write image manipulation functions like multiply, add, gamma, etc. – recycledElectrons Apr 24 '15 at 23:42
  • You probably want something like `im = QImage(); im.load('sample.png')` then. BTW, the QColor class has a lot of useful methods, check the docs. – Paulo Scardine Apr 25 '15 at 00:04
  • Thank you Paulo you've been very helpful .the image is indeed loaded further up in the code, just need to better understand how to manipulate each single one of them – recycledElectrons Apr 25 '15 at 01:09
  • You are welcome. Just to be clear, do you consider your question answered? If so, you can signal this by clicking the green checkmark on the left - and if not, how can I improve my answer in order to get it accepted? – Paulo Scardine Apr 25 '15 at 01:28