Reducing image file sizes
Jim Bufalini
jim at visitrieve.com
Mon May 18 22:55:02 EDT 2009
Hi again Jacque,
> I have a customer project that displays scanned images and photos of
> varying dimensions. These files are usually huge, with formatted sizes
> in thousands of pixels. I need a way to change the image resolution, or
> otherwise reduce the file size, so that the images can be rewritten to
> disk with a much smaller footprint in order to fit them on a single CD.
> But I need to preserve the original dimensions in inches, so that they
> will still print at their actual, original size. I think I need to
> determine the original resolution so that I can calculate the
> dimensions
> in inches, right? Is there a way to get that?
Actually try playing with this. It was an early version of the handler and I
forget even why it worked but it can take as input any format but always
saves as Jpeg. This then I believe can also be combined with a compress(). I
have to search for the later versions. Sorry, rushed. Later...
ON mouseUp
local tData,tNewData
-----
answer file "Choose a large jpeg image:"
IF it = empty THEN exit mouseUp
put url("binfile:" & it) into tData
put ReduceImage(tData) into tNewData --
put "#2" after char -5 of it
put tNewData into url("binfile:" & it)
beep
END mouseUp
-----------------------------------
FUNCTION ReduceImage pData
local tWidth,tHeight
constant kMaxSize = 800
constant kJpegQuality = 60
-----
put the jpegquality into tJpegQuality
create invisible image "Temp"
set the text of image "Temp" to pData
put the formattedwidth of image "Temp" into tWidth
put the formattedheight of image "Temp" into tHeight
-----
IF tWidth > kMaxSize OR tHeight > kMaxSize THEN
IF tWidth > tHeight THEN
set the width of image "Temp" to kMaxSize
set the height of image "Temp" to round(tHeight * (kMaxSize/tWidth))
ELSE
set the height of image "Temp" to kMaxSize
set the width of image "Temp" to round(tWidth * (kMaxSize/tHeight))
END IF
set the jpegquality to kJpegQuality
export image "Temp" to pData as JPEG
END IF
delete image "Temp"
set the jpegquality to tJpegQuality
return pData
END ReduceImage
Aloha from Hawaii,
Jim Bufalini
More information about the use-livecode
mailing list