The following AppleScript/Objective-C solution works on Yosemite through High Sierra. Folder icons are 1024x1024 at 144 DPI. Unless your icon is exactly that size, or a .icns with background transparency, the folder icon may appear larger, smaller, ideal, or somewhat distorted.
Here is the result of using this application to set a folder to a 512x512 .icns icon (first two, and an image). If you don't like the result, perform a Get Info on the folder, and click on the top most icon to select it, and then just backspace.
Copy the entire code from the scrollable window below into Launchpad : Other : Script Editor. Click the compile (hammer icon), and run it. You can save it as a script, script bundle, or application.
property isImage : {"public.image", "com.adobe.pdf"}
property adesktop : (path to desktop as text) as alias
property apictures : (path to pictures folder as text) as alias
use framework "Cocoa"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions
set theFolder to POSIX path of (choose folder default location adesktop without invisibles, multiple selections allowed and showing package contents)
set theImg to POSIX path of (choose file of type isImage default location apictures without invisibles, multiple selections allowed and showing package contents)
if my setIcon(theFolder, theImg) = true then
display dialog "Folder icon has been set"
else
display dialog "Folder icon was not set"
end if
return
on setIcon(theFolder, theImage)
set img to current application's NSImage's alloc()'s initWithContentsOfFile:theImage
return ((current application's NSWorkspace's sharedWorkspace)'s setIcon:img forFile:theFolder options:0)
end setIcon