2017-12-01 11 views
3

enter image description hereどのように私はImageMagickのを使用して、高解像度のJPEGファイルにいくつかのPDFファイルを変換しようとしている

バイナリ文字列からワンドで高解像度のJPEGを作成します。私は10、64のpython 3.62 - 64ビットと0.4.4のwanで作業しています。コマンドラインで私は:

$ /e/ImageMagick-6.9.9-Q16-HDRI/convert.exe -density 400 myfile.pdf -scale 2000x1000 test3.jpg. 

私にとってはうまくいきます。 Pythonで

私は低解像度のJPEGファイルを与えている
from wand.image import Image 

file_path = os.path.dirname(os.path.abspath(__file__))+os.sep+"myfile.pdf" 

with Image(filename=file_path, resolution=400) as image: 
    image.save() 
    image_jpeg = image.convert('jpeg') 

。同じことをするには、これを私のワンドコードにどのように翻訳すればよいですか?

編集:

私はので、私が試したhttp://docs.wand-py.org/en/0.4.4/guide/read.html#read-blobに基づいて、問題は入力PDFは、バイナリ文字列としてImageオブジェクトに読み込まなければならないことであることに気づい:

with open(file_path,'rb') as f: 
    image_binary = f.read() 

f.close() 

with Image(blob=image_binary,resolution=400) as img: 
    img.transform('2000x1000', '100%') 
    img.make_blob('jpeg') 
    img.save(filename='out.jpg') 

をこの読み取りファイルはokですが、出力は10個のファイルに分割されています。どうして?私は1つの高解像度jpegにこれを取得する必要があります。

EDIT:

私は、OCR APIにJPEGを送信する必要があるので、私は、オブジェクトのような出力をファイルに書き込むことができれば、私は思っていました。 https://www.imagemagick.org/api/magick-image.php#MagickWriteImageFileを見て、私が試した:

emptyFile = Image(width=1500, height=2000) 

with Image(filename=file_path, resolution=400) as image: 

    library.MagickResetIterator(image.wand) 
    # Call C-API Append method. 
    resource_pointer = library.MagickAppendImages(image.wand, 
                True) 

    library.MagickWriteImagesFile(resource_pointer,emptyFile) 

これが与える:

File "E:/ENVS/r3/pdfminer.six/ocr_space.py", line 113, in <module> 
test_file = ocr_stream(filename='test4.jpg') 
File "E:/ENVS/r3/pdfminer.six/ocr_space.py", line 96, in ocr_stream 
library.MagickWriteImagesFile(resource_pointer,emptyFile) 
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type 

どのように私はこの作業を得ることができますか?

+0

正確な問題は何ですか?プログラムは何を出力しますか?何を出力したいですか? –

答えて

2

Why? I need to get this into 1 high res jpeg.

PDFには、「スタック」内の個々の画像を考慮するページが含まれています。 ライブラリでは、各ページで動作するようにwand.image.Image.sequanceが用意されています。

しかし、すべての画像を1つのJPEGに追加する。それぞれのページを繰り返すか、C-APIのメソッドMagickAppendImagesを呼び出してください。

from wand.image import Image 
from wand.api import library 
import ctypes 

# Map C-API not provided by wand library. 
library.MagickAppendImages.argtypes = [ctypes.c_void_p, ctypes.c_int] 
library.MagickAppendImages.restype = ctypes.c_void_p 

with Image(filename="path_to_document.pdf", resolution=400) as image: 
    # Do all your preprocessing first 
    # Ether word directly on the wand instance, or iterate over each page. 
    # ... 
    # To write all "pages" into a single image. 
    # Reset the stack iterator. 
    library.MagickResetIterator(image.wand)      
    # Call C-API Append method. 
    resource_pointer = library.MagickAppendImages(image.wand, 
                True)   
    # Write C resource directly to disk. 
    library.MagickWriteImages(resource_pointer,     
           "output.jpeg".encode("ASCII"), 
           False) 

更新:OpenCVのののpython APIを使用して、あなたを想定し

I need to send the jpeg to an OCR api ...

、あなただけの各ページを反復処理する必要がある、とnumpyのバッファを経由してOCRに画像ファイルデータを渡します。

