PDA

View Full Version : Image Resizing


srussell
12-28-2007, 04:39 PM
Hello All,

I've looked around the forum and found Intrigued's project using ImageMagic to resize an image, but the EXE seems rather hefty (4.4 MB)

Does anyone know of a a DLL or EXE (free or cheap) and a bit smaller with a command line function that will let me create a 100x100 thumbnail in AutoPlay? All I need is to resize the image... no cropping, etc.

Anyone interested in writing a DLL?

Any input is always appreciated!

Steve Russell

jfxwave
12-28-2007, 05:58 PM
I got this from the forum a while back (I think it was from Worm but not sure)

Dermot
12-28-2007, 06:05 PM
Here is a small dll I created for my needs. Only returns a JPG but can accept BMP, PNG and JPG.

Use it like this.
local SourceImage = "Full Path To Your Image" -- Can be BMP, PNG or JPG
local Width = 100
local Height = 100
local SaveAs = "Full path and file name to save resized image as" -- Must have ".jpg" extension
local Format = "JPG" -- Only JPG supported

local result = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\xImage.dll", "Convert_Image", "\""..SourceImage.."\",\""..SaveAs.."\",\""..Width.."\",\""..Height.."\",\""..Format.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL)

if result == "OK" then
Dialog.Message("Done", "Image resized and saved as "..SaveAs)
else
Dialog.Message("Error", result)
end

srussell
12-29-2007, 09:12 AM
A great thanks... I'll try both of these out! :yes

srussell
12-29-2007, 02:37 PM
Dermot, that worked out perfectly... many thanks!

clueless
01-15-2008, 04:49 AM
Here's a small command line program i wrote that outputs to BMP

Picres is a picture resizer that accepts BMP,PNG,JPG,GIF,icon)
Filesize 74kb

The command line syntax is :
Picres [SourceFile]#[TargetFile]#[Optional new width]#[Optional new height]

File.Run("AutoPlay\\Docs\\MyProg.exe","Pic.bmp#ResizedPic.bmp#200#200", "", SW_SHOWNORMAL, true);

The program can be used as a simple 'to bmp' format coverter if the width and height are left out:
Picres [SourceFile]#[TargetFile]

File.Run("AutoPlay\\Docs\\MyProg.exe","Pic.bmp#ResizedPic.bmp", "", SW_SHOWNORMAL, true);

thetford
01-15-2008, 09:13 PM
what about calling windows GDI32.DLL? Is it possible?

srussell
01-15-2008, 09:14 PM
what about calling windows GDI32.DLL? Is it possible?

Haven't tried it myself, but might be possible? Right now I'm using Dermot's DLL, which works perfectly - thanks Dermot!

yosik
02-28-2008, 04:28 AM
Question to Dermot:
How can you keep the image proportions? Can the resize be done in percentage instead of pizels?
Maybe by checking the original image size? Is there a way to do that?

Thanks

Yossi

RizlaUK
02-28-2008, 07:52 AM
Can the resize be done in percentage instead of pizels?

you can do that in AMS then pass the size to the dll, i have a function in a apz somewhere that divides a number into 100% subtracts or adds the required amount (in%) then returns the amended size based of the original size, just for things like this, but do you think i can find it.....no, i'll keep looking

Dermot
02-28-2008, 11:04 AM
The dll will keep the image proportional so you don't have to worry about. As RizlaUK says, you should be able to use AMS to calculate percentages and come up with the new size and then pass that to the dll.

yosik
02-28-2008, 01:49 PM
Sorry, I don't get it.
Your code makes use of width and height:
local result = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\xImage.dll", "Convert_Image", "\""..SourceImage.."\",\""..SaveAs.."\",\""..Width.."\",\""..Height.."\",\""..Format.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL)

How can I keep proportion if I don't know what they are (image in landscape or portrait for example).
Let's say I have a 640X 480 image. Choosing width=320 and height=240 will give me exactly 1/4 size image without distortion. But if I start with a 480X640 image (portrait) and set the same width (320) and height(240), the image will be distorted.

What I want is a way to resize images to same size but keep proportions.

Thank you

Yossi

Dermot
02-28-2008, 02:23 PM
Sorry, I don't get it.
Your code makes use of width and height:
local result = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\xImage.dll", "Convert_Image", "\""..SourceImage.."\",\""..SaveAs.."\",\""..Width.."\",\""..Height.."\",\""..Format.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL)

How can I keep proportion if I don't know what they are (image in landscape or portrait for example).
Let's say I have a 640X 480 image. Choosing width=320 and height=240 will give me exactly 1/4 size image without distortion. But if I start with a 480X640 image (portrait) and set the same width (320) and height(240), the image will be distorted.

What I want is a way to resize images to same size but keep proportions.

Thank you

Yossi

It will resize the image as close to the speicfied size as possible without distorting. It will never distort. If you want to pass the correct width and height based on the original size then use this
new_width = (new_height * old_width) / old_height
or
new_height = (old_height * new_width) / old_width

Intrigued
02-28-2008, 03:02 PM
Here is a small dll I created for my needs. Only returns a JPG but can accept BMP, PNG and JPG.

