Rasterio Creating TIFF file - arrays

I tried to use the code on the Rasterio-doc website to write an array as a TIFF to disk
https://rasterio.readthedocs.io/en/latest/topics/writing.html
with rasterio.Env():
profile = src.profile
profile.update(
dtype=rasterio.uint8,
count=1,
compress='lzw')
with rasterio.open('example.tif', 'w', **profile) as dst:
dst.write(array.astype(rasterio.uint8), 1)
When I run the code the following error occurs: 'name 'array' is not defined'.
I tried in the last line with 'np.array' instead of 'array' to say that it is a numpy-array but it didn't worked.

The variable 'array' stands for the data that should be written to disk. Create a numpy array as for example:
import numpy as np
array = np.array(my_array_data)
Then you can write this data to disk as described in the tutorial.

Related

Is there a way to ask numpy to ignore UnicodeEncode errors when creating ndarrays made up of text?

I am an absolute novice to Python so go easy on me please. See my code below
import pdfplumber
with pdfplumber.open('C:/Users/hasan/simsani/Invoices/Invoice SB14082021-1.pdf') as pdf:
first_page = pdf.pages[0]
b = first_page.extract_text()
import numpy as np
c = np.array(b, dtype = 'S')
When i run the script above i get an error (line 6) that says the following
UnicodeEncode error: asci codec cannot encode character '\uf009' in position 25: ordinal not in range (128)
Is there a way to tell numpy to ignore the uf009 character and any other ones that may throw up a similar UnicodeEncode error when creating the array ?
Please give example code if you have an answer please, and make response simple (remember i am a novice here :)
Cheers

Converting RGB data into an array from a text file to create an Image

I am trying to convert txt RGB data from file.txt into an array. And then, using that array, convert the RGB array into an image.
(RGB data is found at this github repository: IR Sensor File.txt).
I am trying to convert the .txt file into an array which I could use the PIL/Image library and convert the array into an Image, and then put it through the following script to create my image.
My roadblock right now is converting the arrays in file.txt into an appropriate format to work with the Image function.
from PIL import Image
import numpy as np
data = [ARRAY FROM THE file.txt]
img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.show()
The RGB data looks like as follows, and can also be found at the .txt file from that github repository linked above:
[[(0,255,20),(0,255,50),(0,255,10),(0,255,5),(0,255,10),(0,255,25),(0,255,40),(0,255,71),(0,255,137),(0,255,178),(0,255,147),(0,255,158),(0,255,142),(0,255,163),(0,255,112),(0,255,132),(0,255,137),(0,255,153),(0,255,101),(0,255,122),(0,255,122),(0,255,147),(0,255,66),(0,255,66),(0,255,30),(0,255,61),(0,255,0),(0,255,0),(0,255,40),(0,255,66),(15,255,0),(0,255,15)],
[(0,255,40),(0,255,45),(15,255,0),(20,255,0),(10,255,0),(35,255,0),(0,255,5),(0,255,56),(0,255,173),(0,255,168),(0,255,153),(0,255,137),(0,255,158),(0,255,147),(0,255,127),(0,255,117),(0,255,142),(0,255,142),(0,255,122),(0,255,122),(0,255,137),(0,255,137),(0,255,101),(0,255,66),(0,255,71),(0,255,61),(0,255,25),(0,255,25),(0,255,61),(0,255,35),(0,255,0),(35,255,0)],
[(0,255,15),(0,255,25),(51,255,0),(71,255,0),(132,255,0),(101,255,0),(35,255,0),(0,255,20),(0,255,91),(0,255,153),(0,255,132),(0,255,147),(0,255,132),(0,255,158),(0,255,122),(0,255,132),(0,255,142),(0,255,158),(0,255,122),(0,255,137),(0,255,142),(0,255,147),(0,255,101),(0,255,101),(0,255,86),(0,255,86),(0,255,50),(0,255,45),(0,255,50),(0,255,56),(0,255,30),(56,255,0)],
[(0,255,45),(0,255,10),(76,255,0),(127,255,0),(132,255,0)]]
I think this should work - no idea if it's decent Python:
#!/usr/local/bin/python3
from PIL import Image
import numpy as np
import re
# Read in entire file
with open('sensordata.txt') as f:
s = f.read()
# Find anything that looks like numbers
l=re.findall(r'\d+',s)
# Convert to numpy array and reshape
data = np.array(l).reshape((24,32,3))
# Convert to image and save
img = Image.fromarray(data, 'RGB')
img.save('result.png')
I enlarged and contrast-stretched the image subsequently so you can see it!

