@
chutsetien 顺附自写的该 batch 档的用法解释:
A tiny little Windows Batch script used for the cropping, scaling, and format conversion of images. It requires the following software to be properly set up in %PATH% (here in my case is C:\_c\):
ffmpeg;
libwebp;
optipng; and
exiftool.
Understanding ‘orientation-adaptive’ and ‘orientation-irrelevant’
As the script is used for the cropping and scaling of images, it would require the clarification of two concepts: ‘orientation-adaptive’ and ‘orientation-irrelevant’.
The former, as its name indicates, takes the image’s orientation into account. This can be easily understood in the context of scaling, as we often do need to scale an image by its ‘long edge’ or ‘short edge’, regardless of whether it is the width or the height.
It might get a little bit confusing when it comes to cropping. In cropping, by saying ‘orientation-adaptive’, it means that when given a ratio, the long edge always takes the first number, while the short edge takes the second. For example, if we have a horizontal image (which means its width is greater than its height), and we want to crop it to 4:3 orientation-adaptively, then the long edge, the width, will take the ‘4’ while the short edge, the height, will take the ‘3’. And when done, it would indeed be an image of 4:3. But if we have a vertical image (which means the height is the longer edge), and we still want to orientation-adaptively crop it to 4:3, then the height of the image, now the long edge, shall take the ‘4’, while the width of the image will take the ‘3’, and the final result we get is actually an image of 3:4.
What makes the ‘orientation-adaptive cropping’ more confusing is that it can be used to crop ‘against’ the image’s orientation by setting a ratio smaller than 1, in that it can force the long edge to take the smaller number in the ratio to become the new short edge while the original short edge can take the larger number and becomes the new long edge.
The ‘orientation-irrelevant’ scaling or cropping is much easier to understand—as its name indicates, it does not take the image’s orientation into account, only focusing on scaling or cropping based on the width and height, regardless of which one is longer.
Cropping switches
The script accepts switches used for cropping in an order of precedence as follows—if more than one switch is detected in one operation, a switch with a higher rank surpasses the lower ones:
-sq: crops the image to a 1:1 square;
-gr: orientation-adaptively crops the image to the golden ratio;
-gx: orientation-irrelevantly crops the image to the golden ratio by setting its width as the long edge;
-gy: orientation-irrelevantly crops the image to the golden ratio by setting its height as the long edge;
-cr x y: orientation-adaptively crops the image to a set ratio of x:y, where the long edge of the image takes the x, and the short edge takes the y;
-cx x y: orientation-irrelevantly crops the image to a set ratio of x:y, where the width of the image takes the x, and the height takes the y; and
-rg: performs a ‘reversed’ orientation-adaptive golden ratio cropping, where the long edge of the image takes the smaller number of the golden ratio, and the short edge takes the larger (not very useful actually, and that’s why it gets the lowest precedence).
Scaling switches
The script accepts switches used for scaling in an order of precedence as follows—if more than one switch is detected in one operation, a switch with a higher rank surpasses the lower ones:
-sl x: proportionally scales the image by setting its long edge to x;
-ss x: proportionally scales the image by setting its short edge to x;
-sx x: proportionally scales the image by setting its width to x;
-sy x: proportionally scales the image by setting its height to x;
-sf x y: proportionally scales the image by a given fraction of x/y; and
-sp x: proportionally scales the image by a given percentage x (‘%’ is not needed when giving the percentage number).
The script, by default, prohibits upscaling, unless the user explicitly orders the script to do so by changing the first letter of these scaling switches to ‘u’ (-ul, -us, -ux, etc). I strongly dissuade anyone who intends to use this script to upscale any image, for that image upscaling simply shall not be done in this way as it needs much more complicated algorithms and should be done with dedicated manual tuning.
Output file format and quality switches
The script accepts switches -p, -j, and -w to indicate an output of a .png image, a .jpg image, or a .webp image. These switches can be used simultaneously to output more than one image. For -j and -w, there are four quality suffixes, hq (‘highest quality’), mq (‘medium quality’), lq (‘low quality’), and pq (‘poor quality’), each representing a fixed quality parameter (or a group of fixed quality-related parameters in the case of -w), that can be used by immediately appending to the letter j or q, with no spaces in between, to form a five-tier image quality levels (together with the two default ones of no sticking letters, the -j and -w, which are both ranked second best in the hierarchy) for the user to choose. And by knowing me, you can rest assured that even by using -jpq or -wpq, the output will still be acceptable. But since I use ffmpeg to encode JPEG files—which uses mjpeg as its encoder—the output files will then always be added with a ‘dash’ label in the names, indicating that they are intended for quick sharing uses, and are not expected to be used in serious scenarios. If -p is detected, then the output .png file will be compressed by optipng with maximum tries, which means it will take a considerably long period of time if the image is large. Lastly, all of the output files will be cleaned of EXIF information in the end.
Designating output filename and path
By default, the output file or files will be saved in the same location as the input file, using the original filename as the basename with text labels of the process or processes being done to it and of the image quality parameter appended in the end. And yet the output filename and path are designatable with the switches listed below:
-op "path\to\the\designated location\": if used, the output files will then be exported to the designated location;
-ob new_basename: if used, then the given string will surpass the file’s original name and be used as the designated basename—the text labels for processed and image quality will still be appended to the given basename; and
-on "new filename": if used, then the given string will be used as the full filename of the output file, with no more appendings.
Since one operation of the script can produce more than one file in different formats, strings given after -ob and -on shall not contain a file extension, as it will be added automatically according to the output format.