0

I'm a hobby programer and I had an idea for pixelating images and I wrote it. I was wondering how would a better programer write the same function.

I want to learn and I think that this would help me a lot to see how code is supposedto be written, also my code is quite slow and i would like to make it swifter.

I use PIL for image manipulation, pygame for window and numpy for math. after this code i return img that is in PIL format and pass it to pygame with fromstring.

This code divides the image into blocks defined by pixSize and then thakes RGB value from all the pixels in that block and finds median value with witch then that block is colored and then it moves on to next block and and does the same.

Here is my code

def pixelateImage(self, img, pixSize):
        print(img.size[1])
        
        n = 0
        ny = 0
        blockInfo = []
        for i in range(int(img.size[0]/pixSize)*int(img.size[1]/pixSize)):
            colVal = (randrange(0,256), randrange(0,256), randrange(0,256))
            if n+1 > int(img.size[0]/pixSize):
                n = 0
                ny += 1
            xs = range(pixSize*n, pixSize*(n+1))
            ys = range(pixSize*ny, pixSize*(ny+1))
            RGB = []
           
            for x in xs:
                for y in ys:
                   
                    cModeRGB = []
                    rgb = img.getpixel((x,y))
                    for i in rgb:
                        if i == 0:
                            cModeRGB.append(0)
                            continue
                        cMode = i / 255
                        cModeRGB.append(cMode)
                    
                    RGB.append(cModeRGB)
            RGBc = np.array(RGB)
           
            RGB = []
            for i in np.median(RGBc, axis=0):
                RGB.append(int(i * 255))

            rgbMid = (RGB[0], RGB[1], RGB[2])    
            
            for x in xs:
                for y in ys:
                    img.putpixel((x,y), rgbMid)
            n += 1
        return img
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 4
    this question would probably fit better over on https://codereview.stackexchange.com/ – Jeanot Zubler Oct 07 '22 at 12:51
  • One idea here... https://stackoverflow.com/a/51547855/2836621 – Mark Setchell Oct 07 '22 at 12:59
  • 1
    @JeanotZubler I didn't even know codereview exists, I'm new here.. – FrankSintara Oct 07 '22 at 13:03
  • @MarkSetchell That is a way for sure but it used PIL functions and I would like to see how my code can be optimized – FrankSintara Oct 07 '22 at 13:05
  • You're not going to like this either then https://scikit-image.org/docs/stable/api/skimage.filters.html#skimage.filters.median – Mark Setchell Oct 07 '22 at 13:13
  • @MarkSetchell Yeah :D, your solutions are great if I realy needed that function for something, but I just had an idea how to do it and I did it for practice, dont really have a use for it, just want to see how can it be done better so I can learn. But thanks! – FrankSintara Oct 07 '22 at 13:17
  • There is also the `generic_filter()` in the second part of this answer that lets you do custom processing on *"windows"* into your image https://stackoverflow.com/a/64330973/2836621 I guess you would change `np.std()` to a median calculation. – Mark Setchell Oct 07 '22 at 13:30
  • You could use fast image libraries like `skimage` or `cv2` or to make the function faster you could use `numba`, which complies a function, or `OpenGL` to calculate each pixel on the gpu. Using `numpy` also would make sense. – Jerry Oct 07 '22 at 19:40

0 Answers0