user1592380
user1592380

Reputation: 36247

How to create high res JPEG with Wand from binary string

enter image description here

I'm trying to convert some PDFs to high res jpegs using imagemagick . I'm working on win 10, 64 with python 3.62 - 64 bit and wand 0.4.4. At the command line I have :

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

which is working well for me.

In python:

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')

Which is giving me low res JPEGs . How do I translate this into my wand code to do the same thing?

edit:

I realized that the problem is that the input pdf has to be read into the Image object as a binary string, so based on http://docs.wand-py.org/en/0.4.4/guide/read.html#read-blob I tried:

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')

This reads the file in ok, but the output is split into 10 files. Why? I need to get this into 1 high res jpeg.

EDIT:

I need to send the jpeg to an OCR api, so I was wondering if I could write the output to a file like object. Looking at https://www.imagemagick.org/api/magick-image.php#MagickWriteImageFile, I tried :

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)

This gives:

 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

How can I get this working?

Upvotes: 4

Views: 2058

Answers (2)

emcconville
emcconville

Reputation: 24419

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

The PDF contains pages that ImageMagick considers individual images in a "stack". The library provides a wand.image.Image.sequance to work with each page.

However, to append all images into a single JPEG. You can either iterate over each page & stitch them together, or call C-API's method 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)

Update:

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

Assuming your using OpenCV's python API, you'll only need to iterate over each page, and pass the image-file data to the OCR via numpy buffers.

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

Don't assume that python "image" objects (or underlining C structures) from different libraries are comparable with each other.

Without knowing the OCR api, I can't help you past the part, but I can suggest one of the following...

  • Use temporary intermediate files. (slower I/O, but easier to learn/develop/debug)

    with Image(filename=INPUT_PATH) as img:
        # work
        img.save(filename=OUTPUT_PATH)
    # OCR work on OUTPUT_PATH
    
  • Use file descriptors if the OCR API supports it. (Same as above)

    with open(INPUT_PATH, 'rb') as fd:
        with Image(file=fd) as img:
            # work
            # OCR work ???
    
  • Use blobs. (faster I/O but need a lot more memory)

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

Even More Updates

Wrapping all the comments together, a solution might be...

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)})

Upvotes: 2

jmunsch
jmunsch

Reputation: 24089

What about something like:

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

or:

with ok.resize(2000, 1000)

related:

Upvotes: 2

Related Questions