from wand.image import Image 
import numpy 
import cv2 

def ocr_process(file_data_buffer): 
    """ Replace with whatever your OCR-API calls for """ 
    mat_instance = cv2.imdecode(file_data_buffer) 
    # ... work ... 

source_image="path_to_document.pdf" 
with Image(filename=source_image, resolution=400) as img: 
    for page in img.sequence: 
     file_buffer = numpy.asarray(bytearray(page.make_blob("JPEG")), 
            dtype=numpy.uint8) 
     ocr_process(file_buffer) 

so I was wondering if I could write the output to a file like object

異なるライブラリーから「画像」オブジェクト(または下線C構造)はパイソンを想定しないでお互いに匹敵します。

OCRのAPIを知らなくても、私は一部を過ぎてあなたを助けることはできませんが、私は次のよう...

  • 利用の一時中間ファイルのいずれかを提案することができます。 OCRのAPIがサポートしている場合は

    with Image(filename=INPUT_PATH) as img: 
        # work 
        img.save(filename=OUTPUT_PATH) 
    # OCR work on OUTPUT_PATH 
    
  • 使用したファイルディスクリプタ(I/O遅くなりますが、学ぶ/開発/デバッグが容易)。 (上記と同じ)

    with open(INPUT_PATH, 'rb') as fd: 
        with Image(file=fd) as img: 
         # work 
         # OCR work ??? 
    
  • ブロブを使用します。 (より高速なI/Oが、多くより多くのメモリを必要とする)

    buffer = None 
    with Image(filename=INPUT_PATH) as img: 
        # work 
        buffer = img.make_blob(FORMAT) 
    if buffer: 
        # OCR work ??? 
    

さらにアップデート

はソリューションがあるかもしれない、一緒にすべてのコメントをラッピング...

from wand.image import Image 
from wand.api import library 
import ctypes 
import requests 

# Map C-API not provided by wand library. 
library.MagickAppendImages.argtypes = [ctypes.c_void_p, ctypes.c_int] 
library.MagickAppendImages.restype = ctypes.c_void_p 

with Image(filename='path_to_document.pdf', resolution=400) as image: 
    # ... Do pre-processing ... 
    # Reset the stack iterator. 
    library.MagickResetIterator(image.wand) 
    # Call C-API Append method. 
    resource_pointer = library.MagickAppendImages(image.wand, True) 
    # Convert to JPEG. 
    library.MagickSetImageFormat(resource_pointer, b'JPEG') 
    # Create size sentinel. 
    length = ctypes.c_size_t() 
    # Write image blob to memory. 
    image_data_pointer = library.MagickGetImagesBlob(resource_pointer, 
                ctypes.byref(length)) 
    # Ensure success 
    if image_data_pointer and length.value: 
     # Create buffer from memory address 
     payload = ctypes.string_at(image_data_pointer, length.value) 
     # Define local filename. 
     payload_filename = 'my_hires_image.jpg' 
     # Post payload as multipart encoded image file with filename. 
     requests.post(THE_URL, files={'file': (payload_filename, payload)}) 
+0

ありがとう、私自身の理解のために:私はimageMagickプログラム自体がC言語で書かれていると仮定しています。ワンドはC APIにアクセスして、すべてではない機能の一部を提供しますか? – user61629

+0

正しい。 ImageMagickはC言語で書かれ、 'MagickWand'と呼ばれるC-APIライブラリを持っています。 Pythonパッケージ 'wand'は' ctypes'モジュールを通して 'MagickWand'と統合されています。 – emcconville

+0

ありがとう、それは動作します。もう1つ物を求めることができますか?上記の編集をご覧ください。 – user61629

2

どうのようなものについて:関連

with ok.resize(2000, 1000) 

ok = Image(filename=file_path, resolution=400) 
with ok.transform('2000x1000', '100%') as image: 
    image.compression_quality = 100 
    image.save() 

または

+0

私はあなたのコードを貼り付けましたが、私は次のようになっています:画像としてok.transform( '2000x1000'、 '100%'): AttributeError: '__enter__' – user61629

+0

問題は、バイナリ文字列。編集をご覧ください。 – user61629

関連する問題