Comments (5)
The fourth band is the alpha. Just take the first three to remove it:
rgb = alpha[0:3]
from pyvips.
How to convert this pyvips image to numpy array of dimension HW x 3? By this I need to vectorize the image. I feel that if I was able to define a pyvios image with alpha channel ignored then I can write it using vips2numpy method where we assign shape as [HW,3]. Am I correct?
from pyvips.
Sorry, I don't understand. vips2numpy should work, shouldn't it?
Post some code I can run and explain the error you see.
from pyvips.
import pyvips
import numpy as np
format_to_dtype = {
'uchar': np.uint8,
'char': np.int8,
'ushort': np.uint16,
'short': np.int16,
'uint': np.uint32,
'int': np.int32,
'float': np.float32,
'double': np.float64,
'complex': np.complex64,
'dpcomplex': np.complex128,
}
**#map np dtypes to vips**
dtype_to_format = {
'uint8': 'uchar',
'int8': 'char',
'uint16': 'ushort',
'int16': 'short',
'uint32': 'uint',
'int32': 'int',
'float32': 'float',
'float64': 'double',
'complex64': 'complex',
'complex128': 'dpcomplex',
}
def numpy2vips(a):
height, width, bands = a.shape
linear = a.reshape(width * height * bands)
vi = pyvips.Image.new_from_memory(linear.data, width, height, bands,
dtype_to_format[str(a.dtype)])
return vi
def vips2numpy(vi):
return np.ndarray(buffer=vi.write_to_memory(),
dtype=format_to_dtype[vi.format],
shape=[vi.height, vi.width, vi.bands])
filename = '/home/Drive2/deepak/nikhil/her2_negative_svs/TCGA-AR-A24R-01Z-00-DX1.47D79205-63E7-43E6-A513-2CD10DFA53C9.svs'
Ip = pyvips.Image.new_from_file(filename)
Ip
**Out[8]:** <pyvips.Image 106623x65505 uchar, 4 bands, rgb>
**#simple vips2numpy**
def vips2numpy_vect(vi):
return np.ndarray(buffer=vi.write_to_memory(),
dtype=format_to_dtype[vi.format],
shape=[vi.height*vi.width, vi.bands])
img = vips2numpy_vect(Ip)
img.shape
**Out[11]:** (6984339615, 4)
del img
**#vips2numpy for just taking first three channels (r,g,b)**
def vips2numpy_vect(vi):
return np.ndarray(buffer=vi.write_to_memory(),
dtype=format_to_dtype[vi.format],
shape=[vi.height*vi.width, 3])
img = vips2numpy_vect(Ip)
img.shape
**Out[15]:** (6984339615, 3)
So first time i have 4 channels and vips2numpy satisfies the number of elements. What happens the second time? Are these 3 channels r,g,b or it is some component of the previous numpy array.
from pyvips.
It should just work. Numpy lets you reshape arrays quickly, I think. Use array slicing in pyvips to drop the alpha.
#!/usr/bin/python3
import sys
import pyvips
import numpy as np
format_to_dtype = {
'uchar': np.uint8,
'char': np.int8,
'ushort': np.uint16,
'short': np.int16,
'uint': np.uint32,
'int': np.int32,
'float': np.float32,
'double': np.float64,
'complex': np.complex64,
'dpcomplex': np.complex128,
}
#map np dtypes to vips
dtype_to_format = {
'uint8': 'uchar',
'int8': 'char',
'uint16': 'ushort',
'int16': 'short',
'uint32': 'uint',
'int32': 'int',
'float32': 'float',
'float64': 'double',
'complex64': 'complex',
'complex128': 'dpcomplex',
}
def numpy2vips(a):
height, width, bands = a.shape
linear = a.reshape(width * height * bands)
vi = pyvips.Image.new_from_memory(linear.data, width, height, bands,
dtype_to_format[str(a.dtype)])
return vi
def vips2numpy(vi):
return np.ndarray(buffer=vi.write_to_memory(),
dtype=format_to_dtype[vi.format],
shape=[vi.height, vi.width, vi.bands])
image = pyvips.Image.new_from_file(sys.argv[1])
print("loaded", image)
print("as a 4 band numpy image:")
nump = vips2numpy(image)
print(nump.shape)
# drop the alpha channel
image = image[0:3]
print("as a 3 band numpy image:")
nump = vips2numpy(image)
print(nump.shape)
# finally, as a (W * H, 3) image
print("as a 3 band linear image:")
nump = nump.reshape(image.width * image.height, 3)
print(nump.shape)
I see:
$ python3 try283.py ~/pics/openslide/CMU-1-Small-Region.svs
loaded <pyvips.Image 2220x2967 uchar, 4 bands, rgb>
as a 4 band numpy image:
(2967, 2220, 4)
as a 3 band numpy image:
(2967, 2220, 3)
as a 3 band linear image:
(6586740, 3)
from pyvips.
Related Issues (20)
- Memory leaking with pdf HOT 21
- dzsave_target returns NoneType not list[] as in docs HOT 1
- Pyvips fetch returns bad image on .svs files HOT 4
- Reading in image a second time breaks on qptiff file HOT 6
- Documentation link to conda package HOT 1
- Create svs file with image pyramid, label and macro in pyvips? HOT 4
- Image merge stitch HOT 2
- `addalpha` has different behaviour compared to C HOT 1
- Error installing pyvips on Mac OS HOT 15
- draw_rect fill error HOT 2
- creating tiles of 16bit multiband (4 band) tiff image leaves black canvas HOT 4
- class "jxlsave" not found HOT 9
- Assertion failed: sp->cinfo.comm.is_decompressor HOT 3
- Help installing pyvips for jupyter HOT 5
- `TIFFFillTile: Read error` when writing a TIFF file on a per-tile basis. HOT 2
- Thumbnail creation taking too long HOT 2
- Unable to save images when certain combinations of them are resize()'d HOT 7
- pyvips stats HOT 1
- How does composite() merge colors? HOT 8
- ICC profiles from file stream in pyvips HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvips.