Change the color of an image to black and white. This VBA shells to Irfanview (freeware image editor) and converts an image by specifying bpp (bits per pixel) to be only 1, which means just 2 colors (black or white), then saves the converted file with a different name.
To resize an image using Irfanview, see Irfanview_ImageToBW,
Alternately, look at code using the Windows Image Acquisition (WIA) library, resize an image using WIA by Geoff Griffith.
Go from this picture
to this:
or
... on paper -- notice the background isn't white! to this:
To create the drawing, you can use a pen, but a marker has better saturation. Light pen and pencil strokes may disappear if they aren't heavy enough.sCommand can be something like
sCommand = sPathFileIrfanview _
& " " & sPathFileImageSource _
& " /bpp=1" _
& " /convert=" & sPathFileImageTarget
Even though VBA is controlling everything (you can use Access, Excel, Word, PowerPoint, or other Office application), first, install Irfanview so you can use its features. Watch each dialog box of the installation and write down the path and filename of the executable, when it is displayed, so you can put it into the VBA code (you can also get the path\file when it is done if you know how to find it). My path\file is
In the code, customize sPathFileIrfanview, sPathFileImageSource, and sPathFileImageTarget for your path\filenames.
Look up the switches you want and customize sCommand.
Shell runs an external program. The command specifies the program to run (Irfanview), the file to open (the image you want to change), the switches to use (what you want to do), and saves the converted file with a new name.
If the Shell command was successful, you wouldn't know since Irfanview doesn't seem to return the error, so you will still get a message box at the end indicating that the change was made.
Then the folder with the converted file opens using FollowHyperlink.
Compile and save the code, before and after you customize it. When prompted for the module name, if you don't have your own name, use this so the module name is logical and not the same as any procedure name:
You can use this logic in a loop that looks at all the files in a folder, or just files of a specific type. For VBA code to loop through files, see VBA > File > Loop and Rename
Irfanview appears to create a path if it can, so you can specify a folder that doesn't yet exist. Otherwise, here is code to Make a Path
There aren't any parameters, but you would want to modify this and add them, to replace the custom information, once you test it and see how it works. You'd probably also want to make the switches a passed parameter. And customize, or comment, the message box at the end, and comment opening the folder if you are looping.
' Module Name: mod_Irfanview_ImageToBW '*************** Code Start ***************************************************** ' Purpose : Use Irfanview to change image to Black&White. Save with different file name ' Author : crystal (strive4peace) ' License : below code ' Code List: www.MsAccessGurus.com/code.htm '--------------------------------------------------------------------------------' Irfanview_ImageToBW
'--------------------------------------------------------------------------------' Public Sub Irfanview_ImageToBW() '190320 strive4peace ' download Irfanview, freeware image editor http://www.irfanview.com ' CHANGE an Image to Black & White 'if there is an error, go to the handler On Error GoTo Proc_Err 'dimension variables Dim sCommand As String _ , sPathFileIrfanview As String _ , sPathFileImageSource As String _ , sPathFileImageTarget As String _ , sMsg As String 'initialize variables ' ----------------------------------------- customize sPathFileIrfanview = "C:\Program Files (x86)\IrfanView\i_view32.exe" sPathFileImageSource = "c:\path\image_name.png" sPathFileImageTarget = "c:\path\image_name_BW.png" ' ----------------------------------------- 'specify Command for Shell: ' path and filename for Irfanview executable, ' path and filename of image to convert, ' switches: ' bpp (BitsPerPixel) = 1 -- color is either black or white ... on or off ' convert = save converted file in another location instead of over-writing sCommand = sPathFileIrfanview _ & " " & sPathFileImageSource _ & " /bpp=1" _ & " /convert=" & sPathFileImageTarget ' ----customize 'Shell to Irfanview and hide process Irfanview_ImageToBW = Shell(sCommand, vbHide) 'message to user sMsg="Created " & sPathFileImageTarget MsgBox sMsg,,"Done" 'open path Application.FollowHyperlink Left(sPathFileImageTarget, InStrRev(sPathFileImageTarget, "\")) Proc_Exit: On Error Resume Next Exit Function Proc_Err: MsgBox Err.Description _ , , "ERROR " & Err.Number _ & " Irfanview_ImageToBW" Resume Proc_Exit Resume End Function ' ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** Code End *******************************************************
One of the all-time brilliant minds, and great Access developer, is Ben Clothier. He was commemorated by other Access MVPs with a special proclamation at the annual Microsoft MVP Summit in March 2019. Several others who couldn't be there also want to add their names ... so this is to help incorporate other names, if that is to be done ... maybe another page?.
Irfanview has a transparency switch but I couldn't get it to work right, even making one change at a time. Oh well, changing a white background to Transparent is easily done with Snagit. Be sure to save the file as PNG, if it is not, so transparency information will be saved.
You can also use this code to make black and white versions of pictures
And pictures with less colors ... Irfanview bpp can be 1,4, 8, or 24. Here is the cat image with bpp=4, which is 4 bits, or 2^4 = 16 colors.
VBA > Image > Resize Image Irfanview
VBA > Image > Resize Image Irfanview
Help: Application.FollowHyperlink method (Access)
Frequently Asked Questions about IrfanView
here's the link to copy:
http://msaccessgurus.com/VBA/Code/Irfanview_Image_toBW.htm
It is interesting to hear from you. Was something not clear? Did you find a bug? Is an explanation wrong or not sufficient? Do you want the code do more? (there is always more)
Some of you write to say thanks and share what you're doing with Access ... nice to hear from you! It is my hope that you build great applications with Access, design your database structure well, link to and share with data in other formats, and use other Office applications such as Excel, Word, and PowerPoint, ... take advantage of built-in abilities, use strengths of each product, and manage your information wisely.
Are you a developer? Do you want to share? Email to ask about getting your pages added to the code index.
When we communicate, collaborate, and appreciate, we all get better.
Thank you.
Email me at info@msAccessGurus