Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VBS to call Photoshop to generate thumbnails in batch

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use VBS to call Photoshop to generate thumbnails in batches. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Imitate the Tencent news page, and add a VOD code for pictures on the news page to KingCms. The required image VOD format is as follows:

Title # https://cache.yisu.com/upload/information/20210311/308/13725.jpg@@@/small/123.gif@@@8 title one * * https://cache.yisu.com/upload/information/20210311/308/13726.jpg@@@/small/456.gif@@@ title two * https://cache.yisu.com/upload/information/20210311/308/13728.jpg@@@/small/789.gif@@@ title three

The format is explained as follows:

0 means picture-on-demand appears on page 0

Https://cache.yisu.com/upload/information/20210311/308/13725.jpg is the address of the first image. / small/123.gif is the address of the first thumbnail. The name of the original image is the same as that of the thumbnail, but the suffix is different. The original image is jpg and the thumbnail is gif. Title one is the caption of the first picture.

The second and third pictures are in the same format as the first picture.

#, @ @, and * * are the corresponding delimiters.

-Segmentation line-

At first, I wrote these picture formats by hand, and found that it was very inefficient. I only published two pieces of news in one afternoon and wrote the corresponding VBS script.

Script 1: collect news pictures and generate the corresponding picture format code

Directory = "original picture"

Directory = CreateObject ("Scripting.FileSystemObject"). GetFolder ("."). Path & "\" & Directory & "\"

Call DeleteFiles (Directory)

StrUrl = InputBox ("Please enter URL:")

If strUrl "" Then

Call getImages (strUrl)

End If

Function getImages (strUrl)

Set ie = WScript.CreateObject ("InternetExplorer.Application")

Ie.visible = True

Ie.navigate strUrl

Do

Wscript.Sleep 500

Loop Until ie.ReadyState=4

Set objImgs = ie.document.getElementById ("fontzoom") .getElementsByTagName ("img")

StrTitles = InputBox ("Please enter picture words:")

ArrTitles = Split (strTitles, "")

StrCode = "0,000,000 #"

For iTunes 0 To objImgs.length-1

If I > 0 Then strCode = strCode + "* *"

SmallPic = Replace (Mid (objImgs (I). Src, InStrRev (objImgs (I). Src, "/") + 1), "jpg", "gif")

StrCode = strCode + objImgs (I) .src + "@ @ / small/" + smallPic + "@ @" + arrTitles (I)

SaveRemoteFile objImgs (I) .src

Next

Ie.Quit

InputBox "Please copy the result:", strCode

End Function

Sub SaveRemoteFile (RemoteFileUrl)

LocalFile = Directory & Mid (RemoteFileUrl, InStrRev (RemoteFileUrl, "/") + 1)

Set xmlhttp = CreateObject ("Microsoft.XMLHTTP")

With xmlhttp

.Open "Get", RemoteFileUrl, False, ","

.Send

GetRemoteData = .ResponseBody

End With

Set xmlhttp = Nothing

Set Ads = CreateObject ("Adodb.Stream")

With Ads

.Type = 1

.Open

.Write GetRemoteData

.SaveToFile LocalFile, 2

.Cancel ()

.Close ()

End With

Set Ads=nothing

End Sub

Function DeleteFiles (strFolder)

