Logiciel pour recadrer une animation

Vous avez développé un logiciel en PureBasic et vous souhaitez le faire connaitre ?
Avatar de l’utilisateur
DjPoke
Messages : 121
Inscription : mar. 02/nov./2010 13:53
Localisation : Corte, Corse, France
Contact :

Logiciel pour recadrer une animation

Message par DjPoke »

Bonjour à la communauté.

J'ai développé un petit logiciel qui recherche le meilleur recadrage possible pour une animation.
Je m'explique...

Imaginez que vous ayez fait une animation de personnage avec Daz3D, rendue au format PNG avec un fond transparent.
Si vous recadrez manuellement les images avec un logiciel comme Photoshop Elements, difficile de conserver le point chaud (point central) de l'animation au même endroit.

Le script à compiler pour windows que je vous fournis scanne toute la série d'images que vous avez, cherche le meilleur cadre qui englobe toutes vos images, et fait le découpage, puis la sauvegarde.

Il vous faudra une série d'image numérotées comme ceci : NomImage_NuméroImage.png
Par exemple :
Sprite_Walking_01.png
Sprite_Walking_02.png
Sprite_Walking_03.png
etc...

Ensuite, suivez les instructions (en Anglais) dans le script. Pour l'instant, il est en mode console, mais je pense qu'il est facilement adaptable dans un environnement fenêtré pour Linux et Mac OS X.

Exemple de série d'images recadrées :
Image

Voici le source pour PB 5.5 :

Code : Tout sélectionner

; ImgCrop, by Retro-Bruno (c) 2016

UsePNGImageEncoder()
UsePNGImageDecoder()

OpenConsole()
EnableGraphicalConsole(1)

Repeat
  
ClearConsole()

PrintN("ImgCrop")
PrintN("-------")
PrintN("")

Print("Press [RETURN] to pick the 1st PNG image from an animation")
PrintN("")
Input()
PrintN("")

; *** Pick the 1st PNG file of an animation. Ex: "Male_Walking_0001.png" ***
f$ = OpenFileRequester("Load the 1st PNG file from an animation...", "", "Fichier PNG|*.PNG", 0)

