ValueError: Received a null pointer - python

I'm writing a code to convert GeoTiff files to png.
Pillow didn't work for me. Hence I am using gdal. Am I missing something in the code?
The code is as below:
from osgeo import gdal
import os
options_list = ['-ot Byte','-of PNG']
options_string = " ".join(options_list)
in_tif = r"E:\some\path\tif_folder"
out_png = r"E:\some\other\path\png_folder"
list_tif = os.listdir(in_tif)
for x in list_tif:
in_tif = in_tif + "\\" + x
out_png = out_png + "\\" + x
gdal.Translate(out_png,in_tif,options=options_string)

It actually gave me a file with .tif extension which I previously failed to notice. Thus, I used replace function to change tif to png. There was also very basic flaw in the for loop because of which the file was kept getting added.
new code which works:
from osgeo import gdal
import os
options_list = ['-ot Byte','-of PNG']
options_string = " ".join(options_list)
in_tif = r"E:\some\path\tif_folder"
out_png = r"E:\some\other\path\png_folder"
list_tif = os.listdir(in_tif)
for x in list_tif:
indi_tif = in_tif+"\\" + x
print(in_tif)
indi_png = out_png + "\\" + x
try:
gdal.Translate(indi_png.replace(".tif",".png"),indi_tif,options=options_string)
except:
print("Failed files,",indi_tif)

Related

Executing Python Script in Terminal When Outside of Script's Directory

I have a Python script that prompts for text input, searches an online Korean dictionary, and then downloads MP3 audio files for the words found. I use the script to help me make Anki flashcards with audio. The script is originally from this post on reddit.
I can execute the script from the terminal while in the directory that the script is stored in. However, when I am in a different directory and execute the script by calling its full path, the script appears to run but does not find any words or download any MP3s. I cannot figure out why the script fails to execute correctly when I call it from a different directory.
The script is stored in the downloads folder on my Mac /Users/matt/Downloads
So, when I run the following commands, it works:
cd Downloads
python3 naver.py
However, when I run the following, the script executes, but doesn't download any MP3s:
python3 /Users/matt/Downloads/naver.py
The full Python script is here:
import urllib.request, json, codecs, math, time
def searchWords(koreanWords):
url = ('https://ko.dict.naver.com/api3/koko/search?' + urllib.parse.urlencode({'query': koreanWords}) + '&range=word&page=1')
response = urllib.request.urlopen(url)
reader = codecs.getreader("utf-8")
jsonInfo = json.load(reader(response))
pageCount = jsonInfo["pagerInfo"]["totalPages"]
searchData = jsonInfo["searchResultMap"]["searchResultListMap"]["WORD"]["items"]
for pageCountInc in range(0, pageCount):
if pageCountInc != 0:
url = ('https://ko.dict.naver.com/api3/koko/search?' + urllib.parse.urlencode({'query': koreanWords}) + '&range=word&page=' + str(pageCountInc+1))
response = urllib.request.urlopen(url)
reader = codecs.getreader("utf-8")
jsonInfo = json.load(reader(response))
searchData = jsonInfo["searchResultMap"]["searchResultListMap"]["WORD"]["items"]
for z in range (0, len(searchData)):
if searchData[z]["handleEntry"] in unchangedWordList:
if searchData[z]["searchPhoneticSymbolList"]:
if searchData[z]["searchPhoneticSymbolList"][0]["phoneticSymbolPath"] != "":
timesDownloaded[unchangedWordList.index(searchData[z]["handleEntry"])] += 1
mp3Link = searchData[z]["searchPhoneticSymbolList"][0]["phoneticSymbolPath"]
if mp3Link not in mp3Links:
mp3Links.append(mp3Link)
urllib.request.urlretrieve(mp3Link, searchData[z]["handleEntry"] + str(timesDownloaded[unchangedWordList.index(searchData[z]["handleEntry"])]) + ".mp3")
time.sleep(.3)
def parseWords(listOfWords):
for x in range(0, math.floor(len(listOfWords)/10)):
tempWords = []
for y in range(0, 10):
tempWords.append(listOfWords[x*10+y])
print("Searching: " + str(x+1) + "/" + str(math.ceil(len(listOfWords)/10)))
searchWords(tempWords)
tempWords = []
for y in range(math.floor(len(listOfWords)/10)*10+1, len(listOfWords)):
tempWords.append(listOfWords[y])
print("Searching: " + str((math.ceil(len(listOfWords)/10))) + "/" + str(math.ceil(len(listOfWords)/10)))
searchWords(tempWords)
unfoundWords = []
unchangedWordList = []
timesDownloaded = []
mp3Links = []
wordInputs = unchangedWordList = input('Enter Words: ').split()
timesDownloaded = [0] * len(unchangedWordList)
parseWords(wordInputs)
for z in range(0, len(timesDownloaded)):
if(timesDownloaded[z] == 0):
unfoundWords.append(unchangedWordList[z])
if unfoundWords:
print(",".join(str(x) for x in unfoundWords) + " could not be found.")
print("Rerunning individual searches for unfound words.")
print(unfoundWords)
oldUnfoundWords = unfoundWords
unfoundWords = []
for x in range(0, len(oldUnfoundWords)):
print("Searching: " + str(x+1) + "/" + str(len(oldUnfoundWords)))
searchWords(oldUnfoundWords[x])
for z in range(0, len(timesDownloaded)):
if(timesDownloaded[z] == 0):
unfoundWords.append(unchangedWordList[z])
if unfoundWords:
print(",".join(str(x) for x in unfoundWords) + " could not be found.")
To answer question of how to save to a specific folder use pathlib to construct the path to MP3 folder.
import os
from pathlib import Path
# Create parent folder
mp3DIR = os.path.join(Path.home(),'Music')
basename = searchData[z]["handleEntry"]
+ str(timesDownloaded[unchangedWordList.index(searchData[z]["handleEntry"])]) + ".mp3"
urllib.request.urlretrieve(mp3Link, os.path.join(mp3Dir, basename))
The reason is the following:
Your python file runs in your current directory. So, when you run this: python3 /Users/matt/Downloads/naver.py, it either runs and saves the mp3 files in the current directory, or it doesn't save anything at all if it doesn't have the permissions to.

