VB icon

Convert and Compress Images to JPG

Email
Submitted on: 4/7/2018 3:59:32 PM
By: A_X_O 
Level: Beginner
User Rating: Unrated
Compatibility: VbScript (browser/client side)
Views: 1322
 
     Drag and Drop multiple images of different formats directly onto this script to convert them all to JPG image files. This project uses the Windows Image Acquisition Object.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Convert and Compress Images to JPG
' Description:Drag and Drop multiple images of different formats directly onto this script to convert them all to JPG image files.
This project uses the Windows Image Acquisition Object.
' By: A_X_O
'**************************************

'
'
'-------------------------------------------------------------------------------'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'		Demonstration: Windows 10, Windows Image Acquisition		'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'										'
'	Purpose		:	VBScript Windows Image Acquisition		'
'	--------------------------------------------------------------		'
'	Creation Date	: 	20/03/2018 [dd/mm/yyyy]				'
'	Version		:	1.0						'
'	Designer	:	Fabian						'
'										'
'###############################################################################'
'			MODIFICATION HISTORY					'
'-------------------------------------------------------------------------------'
'	Version		:	1:0	20/03/2018	Create the Sample	'
'	Version		:	1:1	26/03/2018	Fix Compression Fault 	'
'										'
'-------------------------------------------------------------------------------'		
'
Public Const wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"		'	Fix: Bit_Map_Picture Constant
Public Const wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"		'	Fix: Portable Network Graphic Constant
Public Const wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"		'	Fix: Graphics_Interchange_Format Constant
Public Const wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"		'	Fix: Joint_Photographic_Experts_Group Constant 
Public Const wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"		'	Fix: Tagged_Image_File_Format Constant
'
Public Const DIR_JPEG = "\JPEG_DIR\"						'	Constant OutPut Directory Path	
Public Const DeskDir = "Desktop"						'	Constant Special Folder
Public Const IMGQUAL = 25							'	Constant JPG Quality
Public Const ImageExt = ".jpg"							'	Constant JPG Extention
'
Dim WshShell									'	Memory Dimentions For Windows Scripting Host - Shell
Dim FSO										'	Memory Dimentions For File System Object
'
Dim IP										'	Memory Dimentions WIA - Image Process
Dim IMG										'	Memory Dimentions For New Image File
'
Dim Args									'	Memory Dimentions For WScript Arguents
Dim iCounter									'	Memory Dimentions For Dropped Files
Dim StrMyDesktop								'	Memory Dimentions For Special Folder Name
Dim RetVal									'	Memory Dimentions For Create Folder Function
Dim JPGFolder									'	Memory Dimentions For JPG Directory
Dim JPGDir									'	Memory Dimentions For JPG Directory
'
Set IMG = WScript.CreateObject("WIA.ImageFile")					'	Create Windows Image Acquistition Object - Image
Set IP = WScript.CreateObject("WIA.ImageProcess")				'	Create Windows Image Acquistition Object - Image Process
'
Set WshShell = WScript.CreateObject("Wscript.Shell")				'	Create Windows Script Host Object
Set FSO = CreateObject("Scripting.FileSystemObject")				'	Create Windows File System Object
'
Set Args = WScript.Arguments							'	Put Each Dropped File From The WScript Arguments Into An Array
'
	StrMyDesktop = WshShell.SpecialFolders(DeskDir)				'	Get The Path Of The Desktop
'
	JPGDir = (StrMyDesktop & DIR_JPEG)					'	Build The Path Of The Output Directory
'
For iCounter = 0 To Args.Count -1						'	Loop Through Each Dropped Image File That Has Been Dropped
'
If Args(iCounter) <> VbNullString Then						'	Validate Each Argument
'
If FSO.FolderExists(JPGDir) Then						'	Output Directory Already Exists
'
Call ConvertImages(Args(iCounter), IMGQUAL)					'	Start Processing Images
'
Else										'	Output Directory Doesn't Exist, So Create It
'
Set JPGFolder = FSO.CreateFolder(StrMyDesktop & DIR_JPEG)			'	Create Output Directory
'
	WScript.Sleep 1000							'	Allow The System Som Time To Create The Folder
'
Call ConvertImages(Args(iCounter), IMGQUAL)
'
End If
'
Else
'
	WScript.Quit	
'
End If
'
Next
'
Public Sub ConvertImages(ByRef ImagePath, ByVal ImageQuality)			'	Accept Images With The Path And Quality
On Error Resume Next
'
Dim ImageName									'	Routine Dimentions
Dim NewJPG
Dim FPOS
Dim LPOS
Dim FirstTrim
'
	IMG.LoadFile ImagePath							'	Put Each dropped Image Into The IMG Variable
'
If InStrRev((ImagePath),".", -1, 1) Then					'	Extract Only The Name Of The Image From The Image Path
'
	LPOS = instrRev((ImagePath),".", -1, 1)
'
	FirstTrim = Mid((ImagePath),1, (LPOS)-1)
'
	FPOS = InStrRev((FirstTrim),"\", -1, 1) 
'
	ImageName = Mid((FirstTrim), (FPOS)+1, Len(FirstTrim))
'
	NewJPG = (JPGDir & ImageName & ImageExt)				'	Set The Destination Of The New JPG File To The New Output Dir With A JPG Extention
'
End If
'
If IMG.FormatID <> wiaFormatJPEG Then						'	Check The Image Current Extention
'
While IP.Filters.Count > 0							'	Remove All Previous Filters From The Process
'
	IP.Filters.Remove 1
'
Wend
'
	IP.Filters.Add IP.FilterInfos("Convert").FilterID			'	Add A New Filter
'
With IP.Filters(1)
'
	.Properties("FormatID").Value = wiaFormatJPEG				'	Apply The New Filters
	.Properties("Quality").Value = IMGQUAL					'	Set Compression 
'
End With
'
Set IMG = IP.Apply(IMG)								'	Save The Images
'
	IMG.SaveFile NewJPG
'
End If
'
Exit Sub
'
End Sub										'	Clean Up.
'
Set IMG 	= Nothing
Set IP	 	= Nothing
'
Set WshShell 	= Nothing
Set FSO 	= Nothing
'
Set Args 	= Nothing
'
'
'


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.