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.
Related
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)
I am trying to create a simple image using Numpy and PIL. However, I seem to be getting this bizarre image instead of what I expected.
My code (Cell wise in a jupyter notebook)
import numpy as np
from PIL import Image
arr = np.zeros([100,100,3])
arr[:,:] = [255,128,0]
img = Image.fromarray(arr, 'RGB')
img
The resultant image is this:
I expected an image which would've been completely orange.
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
I am using the following code:
import cv2
import numpy as np
import pyautogui
import sys
img = pyautogui.screenshot()
cv2.imshow('image',img)
When I run this, it tells me
mat is not a numpy array, neither a scalar
I have tried to use different functions from opencv and it seems they all return the same. What do I need to do in order to take a screenshot then work with it in Open CV?
After some digging, I realise that the pyautogui function is using Pillow which is giving a format that must be adapted to work with opencv.
I added the following code so that it worked:
open_cv_image = np.array(img)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
I am looking for a way to rescale the matrix given by reading in a png file using the matplotlib routine imread,
e.g.
from pylab import imread, imshow, gray, mean
from matplotlib.pyplot import show
a = imread('spiral.png')
#generates a RGB image, so do
show()
but actually I want to manually specify the dimension of $a$, say 200x200 entries, so I need some magic command (which I assume exists but cannot be found by myself) to interpolate the matrix.
Thanks for any useful comments : )
Cheers
You could try using the PIL (Image) module instead, together with numpy. Open and resize the image using Image then convert to array using numpy. Then display the image using pylab.
import pylab as pl
import numpy as np
from PIL import Image
path = r'\path\to\image\file.jpg'
img = Image.open(path)
img.resize((200,200))
a = np.asarray(img)
pl.imshow(a)
pl.show()
Hope this helps.