Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

ConvertImage() Convert and resize images with 1 function


  • Please log in to reply
27 replies to this topic
tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
ConvertImage()

ConvertImage() can be used to convert from any image format supported by gdi+ to any other format also supported (jpg,bmp,png,tiff,gif). It can also resize the image either by directly specifying the width and height in pixels of the destination image, or by specifying a percentage of the source image.

Many thanks to PhiLho, Lexikos and Sean

Usage:

ConvertImage(sInput, sOutput, Width="", Height="", Method="Percent")

sInput: Location of the input file to be converted. May be of filetype jpg,bmp,png,tiff,gif
sOutput: Location to save the converted file. Extension determines the output filetype jpg,bmp,png,tiff,gif
Width: Width either in pixels or percentage (depending on Method) to save the converted file
Height: Height either in pixels or percentage (depending on Method) to save the converted file
Method: Can either be "Percent" or "Pixels" to determine the width and height of the converted file
Examples:

; Convert "in.jpg" to "out.png" and keep same size
ConvertImage("in.jpg", "out.png")

; Convert "in.jpg" to "out.png" and make it 200x100 pixels
ConvertImage("in.jpg", "out.png", 200, 100, "Pixels")

; Convert "in.png" to "out.bmp" and make it 1/2 the width (50%) and twice the height (200%)
ConvertImage("in.png", "out.bmp", 50, 200, "Percent")

; Convert "in.png" to "out.bmp" and make it 200 pixels wide, whilst maintaining aspect ratio
ConvertImage("in.png", "out.bmp", 200, -1, "Pixels")

Function with working example:

gdi+ must first be started as shown in the example script.

ConvertImage.ahk

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008
Wonderful script, already making a simple GUI for myself to support it :D

One question; going through the function, I notice many different possible return values. I'm assuming these are for different errors, in which case could you tell me what the various values might mean?

Edit: Also, one more question >< Is this function enabled for using -1 as one dimension to maintain aspect ratio, or is it necessary to calculate it manually?

biotech
  • Members
  • 172 posts
  • Last active: Jan 08 2011 03:16 PM
  • Joined: 23 Feb 2006
great script!
is it possible to change just height or width? and image to be resized
repecting original aspect?

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
Hey, that is handy. Very nice job and well done. I posted a different thread, but on GDI topic in the main group. It would be neat to see some of what I was asking evolve into this (image composite layering & joining) :) Have a very nice day!

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007

Wonderful script, already making a simple GUI for myself to support it :D

One question; going through the function, I notice many different possible return values. I'm assuming these are for different errors, in which case could you tell me what the various values might mean?


I have added the return values to the top of the function now

great script!
is it possible to change just height or width? and image to be resized
repecting original aspect?


Good idea. -1 may now be specified for either Width or Height to maintain aspect ratio.

biotech
  • Members
  • 172 posts
  • Last active: Jan 08 2011 03:16 PM
  • Joined: 23 Feb 2006
one more thing...i have monochrome tiff with 200kb in size when i resize them they appear much bigger over 2Mb and they are now 32-bits tiffs...is there a way to maintain original 1-bit tiff after resize. if so this i would probably use this script because it is going to be the most easiest way of doing this...

thanks!

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008

I have added the return values to the top of the function now

...

Good idea. -1 may now be specified for either Width or Height to maintain aspect ratio.


Great, thanks :3

Bral
  • Members
  • 1 posts
  • Last active: Nov 03 2011 09:12 AM
  • Joined: 29 Sep 2007
How to adjust image quality?

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Check out the gdi+ library. This function has been superceded by it

DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
How can I use a variable in this
ConvertImage("in.jpg", "out.png")

My variable for example would be
Mylistbox = Pic.jpg ; I want to convert it to Pic.bmp
;this does not work
ConvertImage(mylistbox,mylistbox.bmp)

Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
ConvertImage(mylistbox,[color=red]"[/color]mylistbox.bmp[color=red]"[/color])


DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008

ConvertImage(mylistbox,[color=red]"[/color]mylistbox.bmp[color=red]"[/color])

I wanted to retain the original filename but converted to bmp. This converts the image to mylistbox.bmp
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

oliver.lipkau
  • Members
  • 97 posts
  • Last active: Jul 29 2015 07:12 AM
  • Joined: 17 Sep 2006
Try
Mylistbox = Pic
ConvertImage(mylistbox . ".jpg",mylistbox . ".bmp")


DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008

Try

Mylistbox = Pic
ConvertImage(mylistbox . ".jpg",mylistbox . ".bmp")

Mylistbox is a variable defined somewhere else in my script. It already includes the absolute path and filename. If I make Mylistbox = pic then it would be converting pic.jpg which does not exist.

Am I correct?
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

oliver.lipkau
  • Members
  • 97 posts
  • Last active: Jul 29 2015 07:12 AM
  • Joined: 17 Sep 2006

Try

Mylistbox = Pic
ConvertImage(mylistbox . ".jpg",mylistbox . ".bmp")

Mylistbox is a variable defined somewhere else in my script. It already includes the absolute path and filename. If I make Mylistbox = pic then it would be converting pic.jpg which does not exist.

Am I correct?

take a look at SplitPath or StringTrimLeft