pyvips
pyvips copied to clipboard
TypeError in pyvips
Hi,
I am going to use pyvips to create dzi files from a numpy array but get the following error.
TypeError Traceback (most recent call last)
/tmp/ipykernel_56220/2408616579.py in <module>
3
4 a = (np.random.random((100, 100, 3)) * 255).astype(np.uint8)
----> 5 image = pyvips.Image.new_from_array(a)
~/anaconda3/lib/python3.7/site-packages/pyvips/vimage.py in new_from_array(array, scale, offset)
301 for y in range(0, height):
302 for x in range(0, width):
--> 303 a[x + y * width] = array[y][x]
304
305 vi = vips_lib.vips_image_new_matrix_from_array(width, height, a, n)
TypeError: only size-1 arrays can be converted to Python scalars
I am using Ubuntu 18.04.6. Thanks in advance for your help.
Best,
Hi @Rasaa84,
It seems to be working for me. I see:
$ python3
Python 3.10.7 (main, Nov 24 2022, 19:45:47) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import pyvips
>>> a = (np.random.random((100, 100, 3)) * 255).astype(np.uint8)
>>> a
array([[[147, 168, 35],
[235, 102, 5],
[149, 8, 189],
...,
[ 66, 7, 253],
[ 75, 161, 124],
[164, 197, 41]],
[[184, 198, 163],
[ 81, 113, 50],
[ 4, 82, 160],
...,
[152, 177, 119],
[123, 129, 59],
[174, 244, 138]],
[[104, 243, 90],
[ 89, 153, 208],
[ 90, 44, 232],
...,
[176, 46, 127],
[233, 162, 177],
[ 91, 8, 140]],
...,
[[118, 10, 11],
[123, 136, 150],
[ 50, 138, 165],
...,
[109, 8, 56],
[163, 144, 48],
[217, 82, 231]],
[[ 37, 115, 231],
[132, 116, 196],
[ 29, 233, 243],
...,
[216, 58, 221],
[216, 234, 186],
[ 22, 121, 50]],
[[181, 251, 145],
[ 63, 36, 249],
[175, 119, 111],
...,
[ 33, 8, 46],
[ 75, 138, 119],
[ 31, 120, 114]]], dtype=uint8)
>>> x = pyvips.Image.new_from_array(a)
>>> x
<pyvips.Image 100x100 uchar, 3 bands, srgb>
>>> pyvips.__version__
#'2.2.1'
I would guess this is an anaconda issue. Have you tried vanilla python? Anaconda isn't usually necessary on linux, and often causes bugs.
I tried on 18.04 with vanilla python and it seems to work there too. I used this dockerfile:
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y \
build-essential \
python3-pip
RUN apt-get install -y --no-install-recommends \
libvips-dev
RUN pip3 install pyvips numpy
Then:
$ docker build -t pyvips-ubuntu18.04 .
Sending build context to Docker daemon 16.38kB
Step 1/6 : FROM ubuntu:18.04
---> e28a50f651f9
...
$ docker run -it --rm -v $PWD:/data pyvips-ubuntu18.04
root@7000341907eb:/# python3
Python 3.6.9 (default, Nov 25 2022, 14:10:45)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> pyvips.API_mode
True
>>> import numpy as np
>>> np.__version__
'1.19.5'
>>> a = (np.random.random((100, 100, 3)) * 255).astype(np.uint8)
>>> a
array([[[215, 243, 83],
[ 54, 163, 127],
[180, 76, 119],
...,
[246, 202, 209],
[ 80, 48, 242],
[137, 37, 125]]], dtype=uint8)
>>> x = pyvips.Image.new_from_array(a)
>>> x
<pyvips.Image 100x100 uchar, 3 bands, rgb>
>>>