How to convert huge image from PIL to cv2 - python

I have a huge PNG image (625000000 pixels) saved in 1 format via PIL library.
I need to load this image from disk and pass it to cv2.matchTemplate function.
I can't pass an image returned from the PIL library directly because it requires a numpy array:
huge_img = Image.open('huge.png')
result = cv2.matchTemplate(huge_img, sub_img, cv2.TM_CCOEFF_NORMED)
Output
TypeError: <unknown> is not a numpy array
But if I try to construct a numpy array from the image object it throws MemoryError exception:
huge_img = numpy.array(Image.open('huge.png'))
Output
MemoryError
What can I do then? Is there any workaround to use such images?

Related

Converting PIL.Image to skimage

I have 2 modules in my project: first works with image in bytes format, second requires skimage object. I need to combine them.
I have this code:
import io
from PIL import Image
import skimage.io
area = (...)
image = Image.open(io.BytesIO(image_bytes))
image = Image.crop(area)
image = skimage.io.imread(image)
But i get this error:
How can i convert an image (object/variable) to skimage? I don't necessarily need PIL Image, this is just one way to work with bytes image, cause i need to crop my image
Thanks!
Scikit-image works with images stored as Numpy arrays - same as OpenCV and wand. So, if you have a PIL Image, you can make a Numpy array for scikit-image like this:
# Make Numpy array for scikit-image from "PIL Image"
na = np.array(YourPILImage)
Just in case you want to go the other way, and make a PIL Image from a Numpy array, you can do:
# Make "PIL Image" from Numpy array
pi = Image.fromarray(na)

Skimage.io.read imread a PIL Object

So I was working with skimage for some image preprocessing (i'm very new to it). I have a PIL Image object and wanted to convert to a skimage image with skimage.io.imread(). I know that I can just save the image and then run imread on that file, but I was wondering if there was a way I could read the PIL Image object from the code itself. I tried to run imread on the PIL Image object itself but I end up getting errors.
OSError: Cannot understand given URI: <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=192....
Anyone know how I can solve this in skimage.
Scikit-Image stores images as Numpy arrays, so you just need to make a Numpy array from your PIL Image:
ImageForScikit = np.array(YourPILImage)
You may review imageio documentation related to the function imread for including in your code the attribute format and code as follows just in case the image format is PNG: imageio.imread(filename, format = 'PNG')

convert ndarray object to Image

I have a gray scale image of type uint16, size = (256,256) ndarray object
I want to use PIL to resize it to (75,75) but it requires the input to be of Image type.
How can I convert image of ndarray object into Image type to use image.resize((75,75), Image.ANTIALIAS)
NOTE:
I know I can read image using Image.open if it is saved, but my image is obtained after some image processing steps and is not read from disk
UPDATE:
I am trying to provide image that I have :
import scipy.misc
scipy.misc.imsave('image.png', box_img)
# read this similar format image of type ndarray
image = scipy.ndimage.imread('image.png')
# convert it to Image type
The image attached when read of similar type as I need.
I need to convert this image into Image type
Thanks,
Gopi
from PIL import Image
import numpy as np
# An array of ones for example
img_array = np.ones((256,256), dtype='uint16')
img = Image.fromarray(img_array)
img = img.resize((75,75))

m is not a numpy array, neither a scalar

import cv2
import numpy as np
from PIL import Image
img = Image.open("test.jpg")
imgfilename = img.filename
imgb,imgg,imgr = cv2.split(img)
count = 0
I've been getting the following error when I try to run my code - this is the error I'm getting:
File "WB.py", line 9, in <module>
imgb,imgg,imgr = cv2.split(img)
TypeError: m is not a numpy array, neither a scalar
You're generally not supposed to use PIL together with numpy, these libraries don't interact a lot.
From numpy (and opencv)'s point of view, images are just 2D or 3D arrays of any given type (2D for grayscale, 3D for color). Also cv2 uses BGR by default...
Start with cv2.imread(path, cv2.IMREAD_COLOR) instead of Image.open(path).
You should use the split function available in PIL for this purpose.
Image.split()
This is because images are interpreted differently in OpenCV and PIL. Hence you cannot you the functions available in these packages interchangeably.

How to open a CR2 file in python OpenCV

I've managed to come very far on a program I'm writing. I don't know how to load CR2 files into an OpenCV Image. I've tried the following:
raw = rawpy.imread(sys.argv[1])
rgb = raw.postprocess()
PILrgb = scipy.misc.toimage(rgb)
image = cv2.imdecode(PILrgb, 1)
It was an attempt at converting the numpyarray returned by Postprocess the currently loaded RAW image and return the new resulting image as numpy array. Then calling spicy.misc.toimage to Takes a numpy array and returns a PIL image..
I get the following msg though TypeError: buf is not a numpy array, neither a scalar
It may be easier if you only rawpy
import rawpy
import cv2
raw = rawpy.imread(sys.argv[1]) # access to the RAW image
rgb = raw.postprocess() # a numpy RGB array
image = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR) # the OpenCV image

Categories

Resources