Create a binary completeness map - python

I'm touching the goal of my project, but I'm getting a problem on : How I can create a completeness map ?
I have lots of data, a field with maybe 500.000 objects which are represented by dots in my plot with different zoom :
I would like to create a mask, I mean, cut my plot in tiny pixels, and say if I have an object in this pixel, I get the value : 1 (black for example) elif, I have not object in my pixel, I get the value : 0 (white for example).
I'll create a mask and I could divide each field by this mask.
The problem is that I don't know how I can process in order to make that :/
I create a first script in order to get a selection on my data. This one :
#!/usr/bin/python
# coding: utf-8
from astropy.io import fits
from astropy.table import Table
import numpy as np
import matplotlib.pyplot as plt
###################################
# Fichier contenant le champ brut #
###################################
filename = '/home/valentin/Desktop/Field52_combined_final_roughcal.fits'
# Ouverture du fichier à l'aide d'astropy
field = fits.open(filename)
print "Ouverture du fichier : " + str(filename)
# Lecture des données fits
tbdata = field[1].data
print "Lecture des données du fits"
###############################
# Application du tri sur PROB #
###############################
mask = np.bitwise_and(tbdata['PROB'] < 1.1, tbdata['PROB'] > -0.1)
new_tbdata = tbdata[mask]
print "Création du Masque"
#################################################
# Détermination des valeurs extremales du champ #
#################################################
# Détermination de RA_max et RA_min
RA_max = np.max(new_tbdata['RA'])
RA_min = np.min(new_tbdata['RA'])
print "RA_max vaut : " + str(RA_max)
print "RA_min vaut : " + str(RA_min)
# Détermination de DEC_max et DEC_min
DEC_max = np.max(new_tbdata['DEC'])
DEC_min = np.min(new_tbdata['DEC'])
print "DEC_max vaut : " + str(DEC_max)
print "DEC_min vaut : " + str(DEC_min)
#########################################
# Calcul de la valeur centrale du champ #
#########################################
# Détermination de RA_moyen et DEC_moyen
RA_central = (RA_max + RA_min)/2.
DEC_central = (DEC_max + DEC_min)/2.
print "RA_central vaut : " + str(RA_central)
print "DEC_central vaut : " + str(DEC_central)
print " "
print " ------------------------------- "
print " "
##############################
# Détermination de X et de Y #
##############################
# Creation du tableau
new_col_data_X = array = (new_tbdata['RA'] - RA_central) * np.cos(DEC_central)
new_col_data_Y = array = new_tbdata['DEC'] - DEC_central
print 'Création du tableau'
# Creation des nouvelles colonnes
col_X = fits.Column(name='X', format='D', array=new_col_data_X)
col_Y = fits.Column(name='Y', format='D', array=new_col_data_Y)
print 'Création des nouvelles colonnes X et Y'
# Creation de la nouvelle table
tbdata_final = fits.BinTableHDU.from_columns(new_tbdata.columns + col_X + col_Y)
# Ecriture du fichier de sortie .fits
tbdata_final.writeto('{}_{}'.format(filename,'mask'))
print 'Ecriture du nouveau fichier mask'
field.close()
Ok, it's working ! But now, the second part is this to the moment :
###################################################
###################################################
###################################################
filename = '/home/valentin/Desktop/Field52_combined_final_roughcal.fits_mask'
print 'Fichier en cours de traitement' + str(filename) + '\n'
# Ouverture du fichier à l'aide d'astropy
field = fits.open(filename)
# Lecture des données fits
tbdata = field[1].data
figure = plt.figure(1)
plt.plot (tbdata['X'], tbdata['Y'], '.')
plt.show()
Do you have any idea how process ?
How I can cut my plot in tiny bin ?
Thank you !
UPDATE :
After the answer from armatita, I updated my script :
###################################################
###################################################
###################################################
filename = '/home/valentin/Desktop/Field52_combined_final_roughcal.fits_mask'
print 'Fichier en cours de traitement' + str(filename) + '\n'
# Opening file with astropy
field = fits.open(filename)
# fits data reading
tbdata = field[1].data
##### BUILDING A GRID FOR THE DATA ########
nodesx,nodesy = 360,360 # PIXELS IN X, PIXELS IN Y
firstx,firsty = np.min(tbdata['X']),np.min(tbdata['Y'])
sizex = (np.max(tbdata['X'])-np.min(tbdata['X']))/nodesx
sizey = (np.max(tbdata['Y'])-np.min(tbdata['Y']))/nodesy
grid = np.zeros((nodesx+1,nodesy+1),dtype='bool') # PLUS 1 TO ENSURE ALL DATA IS INSIDE GRID
# CALCULATING GRID COORDINATES OF DATA
indx = np.int_((tbdata['X']-firstx)/sizex)
indy = np.int_((tbdata['Y']-firsty)/sizey)
grid[indx,indy] = True # WHERE DATA EXISTS SET TRUE
# PLOT MY FINAL IMAGE
plt.imshow(grid.T,origin='lower',cmap='binary',interpolation='nearest')
plt.show()
I find this plot :
So, when I play with the bin size, I can see more or less blank which indicate object or not in my pixel :)

