unable to clip raster(ETrF.etrf.tif) in python? - python-2.7

I have 50 rasters saved as LC81520412017282LGN00_ETrF.etrf.tif , and a polygon shapefile saved as a .shp.
as tried to clip this image using Clip(Data Management) in arcgis , it woks fine.but to process all image in bulk , I have written a script as below
import os
import arcpy
arcpy.env.workspace = "E:/Project"
for root, dirs, files in os.walk('E:/Project/a/r'):
for file in files:
if os.path.splitext(file)[1] == '.tif':
print (file)
arcpy.Clip_management(file,"#",
"E:/Project/be/test.gdb/%file%"
,"be/canal.shp","#","Clipping
Geometry", "NO_MAINTAIN_EXTENT")
the above script throws an error of "unsupported file or file doesn't exist".

the clip management is only to clip a shapefile to "Cut" a raster file using a shapefile you have to use
ExtractByMask (in_raster, in_mask_data)
here is an example
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outExtractByMask = ExtractByMask("elevation", "mask.shp")
outExtractByMask.save("C:/sapyexamples/output/maskextract")

Related

Kernel restarts when compressing tif file using PIL in Anaconda

I'm trying to compress a bunch of tiff files with the pillow package. However, when I'm executing the code in python3.7.13 in Spyder IDE within the Anaconda3 environment, the Kernel restarts in the line in which the tiff file should be compressed. I tried different compression methods (e.g. "group4", "zlib", "deflate", etc..).
I also tried other packages like libtiff and tifffile, but the same problem occurs here as well.
import os
import glob
from PIL import Image, TiffTags
from IPython.display import display
import numpy as np
images = [file for file in os.listdir("C:/Home/Slicer/tif") if file.endswith('tif')]
#search for alle the images in the path
for image in images:
img_name = str(image)
img = Image.open("C:/Home/Slicer/tif/"+img_name)
print(img_name + str(img))
display(img)
#save tiff file with new name
img.save("C:/Home/Slicer/tif/" + "compressed" + img_name, compression="tiff_lzw")
Console output

Creating HDF5 format for image segmentation task

I started writing a python code for creating HDF5 for image segmentation tasks. I used the code in this link and the link provided by Shai. my images are one channel and in .mat format. I have written the following code, I only want to check with experts whether this code is correct or not. Could experts please have a look? Thanks
import os, h5py
import caffe
import numpy as np
import scipy
import scipy.io as sio
from array import array
import cv2
import matplotlib.pyplot as plt
caffe_root='/home/ss/caffe/'
import sys
sys.path.insert(0,caffe_root+'python')
def img_to_hdf5(paths_src_file,paths_lbl_file,path_dst,msg):
"""
paths_src_file : path to the image paths in a txt file
paths_lbl_file : path to the image paths in a txt file
path_dst = path to the hdf5 file
"""
print(msg)
arrays = {}
SIZE=256 #fixed size of all images
#read the lines of img and lbl path from text file and save into paths_src and paths_lbl
paths_src = []
with open(paths_src_file) as f:
for line in f.readlines():
line = line.strip('\n')
paths_src.append(line)
paths_lbl=[]
with open(paths_lbl_file) as f:
for line in f.readlines():
line=line.strip('\n')
paths_lbl.append(line)
data = np.zeros( (len(paths_src), 1, SIZE, SIZE), dtype='f4' ) #1 channel grayscale image
label = np.zeros( (len(paths_lbl), 1, SIZE, SIZE), dtype='f4' ) #1 channel label image
for in_idx, in_ in enumerate(paths_src):
print in_idx,in_
f=h5py.File(in_,'r')
mat=f['image'].value
im=np.array(mat,dtype=np.float32)
#im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)
#im = im[:,:,::-1] #switch from RGB to BGR
im = im.reshape(im.shape[0], im.shape[1], 1)
im = im.transpose((2,0,1)) # convert to CxHxW
data[in_idx]=im
for in_idx, in_ in enumerate(paths_lbl):
print in_idx,in_
f=h5py.File(in_,'r')
mat=f['image'].value
im=np.array(mat,dtype=np.float32)
#im = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)
#im = im[:,:,::-1] #switch from RGB to BGR
im = im.reshape(im.shape[0], im.shape[1], 1)
im = im.transpose((2,0,1)) # convert to CxHxW
label[in_idx]=im
h5_train = os.path.join(path_dst, 'train_data.h5')
with h5py.File(h5_train,'w') as H:
H.create_dataset( 'data', data=data ) # note the name X given to the dataset!
H.create_dataset( 'label', data=label ) # note the name y given to the dataset!
text_train = os.path.join(path_dst, 'train-path.txt')
with open(text_train,'w') as L:
L.write(h5_train) # list all h5 files you are going to use
train_img_paths = './train_img.txt' #text file of paths to images
train_label_paths = './train_label.txt' #text file of paths to label images (ground truth)
train_img_hdf5 = '/home/ss/workspace/create_hdf5/' # Your path to h5 file
st='Creating Training Data HDF5 File .....'
img_to_hdf5(train_img_paths, train_label_paths,train_img_hdf5,st)
print('DONE...')

Displaying PNG in matplotlib.pyplot framework in python 2.7

I am pulling PNG images from Jupyter Notebooks and manage to display with IPython.display.Image but not with matplotib.pyplot.plt. What am I missing? I use python 2.7.
I am using the following algorithm:
To open the notebook JSON content I do:
import nbformat
notebook_ = nbformat.read(file_notebook, 4)
After retrieving the relevant cell information I pull the png information from it using:
def cell_to_image(cell, out_value_item_number=1):
if "execution_count" in cell.keys(): # i.e version >=4
return cell["outputs"][out_value_item_number]['data']['image/png']
elif "prompt_number" in cell.keys(): # i.e version < 4
return cell["outputs"][out_value_item_number]['png']
return None
cell_image = cell_to_image(cell)
The first few characters of cell_image (which is unicode) looks like:
iVBORw0KGgoAAAANSUhEUgAAA64AAAFMCAYAAADLFeHSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\n
AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8jef/x/HXyTjZiYQkCGrU3ruR0tr9oq2qGtGo0dbe
\nm5pVlJpFUSMoVb6UoEZ/lCpatWuPUiNEEiMDmef3R75OexonJKUO3s/HI4/mXPd1X/d1f+LRR965
\n7/u6DSaTyYSIiIiIiIiIjbJ70hMQERERERERyYiCq4iIiIiIiNg0BVcRERERERGxaQquIiIiIiIi
\nYtMUXEVERERERMSmKbiKiIiIiIiITVNwFRGRxyIkJIRixYqxfv36+24/e/YsxYoVo3jx4v/yzGxb
\naGgoderUIS4uDoBdu3bRsmVLKlasyCuvvMKgQYOIjo622CcsLIyGDRtSunRp6tSpw8KFC62OW7p0
\naRo2bJju53Lnzh1GjRrFyy+/TNmyZWnRogW//fbbQ835q6++olGjRpQvX5769eszc+ZMkpOTzdtT
\nU1OZNGkSNWrUoHTp0jRp0oTdu3enGyc2NpZOn
I can easily plot in my Jupityer notebook using
from IPython.display import Image
Image(cell_image)
And now to my question:
How can I manipulate cell_image to be plt.subplot friendly?
(Assuming import matplotlib.pyplot as plt).
I realise that plt.imshow wouldn't work because this would require an array, which is not my case (which is a string, as far as I understand).
If you have your image string representation in a variable string_rep, the following code should work.
from io import BytesIO
import matplotlib.image as mpimage
import matplotlib.pyplot as plt
with BytesIO(string_rep.decode('base64')) as byte_rep:
image = mpimage.imread(byte_rep)
plt.imshow(image)

Read Multiple images on a folder in OpenCv (python)

I want to read multiple images on a same folder using opencv (python). To do that do I need to use for loop or while loop with imread funcion? If so, how? please help me...
I want to get images into an array and then processed them one at a time through a loop.
import glob
import cv2
images = [cv2.imread(file) for file in glob.glob("path/to/files/*.png")]
This will get all the files in a folder in onlyfiles. And then it will read them all and store them in the array images.
from os import listdir
from os.path import isfile, join
import numpy
import cv2
mypath='/path/to/folder'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
import glob
import cv2 as cv
path = glob.glob("/path/to/folder/*.jpg")
cv_img = []
for img in path:
n = cv.imread(img)
cv_img.append(n)
This one has better time efficiency.
def read_img(img_list, img):
n = cv2.imread(img, 0)
img_list.append(n)
return img_list
path = glob.glob("*.bmp") #or jpg
list_ = []`
cv_image = [read_img(list_, img) for img in path]
import cv2
from pathlib import Path
path=Path(".")
path=path.glob("*.jpg")
images=[]`
for imagepath in path.glob("*.jpg"):
img=cv2.imread(str(imagepath))
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img=cv2.resize(img,(200,200))
images.append(img)
print(images)
def flatten_images(folder): # Path of folder (dataset)
images=[] # list contatining all images
for filename in os.listdir(folder):
print(filename)
img=plt.imread(folder+filename) # reading image (Folder path and image name )
img=np.array(img) #
img=img.flatten() # Flatten image
images.append(img) # Appending all images in 'images' list
return(images)
Here is how I did it without using glob, but with using the os module instead, since I could not get it to work with glob on my computer:
# This is to get the names of all the files in the desired directory
# Here I assume that they are all images
original_images = os.listdir('./path/containing/images')
# Here I construct a list of relative path strings for each image
original_images = [f"./path/containing/images/{file_name}" for file_name in original_images]
original_images = [cv2.imread(file) for file in original_images]

How to read & export certain files from a Python GUI-prompted directory?

OK guys,
I'm currently working on a file reading and processing with Python & OpenCV cs' GUI feature. The feature will prompt the user to select a directory path for a folder containing 340 JPEG images, which I labelled them as "frame1" to "frame340". Then, I want to select several frames, process them, and save the processed ones in a different directory.
My big issue is, I'm trying to get only frame87, frame164, and frame248 from this folder with 340 images, and Python just keep returning error that claimed "directory name is invalid", like this:
Traceback (most recent call last):
File "C:\Users\maxwell_hamzah\Documents\Python27\imageReadBeta.py", line 25, in <module>
imgRead = os.listdir(str(dirname) + "/frame"+ str(i) + ".jpg")
WindowsError: [Error 267] The directory name is invalid: 'C:/Users/maxwell_hamzah/Documents/Python27/toby arm framed/frame87.jpg/*.*'
To help familiarize with the situation, here's what my work looks like:
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import color, data, restoration
import Tkinter, tkFileDialog
# first, we setup the Tkinter features for file-reading
root = Tkinter.Tk()
root.withdraw()
# prompt user to ask about the file directory
dirname = tkFileDialog.askdirectory\
(parent=root,initialdir="/",title='Pick FRAMES directory')
X = [] # initiate an array to store read images
frameIndex = [87, 163, 248] #this index is which frames we are interested in
imgRead = ""
temp = []
# we begin to read only frame87, frame163, and frame248
for i in frameIndex:
imgRead = os.listdir(str(dirname) + "/frame"+ str(i) + ".jpg")
temp = cv2.imread(imgRead, -1)
X.append(temp)
I'm totally stuck on how to fix this bug on especially the for loop part, where the error comes from. Python keeps freeking out on the imgRead variable claiming that the directory is invalid. Plus, I'm also wondering on how to "export" processed files to other directories (e.g. saving processed images from "My Pictures" to "My Music")
Really appreciate your help, guys.
Maxwell
In the last block, you call a method to list files, which is expecting a directory, but you pass it a file path. That's a bug, and actually you don't need that here in the first place:
for i in frameIndex:
imgRead = "{0}/frame{1}.jpg".format(dirname, i)
temp = cv2.imread(imgRead, -1)
X.append(temp)
As to moving files in Python, that's a pretty classic need, there's plenty of doc out there. One example.