PIL to pyvision conversion - python

How can a PIL image be converted to a Pyvision image?

Based on the documentation, http://sourceforge.net/apps/mediawiki/pyvision/index.php?title=Quick_Start_1, Pvvision image itself is PIL image.
The Image constructor accepts
filenames as an argument and will then
load that file from the disk as a PIL image. The Image constructor
will also accept other python image objects. For example, if you
pass a numpy matrix, PIL image, or an OpenCV image to the constructor
it will crate a pyvision image based on that data.

Related

Matplotlib: How to save an image at full resolution?

I've created a mozaic image, such that a large picture consists of many tiny pictures. I can view this image just fine in the matplotlib viewer and I can zoom in to see all the tiny pictures. But when I save the image, no matter the extension, the image loses the zoom ability such that the tiny images become blurred when zooming in. Is there a way to save the image in full resolution?
I have the image in rgb in a numpy array so if there are other libraries more suitable that would work too.
This should work :
from PIL import Image
Image.fromarray(numpy_img).save("img_path.png")
I think you're being mislead by Windows's Photos Application here, which applies blur automatically if you zoom too much.
The image is being saved correctly with all pixel values by Matplotlib, you can check it by zooming again on the pixels of the loaded image.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# save image
array = np.random.random((4000, 4000, 3))
plt.imsave("save.png", array)
# load image
img = mpimg.imread("save.png")
plt.imshow(img)
plt.show()
Another option is the small library that I wrote called numpngw. If the numpy array is, say, img (an array of 8-bit or 16-bit unsigned integers with shape (m, n, 3)), you could use:
from numpngw import write_png
write_png('output.png', img)
(If the array is floating point, you'll have to convert the values to unsigned integers. The PNG file format does not store floating point values.)
You can try imageio library:
https://imageio.readthedocs.io/en/stable/userapi.html#imageio.imwrite
from imageio import imwrite
imwrite("image.png", array)

Python Pillow: how to produce 3-channel image from 1-channel image?

A Python package that I'm trying to use only works with 3-channel images. If I have a grayscale PNG image, Pillow's Image.open() naturally reads it as a single-layer image. How can I use Pillow to transform the 1-channel image into a 3-channel RGB image?
The simplest method to convert a single-channel greyscale image into a 3-channel RGB image with PIL is probably like this:
RGB = Image.open('image.png').convert('RGB')
Further discussion and explanation is available here.

Cannot convert PIL image from Paint.NET to numpy array

I am getting a ValueError in numpy when performing operations on images. The problem seems to be that the images edited by Paint.NET are missing the RGB dimension when opened using PIL and converted to a numpy array.
If PIL is giving you a 861x1091 image when you are expecting an 861x1091x3 image, that is almost certainly because it is a palette image - see here for explanation.
The simplest thing to do, if you want a 3-channel RGB image rather than a single channel palette image is to convert it to RGB when you open it:
im = Image.open(path).convert('RGB')

Loosing Geospatial information in a .tif image

I have never worked with TIF images, I have a specific problem definition where I need to work on satellite raster in .TIF format, which I need to upload on Google Earth Engine.
But when I convert image into Numpy array to apply some Image Processing techniques it looses its Georeferencing. Is there any way of preserving that in Output Image?
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
ImagenTotal = numpy.asarray(Image.open('Well/newnew3.tif'))
imshow(ImagenTotal[10:-10,10:-10,:])
pylab.savefig('foo{}.tif'.)

reading pgm images with cv2 in python

I want to read pgm image in python. I use cv2.imread('a.pgm') but it returns wrong results. In Matlab, I use imread and get the right result which is a single channel 16-bit image. But cv2.imread in python returns a 3-channel image and the pixel values are also wrong.
Why it happens?
How should I read the 16-bit pgm images in python?
And what libraries?
Thanks in advance.
I got it.
cv2.imread('a.pgm',-1)
works.
You can also use skimage.io library
from skimage.io import imread
image = imread("a.pgm")

Categories