This is usually a process of inserting your data into a grid (pixel wise, or node wise). The following example builds a grid (2D array) and calculates the "grid coordinates" for the sample data. Once it has those grid coordinates (which in true are nothing but array indexes) you can just set those elements to True. Check the following example:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(0,1,1000)
y = np.random.normal(0,1,1000)
##### BUILDING A GRID FOR THE DATA ########
nodesx,nodesy = 100,100 # PIXELS IN X, PIXELS IN Y
firstx,firsty = x.min(),y.min()
sizex = (x.max()-x.min())/nodesx
sizey = (y.max()-y.min())/nodesy
grid = np.zeros((nodesx+1,nodesy+1),dtype='bool') # PLUS 1 TO ENSURE ALL DATA IS INSIDE GRID
# CALCULATING GRID COORDINATES OF DATA
indx = np.int_((x-firstx)/sizex)
indy = np.int_((y-firsty)/sizey)
grid[indx,indy] = True # WHERE DATA EXISTS SET TRUE
# PLOT MY FINAL IMAGE
plt.imshow(grid.T,origin='lower',cmap='binary',interpolation='nearest')
plt.show()
, which results in:
Notice I'm showing an image with imshow. Should I decrease the number of pixels (20,20 = nodesx, nodesy) I get:
Also for a more automatic plot in matplotlib you can consider hexbin.

Related

representation of acp individuals

i would like to reprensent individuals ACP in a graph with colors and label, but it doesn't work i have only color i can't pin up labels and legend, can anyone help me please. here is my result
here is my code
palette = plt.get_cmap("Dark2")
# associe une couleur à chaque continent (cf ci-dessous)
couleurs = dict(zip(dt_df["Bandes"].drop_duplicates(), palette(range(7))))#9 bandes
couleurs
position = dict(zip(couleurs.keys(), range(7))) #9 bandes
#Affichage des points avec une liste de couleurs
dt_df.plot.scatter(x = "CP1", y = "CP2", c = [couleurs[p] for p in dt_df["Bandes"]])
# boucle pour afficher la légende
for cont, coul in couleurs.items():
plt.scatter(3, position[cont] / 3 + 2.15, c = [coul], s = 20)
plt.text(3.2, position[cont] / 3 + 2, cont)
# plt.legend()
plt.show()
plt.xlabel("Dimension 1 ")
plt.ylabel("Dimension 2")
plt.suptitle("Premier plan factoriel")
plt.show()

More efficient way to manipulate large dataframe

