Android Question Auto crop unused space from an image

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I have an image with 2 colors, the text in blue and the background is always white.
I need to crop the white part until only the text is left without borders.
I have seen a thread that does it with transparent background, but in this case it is always white.
Any ideas?
Thank you very much.
I6Rzg3V.jpg

The white borders are not visible if the forum background is white.
bBMjhQj.jpg

But I think you can understand what I am saying.
 

peacemaker

Expert
Licensed User
Longtime User
Plain way: check pixel color in a matrix over whole the image, with some step.
And crop the L, R, T, B sides that are without non-white pixels.
1683283128369.png
 
Last edited:
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Thanks, I just had to change "Transparent" to "white" and it worked fine.

Log:
576x314
425x86

B4X:
Private iv As B4XImageView

Sub Button1_Click
    Dim bmp As Bitmap
    bmp.InitializeResize( File.DirAssets, "antes.jpg", 576,512, True) 'ignore
    
    Dim r As B4XRect = FindMinRect(bmp)
    Dim bmp2 As Bitmap = bmp.Crop(r.Left,r.Top,r.Width,r.Height)
    iv.Bitmap = bmp2
    
    Log(bmp.Width &"x"& bmp.Height)
    Log(bmp2.Width &"x"& bmp2.Height)
    
End Sub

Sub FindMinRect (bmp As Bitmap) As B4XRect
    Dim mxui As XUI
    Dim bc As BitmapCreator
    bc.Initialize(bmp.Width, bmp.Height)
    bc.CopyPixelsFromBitmap(bmp)
    Dim r As B4XRect
    r.Initialize(bc.mWidth / 2, -1, bc.mWidth / 2, 0)
    For y = 0 To bc.mHeight - 1
        For x = 0 To bc.mWidth - 1
            If bc.GetColor(x,y) <> mxui.Color_White Then '     IsTransparent(x, y) = False Then
                r.Left = Min(r.Left, x)
                Exit
            End If
        Next
        If x < bc.mWidth Then
            If r.Top = -1 Then
                r.Top = y
            Else
                r.Bottom = y + 1
            End If
            For x = bc.mWidth - 1 To 0 Step -1
                If bc.GetColor(x,y) <> mxui.Color_White Then '    IsTransparent(x, y) = False Then
                    r.Right = Max(r.Right, x + 1)
                    Exit
                End If
            Next
        End If
    Next
    Return r
End Sub
 
Upvote 1

peacemaker

Expert
Licensed User
Longtime User
Does not look to be universal way, for any point content placement... Or universal ?
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Hello, all images have black text and white background.
They are captured signatures.
 
Upvote 0

cklester

Well-Known Member
Licensed User
There is no need to specify the background color. You can determine that by checking the four sides of the image to find the predominant color (if any). If there is a predominant color, especially if it takes up one whole side of the image, assume that is the color you want trimmed. Now you've got something that auto-trims like some kind of AI wizard! If you cannot determine a predominant color, the image need not be trimmed (it is probably already trimmed).
 
Upvote 0

cklester

Well-Known Member
Licensed User
Thank you, and how is that done? :)

According to ChatGPT, there's a Bitmap library that has a GetPixel function that returns the color of a particular pixel.

Basically, get the color of all pixels on the image borders. If three or four of the borders are all one color, assume it is the background color and run your cropping function with that color.

If two sides are of one color (and the others are multiple colors each), assume it is the background color and run your cropping function with that color.

If two sides have solid but separate colors, prompt the user: "Which of these is the background color?" If there is no user interaction, output two images, using each color as the background color.

If one side is a solid color, and the other sides are not, assume the one color is the background and run your cropping function.

There may be other edge cases, but this should cover most images.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
That chatgpt is a bit clumsy, do you want me to ask the user if it's the color?
It's funny :)
Thank you
 
Upvote 0

cklester

Well-Known Member
Licensed User
That chatgpt is a bit clumsy, do you want me to ask the user if it's the color?
It's funny :)
Thank you

You only ask the user if the algorithm cannot determine the background color. However, in most cases, like the image you provided, the algorithm will easily determine the background color.
 
Upvote 0

MikeH

Well-Known Member
Licensed User
Longtime User
@vecino, you already stated twice that you have a bitmap with only two colours. Unless the signature is of a child that likes to "colour in", its highly likely that the colour you want to crop is the pixel of each corner. Why complicate it? The answer has already been given.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Yes, I had not understood well what my colleagues have answered me, it is the problem of having to use the translator.
Thank you very much friends.
 
Upvote 0
Top