-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image format converting questions #179
Comments
Hello @LightToYang, I would use something like: image = pyvips.Image.new_from_file(f, access="sequential")
image = image.colourspace("srgb")
mem_img = image.write_to_memory()
imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)
return imgnp And you should get an RGB buffer. |
However, I think your benchmark is misleading. libvips will cache the JPEG decode, so it's just being decoded once. Try: def usingVIPS(f):
image = pyvips.Image.new_from_file(f, access="sequential")
mem_img = image.write_to_memory()
imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)
return imgnp This test is just timing JPEG decode, so all the libraries here will be almost the same speed, since they are using the same JPEG decode library. pyvips can be much quicker for some image files -- regions from tiled tiff, for example, or large whole-slide images -- but I don't know if that will apply here. |
Actually, it's still a bit quicker. With this code: #!/usr/bin/python3
import numpy as np
import pyvips
import cv2
from PIL import Image
import timeit
def usingPIL(f):
im = Image.open(f)
return np.asarray(im)
def usingOpenCV(f):
arr = cv2.imread(f,cv2.IMREAD_UNCHANGED)
return arr
def usingVIPS(f):
image = pyvips.Image.new_from_file(f, access="sequential")
image = image.colourspace("srgb")
mem_img = image.write_to_memory()
imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)
return imgnp
def usingPILandShrink(f):
im = Image.open(f)
im.draft('RGB',(1512,1008))
return np.asarray(im)
def usingVIPSandShrink(f):
image = pyvips.Image.new_from_file(f, access="sequential", shrink=4)
image = image.colourspace("srgb")
mem_img = image.write_to_memory()
imgnp=np.frombuffer(mem_img, dtype=np.uint8).reshape(image.height, image.width, 3)
return imgnp
def bench(name):
result = timeit.timeit(f"using{name}('image.jpg')",
setup=f"from __main__ import using{name}",
number=10)
print(f"using{name}: {result * 10} ms")
bench("PIL")
bench("OpenCV")
bench("VIPS")
bench("PILandShrink")
bench("VIPSandShrink") Running on ubuntu19.10 I see:
|
Updatepyvips now has better numpy integration, so you can just write: def usingVIPSandShrink(f):
image = pyvips.Image.new_from_file(f, access="sequential", shrink=4)
return image.numpy() |
I am working on speeding up image loading for training models on ImageNet Dataset.
Based on https://stackoverflow.com/questions/57663734/how-to-speed-up-image-loading-in-pillow-python,
I find pyvips is suitable for my tasks. Since, the image formats of ImageNet contain RGB, Gray and CYMK, how can I transform Gray or CYMK format images to RGB format like https://libvips.github.io/pyvips/intro.html#numpy-and-pil
The text was updated successfully, but these errors were encountered: