Mass Photo Shrink

Blitz3D Forums/Blitz3D Beginners Area/Mass Photo Shrink

AJ00200(Posted 2009) [#1]
Does anyone know of a program to shrink the file-size of photos.

I have 27 4MB photos that are being loaded as sprites, and it goes really slow.


_PJ_(Posted 2009) [#2]
Compress them, perhaps another format such as JPG or PNG or resize the images themselves?

Cooolsoft's Advanced Batch Converter can convert, resize/rescale and much much more.

Why do many sprites? Perhaps it might be easier to change your program a bit to manage the loading better, Do you really NEED ALL 27.4Mb worth in memory at the same time?


Stevie G(Posted 2009) [#3]
If you convert/compress in another format ( PNG is best for lossless compression ) then they will take less disk space but they will still take up the same memory as a BMP equivalent. Best to resize them all.

Stevie


_PJ_(Posted 2009) [#4]
If you convert/compress in another format ( PNG is best for lossless compression ) then they will take less disk space but they will still take up the same memory as a BMP equivalent.

Good point, yes.

You can easily resize the actual images yourself with a couple of Blitz functions:

Just call:
ProcessImageBatch("DIRECTORY PATH")

and the program should resize all valid image files within that directory folder.

Const MaxWidth=800
Const MaxHeight=600

Function ProcessImageBatch(Directory$="")
	If (Directory$="")
		Directory$=CurrentDir$()
	End If
	
	If (Right$(Directory$,1)<>"\")
		Directory$=Directory$+"\"
	End If
	
	If (Not(FileType(Directory$)=2))
		RuntimeError Directory$+" is not a valid directory"
	End If
	
	Local ScanDir%=ReadDir(Directory$)
	Local FullPath$
	Local Extension$
	Local Temporary%
	Local CountProcessed%=0
	Local ImageFile$=NextFile$(ScanDir)
	While (ImageFile$<>"")
		FullPath$=Lower$((Directory$+ImageFile$))
		If (FileType(FullPath$)=1) And (Right$(ImageFile$,1)<>".")
			Extension$=GetExtension$(ImageFile$)
			;Edit these extensions as necessary
			If ((Extension$="bmp")+(Extension$="jpg")+(Extension$="jpeg")+(Extension$="png"))
				DebugLog "Processing: "+ImageFile$
				Temporary%=LoadImage(FullPath$)
				ScaleImageToProportion(Temporary)
				SaveBuffer(ImageBuffer(Temporary),Left$(FullPath$,Len(FullPath$)-(Len(Extension$)+1))+".bmp")
				CountProcessed%=CountProcessed%+1
			End If
		End If
		ImageFile$=NextFile$(ScanDir)
	Wend
	RuntimeError Str$(CountProcessed%)+" Images Processed."
End Function

Function GetExtension$(FileName$)
	If ((Not(Instr(FileName$,"."))) Or (FileName$=""))
		Return ""
	End If
	Local nCount
	For nCount = Len(FileName$) To 1 Step -1
		If (Mid$(FileName$,nCount)=".")
			Return Right$(Lower$(FileName$),Len(FileName$)-(nCount+1))
		End If
	Next
	Return ""
End Function

Function ScaleImageToProportion(Image%)
	
	Local Width%=ImageWidth(Image)
	Local Height%=ImageHeight(Image)
	
	Local ImageFormat=(Width>=Height)
	
	Local RatioWidth#=Float(Float(Width) / Float(MaxWidth))
	Local RatioHeight#=Float(Float(Height) / Float(MaxHeight))
	
	If (Width>=Height)
		RatioHeight#=RatioWidth#
	Else
		RatioWidth#=RatioHeight#
	End If
	
	Local NewWidth#=Float(Float(Width)/RatioWidth#)
	Local Newheight#=Float(Float(Height)/RatioHeight#)
	
	DebugLog Str$(Width%)+" x "+Str$(Height%)
	DebugLog "Resized to:"
	DebugLog Str$(Int(NewWidth))+" x "+Str$(Int(Newheight))
	DebugLog "--------------"
	ResizeImage Image,NewWidth#,Newheight#
	
End Function



If you intend to use this, I recommend testing it on a couple of copies of images first, just in case. I dont want to be responsible for ruiining your holiday snaps or anything :D


AJ00200(Posted 2009) [#5]
OK, I'm trying that out now.
Yes, I need all 27 sprites at once, but I want an easy way to shrink the photo size (width and height) to save space and load time.

EDIT: Didn't work


AJ00200(Posted 2009) [#6]
In case you were wondering,


This is what is was doing, and I single-handedly (OK, I used 2 to type) used more RAM than FireFox.
By shrinking the photos by hand, It now takes less RAM than FireFox :)

Notice how it uses my much enjoyes 3D Movement Keys UserLib :) LOL


_PJ_(Posted 2009) [#7]
EDIT: Didn't work

That's not very informative...
Did it give an error on runtime or not compile?

Did you put in the correct directory path into the function,
I assume something like ( WHATEVERDIRECTORY YOUR APP IS IN + "ph\" )
and check the
"Maxwidth" ansd "MaxHeight" values to suit your needs? I defaulted them to 800x600 but that may still be too big.


AJ00200(Posted 2009) [#8]
It just said 0 converted.
Don't need it anymore, did it by hand, but thanks so much for your help.
If I ever need it again, I'll dig this back out and try it again.

Thanks Again,
AJ00200


AJ00200(Posted 2009) [#9]
I found another time to use it, and it still doesn't work.
It just keeps givinge same error, 0 Images converted. I checked the width and height values, I have a full path C:\..., and I did a bit of troubleshooting myself, but it just won't work.

PS, I'm using *.JPG Files if that matters.


Ross C(Posted 2009) [#10]
I'm sure blitz only saves .bmp files, so you'd need to use a lib like freeimage, to save them as .jpg's


AJ00200(Posted 2009) [#11]
Oh, Ok. That helps alot.
Thanks Ross.


Matty(Posted 2009) [#12]
Or you could use something like irfanview which has batch image processing ability.