Original link: https://scottyeung.top/2023/better-script-with-fsharp/
During the graduation season, I went to take some photos with my classmates, and the photos shared later are in the HEIC format, which can be opened directly on the computer, but the uploaded photos in the iCloud album only support the jpg format, even if these photos are taken with the iPhone, it is not It cannot be uploaded directly, there is no way, only to manually convert the photo format once.
There are also many HEIC to JPG tool websites on the Internet, but they can only upload one by one, and then download one by one, which is obviously not a good solution for the situation where a large number of photos need to be processed. So I naturally planned to write a script to convert.
Of course, with ChatGPT now, you don’t need to write scripts yourself, just throw the problem to it, we just need to be the porter of the code.
It looks very good, copy it to the local, replace the path and run it, and found that the script cannot run normally, because the Pillow library used actually does not support HEIC format pictures. Continue to hand over the problem to it, and it pushes a pyheif library for me to deal with. However, this library requires MSVC-related toolchains to compile and install. The installation on my computer always fails. Later, I tried several dependent libraries recommended by it. , can not be installed, send! The low portability of Python’s cross-platform installation and construction has been severely taught, and it is too exaggerated to configure an environment or container just to write a script.
So I replaced it with F#, which I was learning recently, and continued to hand over the problem to ChatGPT. After copying and pasting the code back and forth and trying to run it a few times, the task was successfully completed.
#r "nuget:Magick.NET-Q16-AnyCPU" open System.IO open ImageMagick let convertHEICtoJPG (inputPath: string) (outputPath: string) = let image = new MagickImage(inputPath) image.Format <- MagickFormat.Jpg image.Write(outputPath) // 获取当前目录下的所有HEIC 文件let heicFiles = Directory.GetFiles(@"/path/to/you", "*.heic") // 遍历每个HEIC 文件,并进行转换for heicFile in heicFiles do let jpgFile = Path.ChangeExtension(heicFile, ".jpg") convertHEICtoJPG heicFile jpgFile printfn "转换完成:%s -> %s" heicFile jpgFile
Compared with Python, using F# to write scripts is actually simple and convenient enough. It can be written in a single fsx file, or you can directly copy the written code to the fsi interactive command line to execute the code piece by piece, which is very convenient. Especially for script code with external dependent libraries, using F# or C# scripts, such a line of code references dependent libraries, is much more useful than installing a dependency globally on the command line. (At least there will be no problems with installation failures)
This article is transferred from: https://scottyeung.top/2023/better-script-with-fsharp/
This site is only for collection, and the copyright belongs to the original author.