我正在尝试使用命令行程序转换将PDF转换为图像(JPEG或PNG)。这是我正在转换的pdf文件之一。
我想让程序去掉多余的空白,并返回足够高质量的图像,以便上标可以轻松读取。
这是我目前最好的尝试。正如你所看到的,修剪工作很好,我只是需要锐化的分辨率相当多。这是我正在使用的命令:
convert -trim 24.pdf -resize 500% -quality 100 -sharpen 0x1.0 24-11.jpg
我试着做了以下有意识的决定:
调整它的大小(对分辨率没有影响)
尽可能提高质量
使用-锐化(我已经尝试了一系列值)
任何建议,请在最终的PNG/JPEG图像的分辨率更高,将非常感谢!
这里的Linux用户:我尝试了转换命令行实用程序(PDF到PNG),我对结果不满意。我发现这样做更简单,效果也更好:
使用PDFTK提取PDF页面
例如:PDFTK file.pdf cat 3输出page3.pdf
用GIMP打开(导入)该pdf
重要:改变导入分辨率从100到300或600像素/英寸
在GIMP导出为PNG(更改文件扩展名为。PNG)
编辑:
根据评论的要求,添加了图片。转换命令使用:
Convert -density 300 -trim struct2vec.pdf -quality 100 struct2vec.png
GIMP: 300dpi进口(px/in);导出为PNG压缩级别3。
我没有在命令行上使用GIMP(回复:我的评论,在下面)。
从Pdf中获取图像在iOS Swift最佳解决方案
func imageFromPdf(pdfUrl : URL,atIndex index : Int, closure:@escaping((UIImage)->Void)){
autoreleasepool {
// Instantiate a `CGPDFDocument` from the PDF file's URL.
guard let document = PDFDocument(url: pdfUrl) else { return }
// Get the first page of the PDF document.
guard let page = document.page(at: index) else { return }
// Fetch the page rect for the page we want to render.
let pageRect = page.bounds(for: .mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let img = renderer.image { ctx in
// Set and fill the background color.
UIColor.white.set()
ctx.fill(CGRect(x: 0, y: 0, width: pageRect.width, height: pageRect.height))
// Translate the context so that we only draw the `cropRect`.
ctx.cgContext.translateBy(x: -pageRect.origin.x, y: pageRect.size.height - pageRect.origin.y)
// Flip the context vertically because the Core Graphics coordinate system starts from the bottom.
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
// Draw the PDF page.
page.draw(with: .mediaBox, to: ctx.cgContext)
}
closure(img)
}
}
/ /使用
let pdfUrl = URL(fileURLWithPath: "PDF URL")
self.imageFromPdf2(pdfUrl: pdfUrl, atIndex: 0) { imageIS in
}
看来以下方法是有效的:
convert \
-verbose \
-density 150 \
-trim \
test.pdf \
-quality 100 \
-flatten \
-sharpen 0x1.0 \
24-18.jpg
它的结果是左图。将其与我的原始命令的结果进行比较(右侧的图像):
(为了真正看到和欣赏两者之间的差异,右键单击每个,并选择“在新选项卡中打开图像…”)
还要记住以下事实:
右边最糟糕的模糊图像的文件大小为1.941.702字节(1.85 MByte)。
它的分辨率是3060x3960像素,使用16位RGB颜色空间。
左边图像比较清晰,文件大小为337.879字节(330 kByte)。
它的分辨率是758x996像素,使用8位灰色空间。
所以,不需要调整大小;添加-density标志。密度值150很奇怪——尝试一个范围的值会导致图像在两个方向上看起来都很糟糕!
我真的没有很好的转换成功[更新2020年5月:实际上:它几乎从来没有为我工作],但我有非常好的pdftoppm成功。下面是几个从PDF生成高质量图像的例子:
[Produces ~25 MB-sized files per pg] Output uncompressed .tif file format at 300 DPI into a folder called "images", with files being named pg-1.tif, pg-2.tif, pg-3.tif, etc:
mkdir -p images && pdftoppm -tiff -r 300 mypdf.pdf images/pg
[Produces ~1MB-sized files per pg] Output in .jpg format at 300 DPI:
mkdir -p images && pdftoppm -jpeg -r 300 mypdf.pdf images/pg
[Produces ~2MB-sized files per pg] Output in .jpg format at highest quality (least compression) and still at 300 DPI:
mkdir -p images && pdftoppm -jpeg -jpegopt quality=100 -r 300 mypdf.pdf images/pg
要了解更多解释、选项和示例,请参阅我的完整答案:
https://askubuntu.com/questions/150100/extracting-embedded-images-from-a-pdf/1187844 1187844。
相关:
[如何将PDF转换为可搜索的PDF w/pdf2searchablepdf] https://askubuntu.com/questions/473843/how-to-turn-a-pdf-into-a-text-searchable-pdf/1187881#1187881
交联:
如何将一个PDF转换成JPG与命令行在Linux?
https://unix.stackexchange.com/questions/11835/pdf-to-jpg-without-quality-loss-gscan2pdf/585574#585574