cv.Save and cv.Load (array)

I need to save and load an array, but I get this error:
cv.Save('i.xml',i)
TypeError: Cannot identify type of 'structPtr'
This is the code:
import cv
i = [[1,2],[3,4],[5,6],[7,8]]
cv.Save('i.xml',i)
That's because cv.Save needs to receive the object to be stored in the file as an OpenCV object. For example, the following is a minimal workable example that saves a numpy array in a file using cv.Save:
import cv2
import numpy as np
i = np.eye(3)
cv2.cv.Save('i.xml', cv2.cv.fromarray(i))
As you can see here, arrays should be converted back to numpy from OpenCV as well after reading.
Regards.

Problems saving arrays as greyscale 'L' images using matplotlib?

I'm trying to save an array as an image using plt.imsave(). The original image is a 16 greyscale 'L' tiff. But I keep on getting the error:
Attribute error: 'str' object has no attribute 'shape'
figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
im2=plt.imread('C:\Documents\Image\pic.tif')
plt.imsave(im2, '*.tif')
The image is 2048x2048, the array is 2048Lx2048L. Everything I've tried doesn't work: shape=[2048,2048], im2.shape(2048,2048). Can anybody tell me out how to add shape as a keyword argument? Or is there any easier way to do this, preferably avoiding PIL, since it seems to have issues with 16-bit greyscale tiffs and I absolutely have to use that format?
I think you've got the arguments backwards. From help(plt.imsave):
Help on function imsave in module matplotlib.pyplot:
imsave(*args, **kwargs)
Saves a 2D :class:`numpy.array` as an image with one pixel per element.
The output formats available depend on the backend being used.
Arguments:
*fname*:
A string containing a path to a filename, or a Python file-like object.
If *format* is *None* and *fname* is a string, the output
format is deduced from the extension of the filename.
*arr*:
A 2D array.
i.e.:
>>> im2.shape
(256, 256)
>>> plt.imsave(im2, "pic.tif")
Traceback (most recent call last):
File "<ipython-input-36-a7bbfaeb1a4c>", line 1, in <module>
plt.imsave(im2, "pic.tif")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1753, in imsave
return _imsave(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1230, in imsave
figsize = [x / float(dpi) for x in arr.shape[::-1]]
AttributeError: 'str' object has no attribute 'shape'
>>> plt.imsave("pic.tif", im2)
>>>

Iterate over files in a folder to create numpy array

this is my first posting and I am really new to programming -
I have a folder with some files that I want to process and then create a numpy array with the values I need I do:
listing = os.listdir(datapath)
my_array=np.zeros(shape=(0,5))
for infile in listing:
dataset = open(infile).readlines()[1:]
data = np.genfromtxt(dataset, usecols=(1,6,7,8,9))
new_array = np.vstack((my_array, data))
and although I have 2 files in listing (datapath folder) the new_array array overwrites the data and gives me only the values of the second file
any ideas?
thanks,
If I understand you correctly, the solution to your problem is simply that you need to vstack it to "my_array" not to a new one.
Just replace the last line with this one and it should work:
my_array = np.vstack((my_array, data))
However, I do not think this is the most efficient way to do it. Since you know how many files are in that folder, just predefine the size of the array and fill its content.
Here is what you need to do to read all files in a numpy array from a specific folder. I have a folder test containing only .txt files. My following file.py is in the same test folder along with all .txt files. Each .txt file contains a 4x4 matrix/array. After running the script the obtained matrices will be a numpy array of [Nx4x4].
import numpy as np
from glob import glob
def read_all_files():
file_names = glob('test/*')
arrays = [np.loadtxt(f) for f in file_names]
matrices = np.concatenate(arrays)

Resources