Android Question How to pixelate images

Roycefer

Well-Known Member
Licensed User
Longtime User
The simplest way is to open the image using LoadBitmapSample and use a really small resolution and then blow it back up to the original resolution. It will be heavily pixelated.

If you want to do it computationally, divide your picture up into a grid where each grid is the size of the new "pixel" you will create. Then go through each grid and calculate the average red value, green value, and blue value of all the pixels within the grid. Then create a new color based on these average values and set all the corresponding pixels in your output image to that color. This is the most naive method of pixelation.

There are more sophisticated methods like Gaussian blur that you can look into.

Here's a library that does some interesting stuff: https://www.b4x.com/android/forum/threads/rsimageprocessing-library.16230/#content
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
The simplest way is to open the image using LoadBitmapSample and use a really small resolution and then blow it back up to the original resolution. It will be heavily pixelated.

If you want to do it computationally, divide your picture up into a grid where each grid is the size of the new "pixel" you will create. Then go through each grid and calculate the average red value, green value, and blue value of all the pixels within the grid. Then create a new color based on these average values and set all the corresponding pixels in your output image to that color. This is the most naive method of pixelation.

There are more sophisticated methods like Gaussian blur that you can look into.

Here's a library that does some interesting stuff: https://www.b4x.com/android/forum/threads/rsimageprocessing-library.16230/#content
Thanks, will check the library out.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Also, canvas can do the trick by drawing to a smaller bitmap and then drawing the result to another with the same size as the original

Before:D Screenshot_2015-10-30-09-35-52_1.jpg After:eek: Screenshot_2015-10-30-09-35-59_1.jpg


B4X:
Sub Pixelate(B0 As Bitmap) as Bitmap

  Dim factor As Float=8    'Change it to get different pixellations

  Dim new_w As Int = B0.Width/factor 
  Dim new_h As Int = B0.Height/factor

  If new_w>=1 And new_h>=1 Then
    
    'Draw the original onto a smaller one
    Dim B_small As Bitmap
    B_small.InitializeMutable(new_w,new_h)
    Dim cv As Canvas
    cv.Initialize2(B_small)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,B_small.Width,B_small.Height)
    cv.DrawBitmap(B0,Null,DestRect)
    
    'Create a new bitmap wth the same dimensions as the original
    Dim B_pixelated as Bitmap
    B_pixelated.initializemutable(B0.width,B0.Height)
    cv.Initialize2(B_pixelated)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,B_pixelated.Width,B_pixelated.Height)
    cv.DrawBitmap(B_small,Null,DestRect)

    return B_pixelated

  Else     
   return B0

  End If

End Sub
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Also, canvas can do the trick by drawing to a smaller bitmap and then drawing the result to another with the same size as the original

Before:D View attachment 38554 After:eek: View attachment 38555


B4X:
Sub Pixelate(B0 As Bitmap) as Bitmap

  Dim factor As Float=8    'Change it to get different pixellations

  Dim new_w As Int = B0.Width/factor
  Dim new_h As Int = B0.Height/factor

  If new_w>=1 And new_h>=1 Then
   
    'Draw the original onto a smaller one
    Dim B_small As Bitmap
    B_small.InitializeMutable(new_w,new_h)
    Dim cv As Canvas
    cv.Initialize2(B_small)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,B_small.Width,B_small.Height)
    cv.DrawBitmap(B0,Null,DestRect)
   
    'Create a new bitmap wth the same dimensions as the original
    Dim B_pixelated as Bitmap
    B_pixelated.initializemutable(B0.width,B0.Height)
    cv.Initialize2(B_pixelated)
    Dim DestRect As Rect
    DestRect.Initialize(0,0,B_pixelated.Width,B_pixelated.Height)
    cv.DrawBitmap(B_small,Null,DestRect)

    return B_pixelated

  Else    
   return B0

  End If

End Sub
@JordiCP - thanks, perfect, you just made my day!
 
Upvote 0
Top