PNG count not transparent pixels

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

PNG count not transparent pixels

Post by infratec »

Hi,

I needed to compare the area of 2 estates.
Ok, I used google maps and grabbed the pictures.
I loaded the pictures in gimp and cut out the estates.
Then I saved each estate as png with transparence.

Now I needed only a tool to count the pixels.
After a short time of searching I used my 'swiss army knife' called PB :mrgreen:

Code: Select all

UsePNGImageDecoder()

Filename$ = OpenFileRequester("Choose a file", "", "PNG|*.png", 0)
If Filename$ <> ""
  
  If LoadImage(0, Filename$)
    
    w = ImageWidth(0)
    h = ImageHeight(0)
    
    If StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_AllChannels)
      
      For y = 0 To h - 1
        For x = 0 To w - 1
          P = Point(x, y)
          If Alpha(P) <> 0
            ;Debug Str(x) + "x" + Str(y) + " " + Str(Alpha(P))
            Count + 1
            Plot(x, y, $FF0000FF)
          EndIf
        Next x
      Next y
      
      StopDrawing()
      
      SaveImage(0, Filename$ + ".bmp", #PB_ImagePlugin_BMP)
      
      MessageRequester("Info", "Found " + Str(Count) + " pixel")
      
    EndIf
  EndIf
EndIf
As test for the truth I store a bmp with all counted pixels in red.
In my case it worked.

Maybe someone can use it too.

Happy new year,

Bernd
Last edited by infratec on Sun Jan 01, 2017 3:14 pm, edited 1 time in total.
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: PNG count not transparent pixels

Post by Bisonte »

/OT
infratec wrote:After a short time of searching I used my 'swiss army knife' called PB :mrgreen:
I love this moments :D

OT/
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: PNG count not transparent pixels

Post by infratec »

An extended version:

Code: Select all

EnableExplicit

#Size = 380

Enumeration
  #OrigPicture
  #SmallOrigPicture
  #SmallTestPicture
EndEnumeration


Define.i w, h, x, y, P, Count
Define.f FactorX, FactorY, Factor
Define Filename$

UsePNGImageDecoder()

Filename$ = OpenFileRequester("Choose a file", "", "PNG|*.png", 0)
If Filename$ <> ""
  
  If LoadImage(#OrigPicture, Filename$)
    
    w = ImageWidth(#OrigPicture)
    h = ImageHeight(#OrigPicture)
    
    CopyImage(#OrigPicture, #SmallOrigPicture)
    
    FactorX = #Size / w
    FactorY = #Size / h
    
    If FactorX < FactorY
      Factor = FactorX
    Else
      Factor = FactorY
    EndIf
    
    If Factor < 0
      Factor = 1.0
    EndIf
    
    ResizeImage(#SmallOrigPicture, w * Factor, h * Factor)
    
    OpenWindow(0, 0, 0, w * Factor * 2 + 30, h * Factor + 30 + 20, "PixelCount", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    
    ImageGadget(0, 10, 10, 0, 0, ImageID(#SmallOrigPicture), #PB_Image_Border)
    ImageGadget(1, 10 + w * Factor + 10, 10, w * Factor, h * Factor, 0, #PB_Image_Border)
    
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(200)
    
    
    If StartDrawing(ImageOutput(#OrigPicture))
      DrawingMode(#PB_2DDrawing_AllChannels)
      
      For y = 0 To h - 1
        For x = 0 To w - 1
          P = Point(x, y)
          If Alpha(P) <> 0
            ;Debug Str(x) + "x" + Str(y) + " " + Str(Alpha(P))
            Count + 1
            Plot(x, y, $FF0000FF)
          EndIf
        Next x
      Next y
      
      StopDrawing()
            
      CopyImage(#OrigPicture, #SmallTestPicture)
      ResizeImage(#SmallTestPicture, w * Factor, h * Factor)
      SetGadgetState(1, ImageID(#SmallTestPicture))
      
      If Count = 0
        StatusBarText(0, 0, "no transparent PNG", #PB_StatusBar_Center)
      Else
        StatusBarText(0, 0, "Found " + Str(Count) + " visible pixels", #PB_StatusBar_Center)
      EndIf
      
    EndIf
    
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
    
  EndIf
EndIf
Bernd
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: PNG count not transparent pixels

Post by netmaestro »

Nice idea, kind of like dropping a blob of butter into a measuring cup that contains a predetermined amount of water. Butter raises the level, by how much tells you the volume of butter.

Switching to estates, if you know the greatest distance North to South and the greatest distance East to West covered by the estate, say it's 100m each way just for an example, the total would be 10,000 square meters. You could calculate the sqare meters of the estate like this:

number of visible px
----------------------- X 10,000
total number of px

which looks to me like a fair way to get the number of square meters covered by the estate.
BERESHEIT
Post Reply