I have a script that should open an image as a 2D array but I can't seem to make it work. I have tried using the numpy an PIL libraries. I tried this on different computers. The issue is that it opens the image as a 2D array on one computer but opens them ups as objects on a different compute running the same version of python.
The code that should open the image and store it as an array can be seen below:
img = np.array(Image.open(imagePath))
On one computer I get an array but on another one I get an image object like this:
array(<PIL.TiffImagePlugin.TiffImageFile image mode=I;16B size=320x240 at 0x2289FA8>, dtype=object)
Have you tried the imread function from matplotlib?
from matplotlib.image import imread
image = imread(image_path)
Returns a numpy array and works fine for me (python 3.4).
Related
I have searched everywhere for an answer for this question but no luck. I want to figure out how to take the image generated by python's noise library and export it. does anybody know how?
The GitHub repository for the noise library has an examples folder. One of them is an example of how to generate 2D perlin noise and write it to a file.
If you want more standard file formats, such as a PNG or TIFF, you can use Numpy to create an array and write the perlin noise values to the array, and then save the array as an image file using OpenCV.
Convert your data list to a numpy array then use PIL library to save the numpy array to a grayscale image:
# Install PIL: pip install pillow (probably already installed)
from PIL import Image
import numpy as np
from perlin_noise import PerlinNoise
noise = PerlinNoise(octaves=10, seed=1)
xpix, ypix = 100, 100
pic = [[noise([i/xpix, j/ypix]) for j in range(xpix)] for i in range(ypix)]
image = Image.fromarray(np.array(pic) * 255, 'L')
image.save('output.png')
these day, i've done some research that change real masonry wall image into architectural drawing(like CAD,Blueprinting).
so.. my solution is change real masonry image into grayscale image, then convert 0 and 1 numpy array. is this process possible?
and also if i can make image numpy array, then can i import this data array into Dynamo(Revit plugin)?
Answering your questions one by one:
yes, you can change a real masonry image into a grayscale image
yes, you can convert an image into the binary matrix stored in a numpy array
yes, you can import this data to Dynamo
What's more, you can actually do all of those in Dynamo using Python Script node starting from importing the image file. If you are working in Revit 2022 you can benefit from CPython and simply import numpy, if older versions, you would have to point to location where numpy is installed.
To do it, first, you need to have numpy for ironpython2.7 (the version used in Revit 2021 and older). Instructions can be found here: How to install numpy and scipy for Ironpython27? Old method doesn't work
Once you have it, simply point in your Dynamo Python Script node to that library location:
import sys
import clr
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import numpy
I'm trying to save a 32-bit floating point image (stored as a Numpy array) as a TIFF file using tifffile.py.
import numpy as np
import tifffile
image = np.random.rand(500, 500, 3).astype(np.float32)
tifffile.imsave('image.tiff', image)
However, when viewing the output of the above code in Eye of Gnome, the image is entirely blank.
I think the problem is that not all tools support multi-channel TIFFs with 32-bits per channel. For example, as far as I can tell Python's PIL library does not. But I think tifffile.py does, because if I use your code I get a TIFF that opens, and looks reasonable, in GIMP:
From what I read, Photoshop can read 32-bit TIFFs too. So I think the TIFF file contains your image, but whether it works for you or not depends on what you want to do with it next.
This question might be relevant too, although it's about using 16-bit integers not floats: Python: Read and write TIFF 16 bit , three channel , colour images
I have CSV files that I need to feed to a Deep-Learning network. Currently my CSV files are of size 360*480, but the network restricts them to be of size 224*224. I am using Python and Keras for the deep-learning part. So how can I resize the matrices?
I was thinking that since aspect ratio is 3:4, so if I resize them to 224:(224*4/3) = 224:299, and then crop the width of the matrix to 224, it could serve the purpose. But I cannot find a suitable function to do that. Please suggest.
I think you're looking for cv.resize() if you're using images.
If not, try numpy.ndarray.resize()
Image processing
If you want to do nontrivial alterations to the data as images (i.e. interpolating between pixel values, assuming that they represent photographs) then you might want to use proper image processing libraries for that. You'd need to treat them not as raw matrixes (csv of numbers) but convert them to rgb images, do the transformations you desire, and convert them back to a numpy matrix.
OpenCV (https://docs.opencv.org/3.4/da/d6e/tutorial_py_geometric_transformations.html)
or Pillow (https://pillow.readthedocs.io/en/3.1.x/reference/Image.html) might be useful to do that.
I found a short and simple way to solve this. This uses the Python Image Library/Pillow.
import numpy as np
import pylab as pl
from PIL import Image
matrix = np.array(list(csv.reader(open('./path/mat.csv', "r"), delimiter=","))).astype("uint8") #read csv
imgObj = Image.fromarray(matrix) #convert matrix to Image object
resized_imgObj = img.resize((224,224)) #resize Image object
imgObj.show()
resized_imgObj.show()
resized_matrix = np.asarray(img) #convert Image object to matrix
While numpy module also has a resize function, but it is not as useful as the aforementioned way.
When I tried it, the resized matrix had lost all the intricacies and aesthetic aspect of the original matrix. This is probably due to the fact that numpy.ndarray.resize doesn't interpolate and missing entries are filled with zeros.
So, for this case Image.resize() is more useful.
You could also convert the csv file to a list, truncate the list, and then convert the list to a numpy array and then use np.reshape.
I am having to do a lot of vision related work in Python lately, and I am facing a lot of difficulties switching between formats. When I read an image using Mahotas, I cannot seem to get it to cv2, though they are both using numpy.ndarray. SimpleCV can take OpenCV images easily, but getting SimpleCV image out for legacy cv or mahotas seems to be quite a task.
Some format conversion syntaxes would be really appreciated. For example, if I open a greyscale image using mahotas, it is treated to be in floating point colour space by default, as I gather. Even when I assign the type as numpy.uint8, cv2 cannot seem to recognise it as an array. I do not know how to solve this problem. I am not having much luck with colour images either. I am using Python 2.7 32bit on Ubuntu Oneiric Ocelot.
Thanks in advance!
I have never used mahotas. But I'm currently working on SimpleCV. I have just sent a pull request for making SimpleCV numpy array compatible with cv2.
So, basically,
Image.getNumpy() -> numpy.ndarray for cv2
Image.getBitmap() -> cv2.cv.iplimage
Image.getMatrix() -> cv2.cv.cvmat
To convert cv2 numpy array to SimpleCV Image object,
Image(cv2_image) -> SimpleCV.ImageClass.Image
With only experience in cv2 and SimpleCV, to convert from SimpleCV to cv2:
cv2_image = simplecv_image.getNumpyCv2()
To convert from cv2 to SimpleCV:
simplecv_image = Image(cv2_image.transpose(1, 0, 2)[:, :, ::-1])