Set objFSO = CreateObject ("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder (strFolder)

Set objFiles = objFolder.Files

For Each objFile in objFiles

ObjFile.Delete

Next

Set objFSO = Nothing

End Function

Script 2: call Photoshop to generate thumbnails in batch

Directory = "original image" 'folder of the original image

NewDirectory = "Thumbnail" 'folder where thumbnails are saved

Const psDoNotSaveChanges = 2

Const PsExtensionType_psLowercase = 2

Const psDisplayNoDialogs = 3

Const psLocalSelective = 7

Const psBlackWhite = 2

Const psNoDither = 1

LimitHeight = 58 'maximum height

ImgResolution = 72 'resolution

Call DeleteFiles (NewDirectory)

Call Convert2Gif (Directory)

Function ReSizeImg (doc)

RsHeight = doc.height

Scale = 1. 0

If rsHeight > limitHeight Then

Scale = limitHeight / (doc.height + 0.0)

RsWidth = doc.width * Scale

RsHeight = doc.height * Scale

End If

Doc.resizeImage rsWidth, rsHeight, ImgResolution, 3

End Function

Function Convert2Gif (Directory)

Set app = CreateObject ("Photoshop.Application")

App.bringToFront ()

App.preferences.rulerUnits = 1 'psPixels

App.DisplayDialogs = psDisplayNoDialogs

Set gifOpt = CreateObject ("Photoshop.GIFSaveOptions")

With gifOpt

.palette = psLocalSelective

.colors = 256

.forced = psBlackWhite

.Transparency = False

.Dither = psNoDither

.interlaced = False

End With

Set fso = CreateObject ("Scripting.FileSystemObject")

If Not fso.FolderExists (Directory) Then

MsgBox "Photo Directory NOT Exists."

Exit Function

End If

Set objFiles = fso.GetFolder (Directory) .Files

NewDirectory = fso.GetFolder (".") Path & "\" & NewDirectory & "\"

For Each objFile In objFiles

If Split (objFile.Name, ".") (1) "db" Then

Set doc = app.Open (objFile.Path)

Set app.ActiveDocument = doc

ReSizeImg (doc)

Doc.SaveAs NewDirectory & Split (objFile.Name, ".") (0) & ".gif", gifOpt, True, PsExtensionType_psLowercase

Call doc.Close (psDoNotSaveChanges)

Set doc = Nothing

End If

Next

Set app = Nothing

End Function

Function DeleteFiles (strFolder)

Set objFSO = CreateObject ("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder (strFolder)

Set objFiles = objFolder.Files

For Each objFile in objFiles

ObjFile.Delete

Next

Set objFSO = Nothing

End Function

By comparison, gif thumbnails are the smallest, so they are gif thumbnails. For VBS to call Photoshop, there are instructions in the Photoshop's C:\ Program Files\ Adobe\ Adobe Photoshop CS4\ Scripting\ Documents directory, and sample code in the C:\ Program Files\ Adobe\ Adobe Photoshop CS4\ Scripting\ Sample Scripts directory. If you want to generate png thumbnails, you can refer to the documentation to modify the corresponding code of the script:

Set pngOpt = CreateObject ("Photoshop.PNGSaveOptions")

With pngOpt

.interlaced = False

End With

At first, the intention is to call Set Jpeg = CreateObject ("Persits.Jpeg") to generate thumbnails. The advantage is that you don't have to load a huge Photoshop. The speed of generating thumbnails is very fast, but the quality of the thumbnails is worse than that of Photoshop, so you give up.

The original plan is not to save the original image, directly open the network picture, and then directly generate thumbnails to the local. Although Photoshop can open network pictures, it is not good to call Photoshop in the script to open network pictures. You have to save the network pictures locally, and then generate thumbnails.

In fact, Photoshop has its own image batch processing function:

Window-> actions-> create a new action-> Open all the pictures you want to do in PS-> Select one of the pictures, resize and save as gif-> close your finished images-> stop playing / recording.

File-> automatic-> batch-> Action bar Select the name of the action you just created-> click "Select" under "Source" to select the folder where you want to process photos-> "Target" under "Select" another folder where you want to save thumbnails-> OK. Just OK!

However, compared with the program, the program is obviously much more flexible, and many batch effects can only be achieved by the program, so there is no recording action to generate thumbnails.

Generate the corresponding picture format code, or enter the following JS code in the address bar:

_ javascript:D=prompt ("Picture matching", ""); E=D.split (""); A=document.getElementById ("fontzoom"); B=A.getElementsByTagName ("img"); C = "0chromosome #"; for (iword 0trait i0) C = "*"; CleavC + B [I] .src.substring (BI] .src.substring ("/") + 1) .replace ("jpg", "gif") + "@ @" + E [I] } window.prompt ("copy", C); void (0)

Thank you for reading! This is the end of this article on "how to use VBS to call Photoshop to generate thumbnails in batches". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report