It's my first real Python script, so feel free to make comments in order to improve my code.
The purpose of this script is to extract 2 Oracle tables with Python, store them in a dataframe and then join them with pandas.
But for queries returning more than 500k lines I feel that it is slow. Do you know why?
import pandas as pd
from datetime import date
from sqlalchemy import create_engine
import cx_Oracle, time
import pandas as pd
import config
## Variable pour le timer
start = time.time()
## User input en ligne de commande
year = input('Saisir une annee : ')
month = input('Saisir le mois, au fomat MM : ')
societe_var = input('SA (APPLE,PEACH,BANANA,ANANAS,ALL) : ')
## SA + BU correspondantes aux SA
sa_list = ['APPLE','PEACH','BANANA','ANANAS']
bu_list_MERE = ['006111','1311402','1311403','1311404','1340115','13411106','1311407','1111','6115910','1166157','6811207','8311345','1111','1188100','8118101','8811102','8810113','8811104','8118105','8811106','8811107','8118108','1111']
bu_list_GARE = ['131400','310254']
bu_list_VOYA = ['0151100','1110073','1007115','1311335','1113340','1311341','1113342','1331143']
bu_list_RESO = ['1211345','13111345','11113395','73111345']
#Permet de pointre vers la bonne liste en fonction de la SA saisie
bu_list_map = {
'APPLE': bu_list_APPLE,
'PEACH': bu_list_PEACH,
'BANANA': bu_list_BANANA,
'ANANAS' : bu_list_ANANAS
}
if societe_var == 'ALL' :
print('non codé pour le moment')
elif societe_var in sa_list :
bu_list = bu_list_map.get(societe_var)
sa_var = societe_var
i=1
for bu in bu_list :
start_bu = time.time()
## On vient ici charger la requête SQL avec les bonnes variables pour gla_va_parametre -- EPOST
query1 = open('gla_va_parametre - VAR.sql',"r").read()
query1 = query1.replace('#ANNEE',"'" + year + "'").replace('%MOIS%',"'" + month + "'").replace('%SA%',"'" + societe_var + "'").replace('%BUGL%',"'" + bu + "'").replace('%DIVISION%','"C__en__PS_S1_D_OP_UNIT13".OPERATING_UNIT')
## On vient ici charger la requête SQL avec les bonnes variables pour cle-gla_tva -- FPOST
query2 = open('cle-gla_tva - VAR.sql',"r").read()
query2 = query2.replace('#ANNEE',"'" + year + "'").replace('%MOIS%',"'" + month + "'").replace('%SA%',"'" + societe_var + "'").replace('%BUGL%',"'" + bu + "'").replace('%DIVISION%','OPERATING_UNIT')
# Param de connexion
connection_EPOST = cx_Oracle.connect(user=config.user_EPOST, password=config.password_EPOST, dsn=config.host_EPOST, )
connection_FPOST = cx_Oracle.connect(user=config.user_FPOST, password=config.password_FPOST, dsn=config.host_FPOST, )
## Récup partie EPOST
with connection_EPOST :
# On déclare une variable liste vide
dfl = []
# On déclare un DataFrame vide
dfs = pd.DataFrame()
z=1
# Start Chunking
for chunk in pd.read_sql(query1, con=connection_EPOST,chunksize=25000) :
# Start Appending Data Chunks from SQL Result set into List
dfl.append(chunk)
print('chunk num : ' + str(z))
z = z + 1
# Start appending data from list to dataframe
dfs = pd.concat(dfl, ignore_index=True)
print('param récupéré')
## Récup partie FPOST
with connection_FPOST :
# On déclare une variable liste vide
df2 = []
# On déclare un DataFrame vide
dfs2 = pd.DataFrame()
# Start Chunking
for chunk in pd.read_sql(query2, con=connection_FPOST,chunksize=10000) :
# Start Appending Data Chunks from SQL Result set into List
df2.append(chunk)
# Start appending data from list to dataframe
dfs2 = pd.concat(df2, ignore_index=True)
print('clé récupéré')
print('Début de la jointure')
jointure = pd.merge(dfs,dfs2,how='left',left_on=['Code_BU_GL','Code_division','Code_ecriture','Date_comptable','Code_ligne_ecriture','UNPOST_SEQ'],right_on=['BUSINESS_UNIT','OPERATING_UNIT','JOURNAL_ID','JOURNAL_DATE','JOURNAL_LINE','UNPOST_SEQ']).drop(columns= ['BUSINESS_UNIT','OPERATING_UNIT','JOURNAL_ID','JOURNAL_DATE','JOURNAL_LINE'])
jointure.to_csv('out\gla_va_'+year+month+"_"+societe_var+"_"+bu+"_"+date.today().strftime("%Y%m%d")+'.csv', index=False, sep='|')
print('Fichier ' + str(i) + "/" + str(len(bu_list)) + ' généré en : '+ str(time.time() - start_bu)+' secondes')
i = i + 1
print("L'extraction du périmètre de la SA " + societe_var + " s'est effectué en :" + str((time.time() - start)/60) + " min" )

abaqus python, why are the extracted displacement values different?