Use it like this.
local SourceImage = "Full Path To Your Image" -- Can be BMP, PNG or JPG
local Width = 100
local Height = 100
local SaveAs = "Full path and file name to save resized image as" -- Must have ".jpg" extension
local Format = "JPG" -- Only JPG supported

local result = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\xImage.dll", "Convert_Image", "\""..SourceImage.."\",\""..SaveAs.."\",\""..Width.."\",\""..Height.."\",\""..Format.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL)

if result == "OK" then
Dialog.Message("Done", "Image resized and saved as "..SaveAs)
else
Dialog.Message("Error", result)
end

Mirrored on amsuser.com, here:

www.amsuser.com/ams/examples/xImage-AMS7-Dermot.zip

Thanks Dermont.

yosik
02-28-2008, 03:14 PM
ok. It looks like I didn't explain myself correctly.

I want to use your Dll to batch resize images from a directory. Some images are landscape, some portraits and not all are the same size, so I DON"T KNOW what the size of the images are.
I want all of the images to be resized to a set value (either width or height) but without distorting them.
I have been looking for a way to check an image size from a file, and THEN I could use your method. BUt I couldn't find it.

I hope this is clearer.
Thank you for trying to help.

Yossi

Edit:
It just struck me. There is an Image.GetFileInfo action which I forgot existed. THIS will give me the necessary information I need.
I think I got that baby licked. I will work on that tomorrow. Thank you all for now

Dermot
02-28-2008, 03:31 PM
This example would resize a landscape image to 100 wide and adjust the height as needed and resize a potrait image to 100 high and adjust the width as needed. Not tested.

local SourceIamge = "Path and file name of your original image"
local NewImage = "Path and file name to save the new image as"

-- Get size of original image
local old_height = Image.GetFileInfo(SourceImage).Height
local old_width = Image.GetFileInfo(SourceImage).Width

-- Set new size
local new_width = 100
local new_height = 100

-- Check if the image is landscape or potrait and adjust new size to keep it proportional
if old_width > old_height then
-- Image is landscape
new_height = (old_height * new_width) / old_width
else
-- Image is potrait
new_width = (new_height * old_width) / old_height
end

local result = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\xImage.dll", "Convert_Image", "\""..SourceImage.."\",\""..NewImage.."\",\""..new_width.."\",\""..nwe_height.."\",\""..Format.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL)

longedge
02-28-2008, 04:55 PM
Thanks Dermot works great :yes

I just tried it out on a folder of fairly large images (portrait and landscape) and at first I though it had hung but then watching the output folder I realised that it was just taking longer than I thought.

In case anyone wants it. I had a folder "picfolder" and sub-folder "thumbs" on my desktop. In preload I put -

Format=".jpg"
folder=_DesktopFolder.."\\picfolder\\"
save_folder=folder.."\\thumbs\\"
tbl_pics = File.Find(folder, "*", false, false, nil, nil);
pics_count = Table.Count(tbl_pics);

then in a button on click I put -

for count in tbl_pics do
local SourceImage = tbl_pics[count]
local NewImage = save_folder..count..".jpg"

-- Get size of original image
local old_height = Image.GetFileInfo(SourceImage).Height
local old_width = Image.GetFileInfo(SourceImage).Width

-- Set new size
local new_width = 100
local new_height = 100

-- Check if the image is landscape or potrait and adjust new size to keep it proportional
if old_width > old_height then
-- Image is landscape
new_height = (old_height * new_width) / old_width
else
-- Image is potrait
new_width = (new_height * old_width) / old_height
end

local result = DLL.CallFunction(_SourceFolder.."\\AutoPlay\\Docs\\xImage.dll", "Convert_Image", "\""..SourceImage.."\",\""..NewImage.."\",\""..new_width.."\",\""..new_height.."\",\""..Format.."\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL)
end

(a small typo in the call "nwe_height" corrected).

It handled the landscape and portrait formats perfectly.

Dermot
02-28-2008, 05:31 PM
Thanks Longedge. I don't like posting code I have not tested. You could throw in a progress bar to provide feedback when processing a lot of images.

yosik
02-28-2008, 10:29 PM
Interesting.
That's what did in my project (great minds..).
Funny thing is that even though the processing goes ahead, even with large images and large number of images, the progress bar gets stuck after a few images, as if it cannot keep up with it.
I checked with smaller images (thus less processing work) and there, th eprogress bar works all the way to the last image. Only with large images, that problem occurs.
I am still working on this

Yossi

Dermot
02-29-2008, 01:02 AM
Interesting.
That's what did in my project (great minds..).
Funny thing is that even though the processing goes ahead, even with large images and large number of images, the progress bar gets stuck after a few images, as if it cannot keep up with it.
I checked with smaller images (thus less processing work) and there, th eprogress bar works all the way to the last image. Only with large images, that problem occurs.
I am still working on this

Yossi

If you havn't done so already, add a Page.Redraw() in the loop right after updating the progressbar.

longedge
02-29-2008, 03:11 AM
I put a label on the page and immediately before the dll call added -

Label.SetText("Label1", "There are "..pics_count.." images. Now processing image number "..count);

It gives sufficient feedback for me.

This is ideal as a 'standalone' to add into other projects :yes

yosik
02-29-2008, 07:08 AM
OK.
Thanks both of you. I will try it.

Yossi