bash 将 .pdf 文件更改为 .png Mac OS 10.3 的 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6349984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:10:24  来源:igfitidea点击:

shell script to change .pdf files to .png Mac OS 10.3

imagemacosbashscripting

提问by Dave2081

I'd like to make a script on Mac OS 10.3 that converts a pdf to a png. I've been looking around but I'm not sure if its even possible. I keep reading about a "sips" command but it doesn't seem to be available on 10.3, or at least this one. I typed man sips in the terminal and nothing came out. I have written a couple bash scripts and kind of understand how they work but was hoping this specific issue at work would be a good way to practice. I could probably write something that changes the name, but I'm not sure that would work in and of itself as the new png file would probably not actually work. Is there a way from the terminal to basically open preview, open each file and then save as a png? that would probably be the best option. Thanks

我想在 Mac OS 10.3 上制作一个脚本,将 pdf 转换为 png。我一直在环顾四周,但我不确定它是否可能。我一直在阅读有关“sips”命令的信息,但它似乎在 10.3 上不可用,或者至少在这个命令上不可用。我在终端中输入 man sips 并没有任何输出。我已经写了几个 bash 脚本并且有点了解它们是如何工作的,但希望这个工作中的特定问题是一个很好的练习方式。我可能会写一些改变名称的东西,但我不确定这是否会起作用,因为新的 png 文件可能实际上不起作用。有没有办法从终端基本上打开预览,打开每个文件然后另存为 png?那可能是最好的选择。谢谢

回答by Matthew Slattery

I don't have a suitable system to test on, but I thinksipsappeared in 10.3. (It's definitely in 10.4.)

我没有合适的系统来测试,但我认为sips出现在 10.3 中。(它肯定在 10.4 中。)

Just because there is no man page doesn't mean that it isn't there (try sips -hor ls /usr/bin/sips).

仅仅因为没有手册页并不意味着它不存在(尝试sips -hls /usr/bin/sips)。

If it's there, Sorpigal's answer (+1) is good for the basic scripting, but replace

如果它在那里,Sorpigal 的答案 (+1) 对基本脚本编写有好处,但替换

convert "$pdf" "${pdf%%.*}.png"

with

sips -s format png --out "${pdf%%.*}.png" "$pdf"

回答by sorpigal

ImageMagickcan do this

ImageMagick可以做到这一点

convert "your file.pdf" "output file.png"

You will have to install it since it's not available by default.

您必须安装它,因为它默认不可用。

If you want to convert a large number of files, add a for loop

如果要转换大量文件,加一个for循环

cd /some/directry/with/pdfs
shopt -s nullglob
for pdf in *{pdf,PDF} ; do
    convert "$pdf" "${pdf%%.*}.png"
done

Which will create equivalently named PNG files for each PDF with a .pdf or .PDF extension in that directory.

这将为该目录中具有 .pdf 或 .PDF 扩展名的每个 PDF 创建等效命名的 PNG 文件。

回答by jm666

10.3 is somewhat old, so can't try solutions.

10.3有点旧,所以不能尝试解决方案。

But here is a tutorial http://www.mactech.com/articles/mactech/Vol.21/21.03/BasicImageManipulation/index.htmlhow to convert via applescript and built-in Apple ImageEvents (what comes with 10.3). Simply try change the jpeg/tiff to pdf/png.

但这里有一个教程http://www.mactech.com/articles/mactech/Vol.21/21.03/BasicImageManipulation/index.html如何通过 applescript 和内置的 Apple ImageEvents(10.3 附带的内容)进行转换。只需尝试将 jpeg/tiff 更改为 pdf/png。

回答by KyungHoon Kim

Use the following shell script.

使用以下 shell 脚本。

  1. Make bash file e.g., test.bash
  1. 制作 bash 文件,例如, test.bash

for i in *.pdf; do name=$i; name=${name%.*}; sips -s format png $i --out ${name}.png; done

for i in *.pdf; do name=$i; name=${name%.*}; sips -s format png $i --out ${name}.png; done

  1. Enter the command
  1. 输入命令

bash test.bash

bash test.bash