I am trying to use abaqus-ython scripting to extract the nodal coordinates.
in doing so i first extract the original nodal positions and then add the displacement value.
But for 1 of my abaqus models i notice that the displacement values i extract are different from the ones i find in abaqus (see attached pictures)
i have no idea how or why this is happening.
Can someone helpe me?
You can find my code below.
for ODBname in os.listdir("D:/r0333338/Documents/MP/nodal_files_genereren/OBD"): # directory van waar alle .odb bestanden zitten, hier worden ze allemaal
print 'Current File: '+ODBname #checken welke file er gebruikt wordt
ODBnamefull = 'D:/r0333338/Documents/MP/nodal_files_genereren/OBD/'+ODBname # Volledig pad naar de .odb file. ander wordt de file in de default work directory gezocht.
odb = openOdb(path=ODBnamefull) #openen van het ODB bestand
ODBalleenNaam = ODBname.rstrip('.odb') #om .odb weg te knippen
NodalName = ODBalleenNaam + '-nodal.txt' #naam ven het te schrijven bestand
for name, instance in odb.rootAssembly.instances.items(): #'name' is naam van elke part van in de assembly, zo kan de nodal coordinaten van het onvervormde testobject (part) achterhaald worden
print name
type(name)
name2 = 'DISK-1'
if name == name2:
numNodesTotal = len( instance.nodes ) #aantal nodes
frame = odb.steps[ 'Step-1' ].frames[-1] #informatie van de laatste frame van Step-1 gebruiken
dispField = frame.fieldOutputs['U'] #verplaatsingsveld van laatste frame van step-1
print 'total numer of nodes: '+ str(numNodesTotal) #checken hoeveel nodes er zijn
for i in range( numNodesTotal ): #voor elke node :
curNode = instance.nodes[i] #informatie van de huidige node
#print curNode.label #nummer van de huidige node
#2D verplaatsing omzetten naar 3D verplaatsing
U1 = dispField.values[i].data[0] #X-verplaatsing aan U1 geven
U2 = dispField.values[i].data[1] #Y-verplaatsing aan U2 geven
array = [] #maken van een lege array voor invullen van de coordinaten
array.append(U1) #X-verplaatsing toevoegen
array.append(U2) #Y-verplaatsing toevoegen
array.append(0) #Z-verplaatsing toevoegen
print 'node: '
print curNode.label
print 'displacement: '
print array #checken van 3D verplaatsing
print 'coordinates: '
print curNode.coordinates
odb.close()
else:
print 'name is not DISK-1 but: ' + str(name)
Abaqus displacement
python extracted displacement
you should pull the node label directly from the field data:
curNodeLabel=dispField.values[i].nodeLabel
you then need to use that to get the node:
curNode=instance.getNodeFromLabel(curNodeLabel)
don't assume the node indexing is the same as the field data indexing.
I'd further for consistency make the for loop:
for i in range( len(dispField.values) ):
Despite the title, I am assuming that you want the final coordinates and not the displacement. As Daniel F mentioned, you should add COORDS as a field output. In that case the code bellow should be helpful.
def findCoordSet(OdbName,StepName,InstanceName,SetName):
"""
This ODB reading script does the following:
-Retrieves coordinates at SetName
"""
Coordinates={'x':[],'y':[]}
# Open the output database.
odbName = OdbName + '.odb'
odb = visualization.openOdb(odbName)
lastFrame = odb.steps[StepName].frames[-1]
coordset = odb.rootAssembly.instances[InstanceName.upper()].nodeSets[SetName.upper()]
# Retrieve Y-displacements at the splines/connectors
dispField = lastFrame.fieldOutputs['COORD']
dFieldpTip = dispField.getSubset(region=coordset)
for i in range(len(dFieldpTip.values)):
Coordinates['x'].append(dFieldpTip.values[i].data[0])
Coordinates['y'].append(dFieldpTip.values[i].data[1])
odb.close()
return Coordinates

Extract nodal coordinates from the deformed testsubject (abaqus-python)

