Page 1 of 1

PNG Image Palette to Gimp Palette

Posted: Fri Jan 10, 2014 6:21 am
by J. Baker
Something I needed recently and thought it may be of interest. The Gimp palette is a simple format not only supported by Gimp but also apps like Pixen and so forth.

Code: Select all

;---PNG Image Palette to Gimp Palette - PureBasic 5.21 LTS---

Image$ = GetHomeDirectory() + "Desktop/MyImage.png"
Columns$ = "8" ; Number of columns you want your Gimp palette to have. Some apps may overlook this value and scale with the palette window.
#PixelStep = 20 ; Color square size of the image palette.

;---no need to edit anything below---

Define.i X, Y, Color, Number = 0

UsePNGImageDecoder()

If LoadImage(0, Image$)
  CreateFile(0, ReplaceString(Image$, ".png", ".gpl", #PB_String_NoCase))
   WriteStringN(0, "GIMP Palette")
   WriteStringN(0, "Name: " + GetFilePart(Image$, #PB_FileSystem_NoExtension))
   WriteStringN(0, "Columns: " + Columns$)
   WriteStringN(0, "#")
 Else
  MessageRequester("Error!", "Unable to load " + Chr(34) + GetFilePart(Image$) + Chr(34) + ".")
   End
EndIf

StartDrawing(ImageOutput(0))
 For Y = 0 To ImageHeight(0) - 1 Step #PixelStep
    For X = 0 To ImageWidth(0) - 1 Step #PixelStep

      Color = Point(X, Y)
      
      Number + 1
      
      WriteStringN(0, Str(Red(Color)) + " " + Str(Green(Color)) + " " + Str(Blue(Color)) + " Color " + Str(Number))
      
    Next
 Next
StopDrawing()
 
CloseFile(0)
FreeImage(0)