libvips / pyvips

python binding for libvips using cffi
MIT License
624 stars 49 forks source link

TypeError: don't know the size pointed to by 'void *' #394

Open maqsudinamdar opened 1 year ago

maqsudinamdar commented 1 year ago

Environment

Problem Description

Unable to write on TargetCustom


Sample Code

import os
import pyvips
from google.cloud import storage
from google.cloud.storage.fileio import BlobWriter, BlobReader
from pyvips import Image

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "gc_a.json"
source_client = storage.Client("a-project")
source_bucket = source_client.get_bucket("a-bucket")
source_file_path = "path/source.jpg"
source_blob = source_bucket.blob(source_file_path)
source_file: BlobReader = source_blob.open(mode="rb")
def read_handler(size):
    return source_file.read(size)
source = pyvips.SourceCustom()
source.on_read(read_handler)

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "gc_b.json"
destination_client = storage.Client("b-project")
destination_bucket = destination_client.get_bucket("b-bucket")
destination_file_path = "path/dest.dz"
destination_blob = destination_bucket.blob(destination_file_path)
destination_file: BlobWriter = destination_blob.open(mode="wb")
def write_handler(chunk):
    return destination_file.write(chunk)
def finish_handler():
    destination_file.close()
target = pyvips.TargetCustom()
target.on_write(write_handler)
target.on_finish(finish_handler)

image: Image = pyvips.Image.new_from_source(source, '')
image.write_to_target(target, "dest.dz")

Traceback:

Exception ignored from cffi callback <function _marshal_write at 0x113177490>:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pyvips/gobject.py", line 77, in _marshal_write
    buf = ffi.buffer(pointer, length)
TypeError: don't know the size pointed to by 'void *'

Edit:

jcupitt commented 1 year ago

Hi again,

I can't reproduce your problem, so I can't fix it. You need to post a complete, runnable program I can test. What cffi are you using, what python version are you using, what platform are you using, what C compiler are you using, how did you install pyvips (ABI or API mode) etc.

There have been some changes to void* use in libvips 8.14.2 which might help, see: https://github.com/libvips/libvips/pull/3383

Sample code:

#!/usr/bin/python3

import sys
import pyvips

image = pyvips.Image.new_from_file(sys.argv[1], access="sequential")

file = open(sys.argv[2], "wb")

def write_handler(chunk):
    return file.write(chunk)

def end_handler():
    # you can't throw exceptions over on_ handlers, you must return an
    # error code
    try:
        file.close()
    except IOError:
        return -1
    else:
        return 0

target = pyvips.TargetCustom()
target.on_write(write_handler)
target.on_end(end_handler)

image.write_to_target(target, ".dz")

Run like this:

$ ./targetsave2.py ~/pics/k2.jpg x
$ file x
x: Zip archive data, at least v4.5 to extract, compression method=store

libvips 8.14.1, python 3.10 in API mode, cffi 1.15.1, gcc 12, ubuntu 22.10.