Page 1 of 1
Help with GRABIMAGE
Posted: Sun Dec 01, 2024 10:34 am
by loulou2522
I'd like to cut a part of an image to recreate one without using a gadget. When I submit the following program, Purebasic takes a very long time to execute it
Code: Select all
UsePNGImageEncoder()
UsePNGImageDecoder()
If LoadImage(0, "C:\Users\xxx\BIL000006.png")
hauteur.l = ImageHeight(0)-1200
largeur.l= ImageWidth(0)-1150
GrabImage(0,1,1150,1200,largeur,hauteur)
SaveImage(1,"c:\users\xxx\essai.png",#PB_ImagePlugin_PNG, #PB_Image_FloydSteinberg,4)
FreeImage(0)
FreeImage(1)
EndIf
End
The program takes a very long time to run. Is this normal or have I misused the instructions?
Thanks in advance
Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 11:24 am
by infratec
First:
We can not test ist ... we don't own the picture, we don't have informations about it.
Next:
You need to know the part which takes the time:
Code: Select all
EnableExplicit
Define.i hauteur, largeur
Define.q StartTime, T1, T2, T3
DisableDebugger
UsePNGImageEncoder()
UsePNGImageDecoder()
StartTime = ElapsedMilliseconds()
If LoadImage(0, "C:\Users\xxx\BIL000006.png")
T1 = ElapsedMilliseconds() - StartTime
hauteur = ImageHeight(0) - 1200
largeur= ImageWidth(0) - 1150
If GrabImage(0, 1, 1150, 1200, largeur, hauteur)
T2 = ElapsedMilliseconds() - T1
SaveImage(1,"c:\users\xxx\essai.png",#PB_ImagePlugin_PNG, #PB_Image_FloydSteinberg,4)
T3 = ElapsedMilliseconds() - T2
FreeImage(1)
EndIf
FreeImage(0)
EndIf
EnableDebugger
Debug "T1: " + Str(T1)
Debug "T2: " + Str(T2)
Debug "T3: " + Str(T3)
I don't think that it is GrabImage().
Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 11:35 am
by loulou2522
Here is the result
T1: 585
T2: 74
T3: 33597
It seems it saveimage with takelonger times.
The size of the image is 1605 ko
Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 11:38 am
by TI-994A
loulou2522 wrote: Sun Dec 01, 2024 11:35 amIt seems it saveimage with takelonger times...
Then it would probably boil down to the speed of your storage device.
I tested Bernd's code with a 100MB, 16000 × 9000 pixel, 600-dpi image, with these results:
Faster on an SSD, perhaps.

Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 12:27 pm
by infratec
Repeat without dithering flag. It costs time.
Also play with the bits per pixel.
Maybe you can find an acceptable compromise.
Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 8:58 pm
by loulou2522
Hi infratec,
with jpg image
T1: 333
T2: 140
T3: 1194
It seems the problem come from saving image in png format becaus withh jpg image the result is good
Is-it normal or this is possibly a bug to inform Fred ?
Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 9:39 pm
by infratec
Have you recalculated the native 24 or 32 bit image to 4 bit for the jpg?
Have used dithering in jpg to achieve this?
These are the settings you used for the png. In jpg this is not possible, so it is faster, because no calculation for this is needed..
Have you tried to save it as png without dithering and with 24 or 32 bits according to your image?
Then saving as png should need the same time as saving as jpg,
Re: Help with GRABIMAGE
Posted: Sun Dec 01, 2024 11:31 pm
by PurePilish
It is not surprising that both dithering and using 4-bit depth add a lot of time to PNG compression. I doubt that it is a bug, it's just that those operations are expensive. As others have said, you can probably solve it by saving as JPG, or saving as PNG without dithering and using 24 or 32 bit depth.