I am trying to make a python script to extract the nodal coordinates from the ODB file (from abaqus).
So far i have come up with the code attached below (don't mind the extra information i put behind the #, sometimes it's just so i can keep track of what i'm doing)
The problem is that the coordinates i extract are those from the undeformed test subject. and i need the coordinates of the nodes from the deformed test subject.
Can somebody help me with how i reach this information using python code?
from abaqus import *
from abaqusConstants import *
import __main__
import section
import regionToolset
import displayGroupMdbToolset as dgm
import part
import material
import assembly
import step
import interaction
import load
import mesh
import optimization
import job
import sketch
import visualization
import xyPlot
import displayGroupOdbToolset as dgo
import connectorBehavior
import shutil
import os
import sys
from odbAccess import openOdb
for ODBname in os.listdir("D:/r0333338/Documents/MP/nodal_files_genereren/OBD"): # this is where all your ODB files are located #t hier wordt ook de odb file gekozen die geopend zal worden
SUBname = ODBname[1:3] # My subject ID is saved in the ODB name - this helps me create the file #woerdt er hier de 3e tot6e letter van de ODBname gepakt ?
print 'Current File: '+ODBname #voor check welke file er gebruikt wordt #t (ZIT '.odb' hier bij in ?)
ODBnamefull = 'D:/r0333338/Documents/MP/nodal_files_genereren/OBD/'+ODBname # Full path to the ODB file, otherwise abaqus tries to find it in default work directory
odb = openOdb(path=ODBnamefull) #open the ODB file
assembly = odb.rootAssembly #t declareren van assembly?
session.viewports['Viewport: 1'].odbDisplay.setFrame(step=0, frame=1)
numNodes = 0 #t num nodes op nul zetten
f = open("D:/r0333338/Documents/MP/nodal_files_genereren/ODBoutput/nodal.txt", "w") #indien het bestand al bestaat moet er "a" staan ipv "w"
for name, instance in assembly.instances.items(): #t 'name' is naam van elke part van in de assembly ?
n = len(instance.nodes) #t tellen van hoeveelheid nodes
print 'Number of nodes of instance %s: %d' % (name, n) #moet niet in de file staan, kan eigenlijk weggelaten worden. (maar is een goede check?)
numNodes = numNodes + n #tellen van totaal aantal nodes (globaal over alle parts) maar is eigenlijk niet nodig?
f.write( "*Part, name=Part-1" + "\n")#moet erin staan volgens de MatchId regels
f.write( "*Nodes" + "\n") #moet erin staan volgens de MatchId regels
if instance.embeddedSpace == THREE_D: #indien het 3D is
print ' X Y Z' #moet niet in de file staan, maar is een goede check om te zien waar we zitten
for node in instance.nodes:
#print node #printen van node
f.write( str(node.label) + ";" ) #schrijven van nodenummer
f.write(str(node.coordinates[0]) + ";" + str(node.coordinates[1]) + ";" + str(node.coordinates[2]) + "\n") #schrijven van coordinaten [X;Y;Z] en enter
else: #indien het 2D is
print ' X Y' ';0' #moet niet in de file staan, maar is een goede check om te zien waar we zitten
for node in instance.nodes:
#print node #printen van node
f.write( str(node.label) + ";" )
f.write(str(node.coordinates[0]) + ";" + str(node.coordinates[1]) + ";" + str(node.coordinates[2]) + "\n") #schrijven van coordinaten [X;Y;Z] en enter
f.write( "*End Part" ) #moet erin staan volgens de MatchId regels
f.close()
get the displacement field:
u=odb.steps['Step-1'].frames[-1].fieldOutputs['U']
then u.values is a list of all nodal values:
u.values[i].data -> array of (ux,uy,uz)
u.values[i].nodeLabel -> node label
then you grab the original position like this:
instance.getNodeFromLabel(u.values[i].nodeLabel).coordinates
You can also directly get the deformed coordinate as a field output, but you need to request COORD output when you run the analysis.

How to use Python re module to replace \n with nothing in a single file

Regards to all.
I'm developing a Image compression system using Python Image Library. The basic workflow is:
Read all images of a certain directory with : find /opt/images -name *.jpg > /opt/rs/images.txt
Read this file and storage the result in a Python list
Iterate the list, create a Image object and passing like a argument to
the compress function
and, copy the resulting image on a certain directory which is buit
depending of the name of the image.
A example:
/opt/buzon/17_499999_1_00000000_00000999_1.jpg
This is the real name of the image:
The final result is:
17_499999.jpg
The ouput directory is: /opt/ftp
and should be storaged on this way:
1- first partition
00000000 - second partition
00000999 - third partition
1- this flag if to decide if we have to compress this image or not (1 is False, 0 is True)
For that reason the final path of the image is:
/opt/ftp/1/00000000/00000999/17_499999.jpg for the original copy
/opt/ftp/1/00000000/00000999/17_499999_tumb.jpg
Now, Where is the problem. When I read the file where I storage the result of the find command, each line of the file has the \n character.
How I can replace with regular expressions this?
The completed source code is this. Any suggests is welcome.
import Image, os ,sys, re, subprocess, shlex
import ConfigParser
from symbol import except_clause
CONFIG_FILE = "/opt/scripts/config.txt"
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILE)
entry_dir = config.get('directories', 'entry_dir')
out_dir = config.get('directories', 'out_dir')
def resize(img, box, fit, out):
'''Downsample the image.
#param img: Un objeto de la clase Imagen
#param box: tuple(x, y) - El recuadro de la imagen resultante
#param fix: boolean - el corte de la imagen para llenar el rectangulo
#param out: objeto de tipo fichero - guarda la imagen hacia la salida estandar
'''
# prepara la imagen con un factor de 2, 4, 8 y el algoritmo mas rapido
factor = 1
while img.size[0]/factor > 2*box[0] and img.size[1]*2/factor > 2*box[1]:
factor *=2
if factor > 1:
img.thumbnail((img.size[0]/factor, img.size[1]/factor), Image.NEAREST)
# Aqui se calcula el rectangulo optimo para la imagen
if fit:
x1 = y1 = 0
x2, y2 = img.size
wRatio = 1.0 * x2/box[0]
hRatio = 1.0 * y2/box[1]
if hRatio > wRatio:
y1 = y2/2-box[1]*wRatio/2
y2 = y2/2+box[1]*wRatio/2
else:
x1 = x2/2-box[0]*hRatio/2
x2 = x2/2+box[0]*hRatio/2
# Este metodo es para manipular rectangulos de una determinada imagen
# definido por 4 valores que representan las coordenadas: izquierda,
# arriba, derecha y abajo
img = img.crop((x1,y1,x2,y2))
# Le damos el nuevo tamanno a la imagen con el algoritmo de mejor calidad(ANTI-ALIAS)
img.thumbnail(box, Image.ANTIALIAS)
return img
def removeFiles(directory):
"""
Funcion para borrar las imagenes luego de ser procesadas
"""
for f in os.listdir(directory):
path = os.path.abspath(f)
if re.match("*.jpg", path):
try:
print "Erasing %s ..." % (path)
os.remove(path)
except os.error:
pass
def case_partition(img):
"""
#desc: Esta funcion es la que realmente guarda la imagen en la
particion 0
#param: imagen a guardar
#output_dir: Directorio a guardar las imagenes
"""
nombre_imagen = img
nombre_import = os.path.splitext(nombre_imagen)
temp = nombre_import[0]
nombre_real = temp.split('_')
if nombre_real[4] == 0:
ouput_file = nombre_real[0] + nombre_real[1] + ".jpg"
output_dir = out_dir + "/%d/%d/%d/" % (nombre_real[2], nombre_real[3], nombre_real[4])
if os.path.isdir(output_dir):
os.chdir(output_dir)
img.save(output_file, "JPEG", quality=75)
else:
create_out_dir(output_dir)
os.chdir(output_dir)
img.save(output_file)
else:
print "Esta imagen sera comprimida"
img = resize(img, 200, 200, 200) ## FIXME Definir el tamano de la imagen
# Salvamos la imagen hacia un objeto de tipo file
# con la calidad en 75% en JPEG y el nombre definido por los especialistas
# Este nombre no esta definido......
# FIXME
output_file = nombre_real[0] + nombre_path[1] + "_.jpg"
output_dir = out_dir + "/%d/%d" % (nombre_real[2], nombre_real[3], nombre_real[4])
if os.path.isdir(output_dir):
os.chdir(out)
img.save(output_file, "JPEG", quality=75)
else:
create_out_dir(output_dir)
os.chdir(output_dir)
img.save(output_file, "JPEG", quality=75)
if __name__ == "__main__":
find = "find %s -name *.jpg > /opt/scripts/images.txt" % entry_dir
args = shlex.split(find)
p = subprocess.Popen(args)
f = open('/opt/scripts/images.txt', "r")
images = []
for line in f:
images.append(line)
f.close()
for i in images:
img = Image.open(filename) # Here is the error when I try to open a file
case_partition(img) # with the format '/opt/buzon/15_498_3_00000000_00000000_1.jpg\n'
# and the \n character is which I want to replace with nothing
removeFiles(entry_dir) #
#
Regards
Assuming s is the string with the carriage return, you may use s.strip("\n") to remove carriage returns at the corners of the string. No need for regular expressions there.
I guess the relevant code lines are these:
for line in f:
images.append(line)
to remove the \n, you can simply call strip() on the string:
for line in f:
images.append(line.strip())
there are many ways to do that without using regexp
simpliest and correct one is use images.append(line.rstrip("\n")), also you can use images.append(line[:-1])
Also you can use glob() module instead call 'find' command through shell. It will return result as python list without having to use the files. ex: images=glob.glob("*.jpg"). http://docs.python.org/library/glob.html?highlight=glob

Categories