0

我已经阅读了很多试图描述如何将 PDF 转换为 PNG 图像的文章。但我根本无法让它工作。我试图import PythonMagick在我的脚本之上,但它返回错误ImportError: No module named PythonMagick

是否可以像通过 Homebrew 安装 shell 工具一样简单地安装 PythonMagick?!背景是我的 Python 脚本,它比等效的 Bash 脚本要短得多。唯一不起作用的是 PDF 到 PNG 的转换和最终图像的缩放。在 Bash 中,我为此使用 Imagemagick,但我也想在 Python 中执行此操作,因为它是单行的。

有任何想法吗?

编辑

代码可以在 Github 上找到:https ://github.com/Blackjacx/Scripts/blob/master/iconizer.py

找到解决方案

使用 MagickWand 效果更好,所以我正在使用它。要安装它,我做了:

$ brew install imagemagick@6
$ export MAGICK_HOME=/usr/local/opt/imagemagick@6
4

3 回答 3

1

我不建议您使用imagemagick. 因为此工具按像素而不是 pdf 文件中的矢量渲染输出。因此,如果您的 pdf 文件的原始分辨率远低于输出 png 文件的分辨率,则将是质量损失。

尝试使用mupdf. mudraw您应该使用的命令因版本而异。大多数时候应该是:

mudraw [-h 1080] [-w 1080] [-o <output_path>] <input_path> 

该工具可以操纵矢量,因此无论您如何缩放原始文件都不会造成任何质量损失。

于 2017-10-23T01:12:52.220 回答
1

试试这个错误 ImportError: No module named PythonMagick

也看看这个链接

尝试更改:从 . import _PythonMagickPythonMagickinit .py 中导入 _PythonMagick

于 2017-10-22T22:48:08.107 回答
0

您可以使用 Apple 自己的 CoreGraphics API 和 python 脚本,“开箱即用”。以下脚本会将作为参数提供的 PDF 文件转换为 PNG。它也可以用于 Automator 的“运行 Shell 脚本”动作。

#!/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
于 2019-02-27T10:26:52.373 回答