我正在尝试使用命令行程序转换将PDF转换为图像(JPEG或PNG)。这是我正在转换的pdf文件之一。

我想让程序去掉多余的空白,并返回足够高质量的图像,以便上标可以轻松读取。

这是我目前最好的尝试。正如你所看到的,修剪工作很好,我只是需要锐化的分辨率相当多。这是我正在使用的命令:

convert -trim 24.pdf -resize 500% -quality 100 -sharpen 0x1.0 24-11.jpg

我试着做了以下有意识的决定:

调整它的大小(对分辨率没有影响) 尽可能提高质量 使用-锐化(我已经尝试了一系列值)

任何建议,请在最终的PNG/JPEG图像的分辨率更高,将非常感谢!


当前回答

你可以在LibreOffice Draw(这通常是预安装在Ubuntu中)中完成:

在LibreOffice Draw中打开PDF文件。 滚动到需要的页面。 确保文本/图像元素被正确放置。如果没有,可以在页面上进行调整/编辑。 顶部菜单:文件>导出… 在右下角菜单中选择所需的图像格式。我推荐PNG。 命名文件并单击Save。 选项窗口将出现,因此您可以调整分辨率和大小。 单击OK,就完成了。

其他回答

下面的python脚本可以在任何Mac (Snow Leopard及以上版本)上运行。它可以在命令行上使用连续的PDF文件作为参数,或者您可以在Automator中放入一个运行Shell脚本操作,并创建一个服务(Mojave中的快速动作)。

您可以在脚本中设置输出图像的分辨率。

脚本和Quick Action可以从github下载。

#!/usr/bin/python
# coding: utf-8

import os, sys
import Quartz as Quartz
from LaunchServices import (kUTTypeJPEG, kUTTypeTIFF, kUTTypePNG, kCFAllocatorDefault) 

resolution = 300.0 #dpi
scale = resolution/72.0

cs = Quartz.CGColorSpaceCreateWithName(Quartz.kCGColorSpaceSRGB)
whiteColor = Quartz.CGColorCreate(cs, (1, 1, 1, 1))
# Options: kCGImageAlphaNoneSkipLast (no trans), kCGImageAlphaPremultipliedLast 
transparency = Quartz.kCGImageAlphaNoneSkipLast

#Save image to file
def writeImage (image, url, type, options):
    destination = Quartz.CGImageDestinationCreateWithURL(url, type, 1, None)
    Quartz.CGImageDestinationAddImage(destination, image, options)
    Quartz.CGImageDestinationFinalize(destination)
    return

def getFilename(filepath):
    i=0
    newName = filepath
    while os.path.exists(newName):
        i += 1
        newName = filepath + " %02d"%i
    return newName

if __name__ == '__main__':

    for filename in sys.argv[1:]:
        pdf = Quartz.CGPDFDocumentCreateWithProvider(Quartz.CGDataProviderCreateWithFilename(filename))
        numPages = Quartz.CGPDFDocumentGetNumberOfPages(pdf)
        shortName = os.path.splitext(filename)[0]
        prefix = os.path.splitext(os.path.basename(filename))[0]
        folderName = getFilename(shortName)
        try:
            os.mkdir(folderName)
        except:
            print "Can't create directory '%s'"%(folderName)
            sys.exit()

        # For each page, create a file
        for i in range (1, numPages+1):
            page = Quartz.CGPDFDocumentGetPage(pdf, i)
            if page:
        #Get mediabox
                mediaBox = Quartz.CGPDFPageGetBoxRect(page, Quartz.kCGPDFMediaBox)
                x = Quartz.CGRectGetWidth(mediaBox)
                y = Quartz.CGRectGetHeight(mediaBox)
                x *= scale
                y *= scale
                r = Quartz.CGRectMake(0,0,x, y)
        # Create a Bitmap Context, draw a white background and add the PDF
                writeContext = Quartz.CGBitmapContextCreate(None, int(x), int(y), 8, 0, cs, transparency)
                Quartz.CGContextSaveGState (writeContext)
                Quartz.CGContextScaleCTM(writeContext, scale,scale)
                Quartz.CGContextSetFillColorWithColor(writeContext, whiteColor)
                Quartz.CGContextFillRect(writeContext, r)
                Quartz.CGContextDrawPDFPage(writeContext, page)
                Quartz.CGContextRestoreGState(writeContext)
        # Convert to an "Image"
                image = Quartz.CGBitmapContextCreateImage(writeContext) 
        # Create unique filename per page
                outFile = folderName +"/" + prefix + " %03d.png"%i
                url = Quartz.CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, outFile, len(outFile), False)
        # kUTTypeJPEG, kUTTypeTIFF, kUTTypePNG
                type = kUTTypePNG
        # See the full range of image properties on Apple's developer pages.
                options = {
                    Quartz.kCGImagePropertyDPIHeight: resolution,
                    Quartz.kCGImagePropertyDPIWidth: resolution
                    }
                writeImage (image, url, type, options)
                del page

这里的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(回复:我的评论,在下面)。

我真的没有很好的转换成功[更新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

看来以下方法是有效的:

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很奇怪——尝试一个范围的值会导致图像在两个方向上看起来都很糟糕!

你附上的PNG文件看起来真的很模糊。如果您需要对生成为PDF预览的每张图像使用额外的后处理,则会降低解决方案的性能。

2JPEG可以将PDF文件转换为一个漂亮的锐化JPG文件,并在一次调用中裁剪空边距:

2jpeg.exe -src "C:\In\*.*" -dst "C:\Out" -oper Crop method:autocrop