Image Size and SAVEIMAGE Problem [Resolved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Image Size and SAVEIMAGE Problem [Resolved]

Post by tikidays »

Sorry if this is a noob question but I have hit a knowledge brick wall and spent the day searching the forums.

How do I get the the size in bytes of an image in memory? Im trying to encode it as a string. In desperation I tried using SAVEIMAGE to the temp directory and using FILESIZE to get the image size, but this resulted in a new problem where SAVEIMAGE is not creating the PNG, not sure why this isn't working. But if I can avoid doing this even better.

Code: Select all

Procedure.s encodePNG(image.i)
  fileName.s = GetTemporaryDirectory()+"temp.png"
  If SaveImage(image,fileName,#PB_ImagePlugin_PNG)
    size.i = FileSize(fileName)
    encoded.s = Base64Encoder(image, size)
    ProcedureReturn encoded
  Else
   Debug "Save error"
  EndIf 
EndProcedure
PureBasic 6.04 LTS - C Backend (MacOS X - arm64)
Last edited by tikidays on Thu Jan 04, 2024 7:30 pm, edited 2 times in total.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Image Size and SAVEIMAGE Problem

Post by boddhi »

When you want to save your image in PNG format, you need to use UsePNGImageEncoder().
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
wombats
Enthusiast
Enthusiast
Posts: 718
Joined: Thu Dec 29, 2011 5:03 pm

Re: Image Size and SAVEIMAGE Problem

Post by wombats »

Check out the EncodeImage command.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Image Size and SAVEIMAGE Problem

Post by Little John »

@tikidays:
Please don't ask the same coding question in 3 different forum threads. TIA.
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

Little John wrote: Sun Dec 31, 2023 9:18 am @tikidays:
Please don't ask the same coding question in 3 different forum threads. TIA.
Apologies, but they were a progression of questions as I looked into it more.
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

boddhi wrote: Sun Dec 31, 2023 8:42 am When you want to save your image in PNG format, you need to use UsePNGImageEncoder().
Thanks for the heads-up, I had no idea I had to invoke this again inside a new procedure! I assumed once at the start of the code was it.
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

wombats wrote: Sun Dec 31, 2023 9:10 am Check out the EncodeImage command.
Thanks so much, I had seen this but was unsure how it could help, but re-reading it this will solve my problem, thanks a lot!
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Image Size and SAVEIMAGE Problem

Post by Little John »

tikidays, wrote:I assumed once at the start of the code was it.
This assumption was/is right: UsePNGImageEncoder() needs only to be called once in a program (normally somewhere at the beginning).
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

Little John wrote: Sun Dec 31, 2023 6:13 pm
tikidays, wrote:I assumed once at the start of the code was it.
This assumption was/is right: UsePNGImageEncoder() needs only to be called once in a program (normally somewhere at the beginning).
Thanks, rechecking I discovered I had moved this command into a LoadImages procedure, moved it back out.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Image Size and SAVEIMAGE Problem

Post by boddhi »

@tikidays

F1 is a magic key !
Sometimes when you invoke it, it makes problems disappear ! :mrgreen: :wink:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

tikidays wrote: Sun Dec 31, 2023 6:28 pm
Little John wrote: Sun Dec 31, 2023 6:13 pm
tikidays, wrote:I assumed once at the start of the code was it.
This assumption was/is right: UsePNGImageEncoder() needs only to be called once in a program (normally somewhere at the beginning).
Thanks, rechecking I discovered I had moved this command into a LoadImages procedure, moved it back out.
Correction I had to re-add it for the encoder to work, if I place it in the parent Procedure its happy. I wouldn't have expected this behaviour.
Little John
Addict
Addict
Posts: 4789
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Image Size and SAVEIMAGE Problem

Post by Little John »

in orfder to make things not more complicated than necessary, put UsePNGImageEncoder() in the main scope of the program, not in a procedure.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Image Size and SAVEIMAGE Problem

Post by boddhi »

@tikidays

As said Little John, to be safe, place this instruction outside and before all image-related procedures and commands. :wink:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

Do I place both

Code: Select all

UsePNGImageEncoder()
UsePNGImageDecoder()
At the start?

The encoding seemed straight forward but my decoder is still not working...the window is to see what image it decodes, but it's showing some random, not what was encoded. :/ I think I have a fundamental mis-understanding of how to address images?

Code: Select all

Procedure decodePNG(PNG.s,size.i)
  Debug "Decoding: " + PNG
  size * 0.65 : If size < 64 : size = 64 : EndIf
  *buffer = AllocateMemory(size)
  If *buffer
    If imageSize = Base64Decoder(PNG,*buffer,size)
      image = CatchImage(#PB_Any, *buffer)
      
     OpenWindow(#PB_Any,0,0,1024,1024,"Decoded image "+Str(imageSize/1024)+"kb")
     ImageGadget(#PB_Any,0,0,1024,1024,ImageID(image))
      
      FreeMemory(*buffer)
      ProcedureReturn image
    Else
      Debug "imageSize : Decode failed."
    EndIf
  Else 
    Debug "*buffer : Decode failed."
  EndIf
  FreeMemory(*buffer)
EndProcedure
User avatar
tikidays
User
User
Posts: 46
Joined: Tue Oct 24, 2023 6:47 am
Location: New Zealand
Contact:

Re: Image Size and SAVEIMAGE Problem

Post by tikidays »

boddhi wrote: Sun Dec 31, 2023 6:49 pm @tikidays

F1 is a magic key !
Sometimes when you invoke it, it makes problems disappear ! :mrgreen: :wink:
you mean fn-F1 for my Mac :lol:
Post Reply