If f$ <> ""
  p$ = LCase(GetPathPart(f$))
  nbase$ = GetFilePart(f$, #PB_FileSystem_NoExtension)
  n$ = LCase(nbase$)
  e$ = LCase(GetExtensionPart(f$))
  If e$ <> "png"
    PrintN("Not a PNG file, canceled...")
    PrintN("")
  Else
    If Right(n$, 1) < "0" Or Right(n$, 1) > "9"
      PrintN("The number of image is missing, canceled...")
      PrintN("")
    Else
      
      v$ = ""
      While Right(n$, 1) >= "0" And Right(n$, 1) <= "9"
        v$ = Right(n$, 1) + v$
        n$ = Left(n$, Len(n$) - 1)
        nbase$ = Left(nbase$, Len(nbase$) - 1)
      Wend
      
      StartValue = Val(v$)
      
      PrintN("First Image File : " + Chr(34) + n$ + v$ + "." + e$ + Chr(34))
      PrintN("First Image Number : " + StartValue)
      PrintN("")
      
      x1 = 0
      x2 = 8191
      y1 = 0
      y2 = 8191
      
      PrintN("Searching borders...")
      PrintN("")
      
      i = StartValue
      StrNumber$ = v$
      
      While FileSize(p$ + nbase$ + StrNumber$ + "." + e$) <> -1
        
        If LoadImage(1, p$ + nbase$ + StrNumber$ + "." + e$) = 0
          PrintN("Can't load PNG image number : " + StrNumber$)
          PrintN("")
          Break(2)
        EndIf
        
        w = ImageWidth(1)
        h = ImageHeight(1)
          
        If i = StartValue
          x2 = w - 1
          y2 = h - 1
        EndIf
        
        StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_AlphaChannel )
        
        
        ; Searching left border
        For x = x1 To x2
          For y = y1 To y2
            If Alpha(Point(x, y)) > 0
              If i = StartValue And x > x1 : x1 = x : EndIf
              Break(2) 
            EndIf
          Next
        Next
        
        For x = x1 - 1 To 0 Step -1
          sum = 0
          For y = y1 To y2
            If Alpha(Point(x, y)) > 0
              sum + 1
            EndIf
          Next
          If sum > 0 And x < x1
            x1 = x
          EndIf
          If sum = 0
            Break
          EndIf
        Next
        
        ; Searching right border
        For x = x2 To x1 Step -1
          For y = y1 To y2
            If Alpha(Point(x, y)) > 0
              If i = StartValue And x < x2 : x2 = x : EndIf
              Break(2) 
            EndIf
          Next
        Next
        
        For x = x2 + 1 To w - 1
          sum = 0
          For y = y1 To y2
            If Alpha(Point(x, y)) > 0
              sum + 1
            EndIf
          Next
          If sum > 0 And x > x2
            x2 = x
          EndIf
          If sum = 0
            Break
          EndIf
        Next
        
        ; Searching top border
        For y = y1 To y2
          For x = x1 To x2
            If Alpha(Point(x, y)) > 0
              If i = StartValue And y > y1 : y1 = y : EndIf
              Break(2) 
            EndIf
          Next
        Next
        
        For y = y1 - 1 To 0 Step -1
          sum = 0
          For x = x1 To x2
            If Alpha(Point(x, y)) > 0
              sum + 1
            EndIf
          Next
          If sum > 0 And y < y1
            y1 = y
          EndIf
          If sum = 0
            Break
          EndIf
        Next
        
        ; Searching bottom border
        For y = y2 To y1 Step -1
          For x = x1 To x2
            If Alpha(Point(x, y)) > 0
              If i = StartValue And y < y2 : y2 = y : EndIf
              Break(2) 
            EndIf
          Next
        Next
        
        For y = y2 + 1 To h - 1
          sum = 0
          For x = x1 To x2
            If Alpha(Point(x, y)) > 0
              sum + 1
            EndIf
          Next
          If sum > 0 And y > y2
            y2 = y
          EndIf
          If sum = 0
            Break
          EndIf
        Next
        
        StopDrawing()
          
        FreeImage(1)
        
        i + 1
        
        StrNumber$ = Str(i)
        While Len(StrNumber$) < Len(v$)
          StrNumber$ = "0" + StrNumber$
        Wend
                        
      Wend
      
      EndValue = i - 1

      PrintN("X: " + Str(x1) + "  Y: " + Str(y1))
      PrintN("Width: " + Str(x2 - x1 + 1) + "  Height: " + Str(y2 - y1 + 1))
      PrintN("")
      PrintN("Enter a name for the animation")
      PrintN("Type " + nbase$ + " to overwrite your animation")
      
      newf$ = ""
      While newf$ = "" Or FindString(newf$, "*") > 0 Or FindString(newf$, "*") > 0 Or FindString(newf$, "*") > 0 Or FindString(newf$, "*") > 0
        newf$ = Input()
        If newf$ = "" Or FindString(newf$, "*") > 0 Or FindString(newf$, "*") > 0 Or FindString(newf$, "*") > 0 Or FindString(newf$, "*") > 0
          PrintN("Bad file name, please retry") 
        EndIf
      Wend     
      
      For i = StartValue To EndValue
        
        StrNumber$ = Str(i)
        While Len(StrNumber$) < Len(v$)
          StrNumber$ = "0" + StrNumber$
        Wend

        If LoadImage(1, p$ + nbase$ + StrNumber$ + "." + e$) = 0
          PrintN("Can't load PNG image number : " + StrNumber$)
          PrintN("")
          Break
        EndIf
        
        GrabImage(1, 2, x1, y1, x2 - x1 + 1, y2 - y1 + 1)       
        FreeImage(1)
        
        If SaveImage(2, p$ + newf$ + StrNumber$ + "." + e$, #PB_ImagePlugin_PNG) = 0
          PrintN("Can't save PNG image number : " + StrNumber$)
          PrintN("")
          Break
        EndIf
        
        FreeImage(2)
        
      Next
      
      PrintN("Done !")
      PrintN("")
      
    EndIf
  EndIf
Else
  PrintN("Canceled...")
  PrintN("")
EndIf

PrintN("Do you want to crop another animation ?")
PrintN("Press [y] for 'yes'")
PrintN("")
a$ = Input()
If UCase(a$) <> "Y" : Break : EndIf

ForEver

CloseConsole()
End