ImageSplitter code

Share your advanced PureBasic knowledge/code with the community.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

ImageSplitter code

Post by dracflamloc »

Code updated for 5.20+

[edit] Made it ask to resize the tiles too [/edit]

This will split an image into individual images based on your specified height/width. Pretty handy for converting the free tilesets you find on the internet into something you can use in the warg engine. Also good for other things too.

Code: Select all

;This code is frikkin free, hell yea! (orig by dracflamloc)

Debug GetCurrentDirectory()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

file.s=ProgramParameter()
If file=""
  file.s=OpenFileRequester("Choose the image to open...","","*.*",0)
EndIf
fileheader.s=InputRequester("File prefix","Enter the name of the file prefix:","tile")
w.l=Val(InputRequester("Split","Enter the width to split at:","64"))
h.l=Val(InputRequester("Split","Enter the height to split at:","64"))
rw.l=Val(InputRequester("Split","Enter the width to resize to:",Str(w)))
rh.l=Val(InputRequester("Split","Enter the height to resize to:",Str(h)))
smooth=MessageRequester("Smooth","Smooth the images?",#PB_MessageRequester_YesNo)
If smooth=#PB_MessageRequester_Yes
  smooth=1
Else
  smooth=0
EndIf 

If w=0 Or h=0
  MessageRequester("","Invalid size")
  End
EndIf

If file.s=""
  MessageRequester("","Invalid file")
  End
EndIf

LoadImage(0,file)
If IsImage(0)=0
  MessageRequester("","Not an image")
  End
EndIf

th.l=ImageHeight(0)
tw.l=ImageWidth(0)
If th>h And tw>w
  yi=th/h
  If th%h>0
    yi+1
  EndIf
  xi=tw/w
  If tw%w>0
    xi+1
  EndIf
 
  tot.s=Str(xi*yi)
   
  For i=0 To yi-1
    For j=0 To xi-1
      c+1
      If Len(Str(c))<Len(tot)
        cs.s=ReplaceString(Space(Len(tot)-Len(Str(c)))," ","0")+Str(c)
      Else
        cs.s=Str(c)
      EndIf
      im=GrabImage(0,#PB_Any,j*w,i*h,w,h)
      If w<>rw Or h<>rh
        If smooth
          ResizeImage(im,rw,rh,#PB_Image_Smooth)
        Else
          ResizeImage(im,rw,rh,#PB_Image_Raw)
        EndIf 
      EndIf 
      SaveImage(im,"./"+fileheader+cs+".png",#PB_ImagePlugin_PNG) ;saves files in parent of current directory
      FreeImage(im)
    Next
  Next
 
  MessageRequester("Success","Split to "+Str(c)+" images.")
Else
  MessageRequester("","Image is smaller than the specified size")
  End
EndIf
Last edited by dracflamloc on Thu Apr 20, 2006 4:14 pm, edited 1 time in total.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

Thanks! This is very useful to me! Now I don't need anymore to work hours on the computer to use sprite sheets I downloaded from the net!
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

small update
Post Reply