Issues with python path

I am working on a program in Arcgis however, I have a python issue.
While trying to run the following script I am getting an invalid MXD error which seems to suggest that it cannot find the location of my MXD file.
# modified by ESRI for use as a toolbox script tool
## imported sys to get the raw inputs and os to use for path separator
import arcpy, sys, os
# Set OverWrite if files already exist
arcpy.OverWriteOutput = 1
print "Enter folder path:"
mapDoc = raw_input()
#mapDoc = sys.argv[1]
print os.path.dirname(mapDoc)
# set a variable to the full path and file name of the MXD
fullnam = os.path.basename(mapDoc)
# Strip off the MXD file extension and store as string variable for use in the 'out_pdf'
nam = fullnam.strip(".mxd")
print nam
# Commented this out, since it doesnt need to be a parameter when you use the MXD name as the PDF name
##print "Enter save as name:"
##mapName = sys.argv[2]
map = arcpy.mapping
mxd = map.MapDocument(mapDoc)
map_document = mxd
#out_pdf = r"K:\projects" + "\\" + mapName + ".pdf"
#out_pdf = r"K:\projects" + os.sep + mapName + ".pdf"
#out_pdf = os.path.dirname(mapDoc) + os.sep + mapName + ".pdf"
out_pdf = os.path.dirname(mapDoc) + os.sep + nam + ".pdf"
# Set all the parameters as variables here:
data_frame = 'PAGE_LAYOUT'
resolution = "300"
image_quality = "NORMAL"
colorspace = "RGB"
compress_vectors = "True"
image_compression = "DEFLATE"
picture_symbol = 'RASTERIZE_BITMAP'
convert_markers = "False"
embed_fonts = "True"
layers_attributes = "NONE"
georef_info = "False"
# Due to a known issue, the df_export_width and df_export_height must be set to integers in the code:
map.ExportToPDF(map_document, out_pdf, data_frame, 640, 480, resolution, image_quality, colorspace, compress_vectors, image_compression, picture_symbol, convert_markers, embed_fonts, layers_attributes, georef_info)
# This gives feedback in the script tool dialog:
arcpy.GetMessages()
Now please look at the command prompt.
(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>"C:\Us
ers\ant\Documents\MyCensus_Files\Python script\Export2PDF.py"
Enter folder path:
C:\Users\ant\Documents\MyCensus_Files\Python script
C:\Users\ant\Documents\MyCensus_Files
Python script
Traceback (most recent call last):
File "C:\Users\ant\Documents\MyCensus_Files\Python script\Export2PDF.py
", line 22, in <module>
mxd = map.MapDocument(mapDoc)
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\arcobjects\mixins.
py", line 651, in __init__
assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(
89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.
As you can see, the path I enter is different from what is being returned by print and I think that is what's causing my issue. Would appreciate it anyone could assist fixing handling this path error or with a python script that accomplishes converting MXD to PDF.
Regards
If you print out mapDoc right before the line map.MapDocument(mapDoc), you would see that you're trying to pass C:\Users\ant\Documents\MyCensus_Files\Python script as a mxd file. That's a directory, not a mxd file.
Try this:
import arcpy, sys, os
arcpy.OverWriteOutput = 1
mapDoc = os.path.join("C:/", "Users", "ant", "Documents", "MyCensus_Files", "Python_script", "test.mxd")
map = arcpy.mapping
mxd = map.MapDocument(mapDoc)
# ...

Creating multiple *.cfg files Python

I'm working with *.cfg files. The file can be read in a text editor like gedit and has this format:
% some comments
VAR_1= 1
%
% More comments
ANOTHER_VAR= -8
%
% comments again
VAR_THE_COMEBACK= 10
I want to create multiple config files just changing VAR_1= 1....2...3.........10. I manage to import the *cfg file without any new import in python but I'm not getting a way to change just this parameter, saving the file and creating another one with another value for VAR_1.
my code until now is really simple:
import os
os.chdir('/home/leonardo/Desktop')
f = open('file.cfg','r') #if I replace r by w I erase the file ....
a = f.read()
print a.find('1')
a.replace('1','2') #I tried this but. ... :(
f.close()
Any tips ?
Thank you for the help !
Untested code, but you will get the idea:
with open('file.cfg', 'r') as f:
contents_by_line = f.readlines()
for var_index, line in enumerate(contents_by_line):
if line.startswith("VAR_"):
break
else:
raise RuntimeError("VAR_ not found in file")
for var_i, new_cfg_file in ((2,"file2.cfg"),
(3, "file3.cfg")): #add files as you want
with open(new_cfg_file, "w") as fout:
for i, line in enumerate(contents_by_line):
if i == var_index:
fout.write("VAR_1=%d\n" % var_i)
else:
fout.write(line)
Thank you guys for all the help. I changed my approach to the problem, since the lines would be all the 'same', I just created a new line and replaced with a function I found here in stack. I hope it will help someone someday.
This script will automate a series of CFD simulations for my final project at college. It creates a series of folders with some simulation conditions on the name, copies the mesh file to the folder created as well as the new config file, the last line will run the code but I need to work in the base simulation setup to run the script.
The code is just in preliminary fase, I'll change it to be more readable and to be easily modified.
thank you guys !
Any tip is welcome, I'm trying to improve my coding skill : )
the code :::
import fileinput
import os
import shutil
import subprocess
class stuff:
root_folder = '/home/leonardo/Desktop/testzone'
mini_mach = 0.3
maxi_mach = 1.3
number_steps = 3
increment = ((maxi_mach-mini_mach)/number_steps)
config_file = 'inv_NACA0012.cfg'
parameter_1 = 'MACH_NUMBER= 0.8'
parameter_2 = 'CONV_NUM_METHOD_ADJFLOW= JST'
init_pa = 'MACH_NUMBER= ' #use a space after '='
init_pa2 = 'CONV_NUM_METHOD_ADJFLOW= ' #use a space after '='
airfoil = 'NACA0012'
command1 = 'parallel_computation.py -f ' #use space before the last " ' "
command2 = '-n 2'
mesh_file = 'mesh_NACA0012_inv.su2'
class modify:
def replaceAll(self,files,searchExp,replaceExp):
for line in fileinput.input(files, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
mod = modify()
stuff = stuff()
for i in xrange(stuff.number_steps):
mach_name = stuff.airfoil + '_mach_' + `float('%.2f'% stuff.mini_mach)`
folder_name = stuff.root_folder + '/' + mach_name
print 'creating ...' + folder_name
os.makedirs(folder_name)
file_father = stuff.root_folder + '/' + stuff.config_file
shutil.copy2(file_father,folder_name)
mesh_father = stuff.root_folder + '/' + stuff.mesh_file
shutil.copy2(mesh_father,folder_name)
os.chdir(folder_name)
pre_mod_file = mach_name + '.cfg'
os.renames(stuff.config_file,pre_mod_file)
new_parameter_1 = stuff.init_pa + `float('%.2f'% stuff.mini_mach)`
new_parameter_2 = stuff.init_pa2 + `float('%.2f'% stuff.mini_mach)`
mod.replaceAll(pre_mod_file,stuff.parameter_1,new_parameter_1)
mod.replaceAll(pre_mod_file,stuff.parameter_2,new_parameter_2)
stuff.mini_mach += stuff.increment
#subprocess.check_call(stuff.command + pre_mod_file + stuff.command2)

How to save an Image using URL in python/django

If you were to save an Image using it's URL how would you do it ?
Also how do I give the Image a unique file name while saving it.
response = urllib.urlopen(image_url)
file_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
f = open('/media/images/temp/'+file_name, "wb")
f.write(response.read())
f.close()
It throws no error nor saves the file... I'm new to this I have no clue what is going wrong : |
import urllib
import string
import random
import os
filename_charset = string.ascii_letters + string.digits
filename_length = 10
file_save_dir = '/home/user/download/'
filename = ''.join(random.choice(filename_charset)
for s in range(filename_length))
urllib.urlretrieve ("http://www.example.com/image.png",
os.path.join(file_save_dir, filename + '.png'))

Not recognized non existant file

im getting a weird error using this code (part of a class):
from sys import path as workingDIR
from os import system, path
image = '' # some jpeg image data
keep = 0
DIR = workingDIR[0] + '\\image'
if path.isfile(DIR + '.jpeg'): # adding numbers to end of file name like how windows prevents multiple files having the same name
x = 2
while path.isfile(DIR + ' (' + str(x) + ').jpeg'):
x += 1
DIR += ' (' + str(x) + ')'
DIR += '.jpeg'
f = open(DIR, 'w+b')
f.write(image)
f.close()
system(DIR)
system('pause')
if not(keep):
remove(DIR)
cmd is telling me '...\image' is not recognized as an internal or external command, operable program or batch file. (ignore the ...), which doesnt make sense, because DIR has already been changed to ...\image.jpeg and yet it is getting image. what am i doing wrong here?
You have an extra white space in the image filename - "..\image (1).jpeg"
so when you call system(DIR), "..\image" becomes the command and "(1).jpeg" is the first argument.

Categories

Resources