Skimage.io.read imread a PIL Object - python

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')

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)

PIL image to array and back

EDIT: Sorry, the first version of the code was bullshit, I tried to remove useless information and made a mistake. Problem stays the same, but now it's the code I actually used
I think my problem is probably very basic but I cant find a solution. I basically just wanted to play around with PIL and convert an image to an array and backward, then save the image. It should look the same, right? In my case the new image is just gibberish, it seems to have some structure but it is not a picture of a plane like it should be:
def array_image_save(array, image_path ='plane_2.bmp'):
image = Image.fromarray(array, 'RGB')
image.save(image_path)
print("Saved image: {}".format(image_path))
im = Image.open('plane.bmp').convert('L')
w,h = im.size
array_image_save(np.array(list(im.getdata())).reshape((w,h)))
Not entirely sure what you are trying to achieve but if you just want to transform the image to a numpy array and back, the following works:
from PIL import Image
import numpy as np
def array_image_save(array, image_path ='plane_2.bmp'):
image = Image.fromarray(array)
image.save(image_path)
print("Saved image: {}".format(image_path))
im = Image.open('plane.bmp')
array_image_save(np.array(im))
You can just pass a PIL image to np.array and it takes care of the proper shaping. The reason you get distorted data is because you convert the pil image to greyscale (.convert('L')) but then try to save it as RGB.

How to convert huge image from PIL to cv2

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?

How do I change my image format?

I'm trying to blur an image using PIL:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
When I do im.show() and save it to my hard drive, it saves as a BMP file, which is incompatible with the place where I'm trying to upload it. How do I change the file format from BMP to something else that is compatible?
Just use the save() function directly:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
im.save("saved.jpg")
This function supports many formats, as explained in the documentation.

Converting raw images to tiff by using rawpy module in python

I'm new to image processing. I just wanted to get a tiff image from raw format(NEF). I used rawpy module to get the desired output, yet the tiff image is RGB with 4 channels. I couldn't know why there is a fourth channel in the new image?
Can anyone please explain to me what is going on, and how I can get tiff image with three RGB channels?
import rawpy
import matplotlib.pylab as plt
raw_image = "DSC_0001.NEF"
raw = rawpy.imread(raw_image)
rgb = raw.postprocess()
plt.imsave("new.tiff", rgb )
image = plt.imread("new.tiff")
print(image.shape)
The array shape is : (2868, 4310, 4) !
Finally I found the reason:
plt.imsave saves the image in RGBA , while I can use skim age.io.imsave and it will save it as RGB.
Source: Github Issue entry

Categories

Resources