I want to copy all images in a file to another file with different names. But, images order changes during copy. For example, this order like that: B0. jpg,B1.jpg,..,B9.jpg,B10.jpg,B11.jpg..,B30.jpg. I want to change the name of B0.jpg to image1.jpg, B1.jpg to image2.jpg in a similar. But, it changes B0.jpg to B10.jpg and then B11.jpg instead of B1,B2,B3...Because of that images order changed. How can I fix this problem?
The problem is the SO orders the file names using ASCII sorting, as they're strings (it doesn't look to numbers differently). The string "10" is placed before the string "2", because "1" < "2".
Instead of relying on the order, you could do something like this:
imgs = dir('*.jpg');
for i = 1:numel(imgs)
% Change the 'B' to 'image'
newName = strrep(imgs(i).name, 'B', 'image');
% Copy the image
copyfile(imgs(i).name, ['c:\destination\' newName]);
end
Related
I have a set of images located in a folder and I'm trying to read these images and store their names in text file. Where the order of images is very important.
My code as follow:
imagefiles = dir('*jpg');
nfiles = length(imagefiles); % Number of files found
%*******************
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
The images stored in the folder in the following sequence : {1,2,3,4,100,110}.
The problem that Matlab read and write the sequence of images as { 1,100,110,2,3,4}. Which is not the correct order.
How can this be overcome?
I would suggest to use scanf to find the number of the file. For that you have to create a format spec which shows how your file name is built. If it is a number, followed by .jpg, that would be: '%d.jpg'.
You can call sscanf (scan string) on the name's of the files using cellfun:
imagefiles = dir('*jpg');
fileNo = cellfun(#(x)sscanf(x,'%d.jpg'),{imagefiles(:).name});
Then you sort fileNo, save the indexes of the sorted array and go through these indexes in the for-loop:
[~,ind] = sort(fileNo);
for ii=ind
currentfilename = imagefiles(ii).name;
% write the name in txt file
end
I'm trying to save my data 'Images' after some treatment by using imwrite but the problem is that imwrite does not work for a sequence. I've read some solutions and tried them but they don't work. This is how I wrote my code, for example:
%read the sequence
for i=1:k
%treatment
Id{k} = waverec2(t_C,L,'sym8');
fileName = sprintf('C:\\Users\\swings\\Desktop\\data\\imagesPourAlgo\\images.tiff\\%02d',k);
imwrite ( Id, 'fileName', 'tif');
end
Knowing that I want to save 'write' each image separately for doing another process on them.
Why don't you try something like this:
for i = 1:10
I = waverec2(t_C,L,'sym8'); % or whatever you have
filename = ['c:\some\directory\file_number_' num2str(i) '.tif'];
imwrite(I,filename);
end
Personally,I prefer not to use 'sprintf' in such simple cases.
Your second input argument for imwrite is the char array fileName. Use the variable instead. The image is probably Id{k} and not Id:
imwrite ( Id{k}, fileName, 'tif');
Longtime lurker, first time posting! I'm new to Ruby so I would love some help on this.
I have a large text file with a list of files separated by a break, so it looks like this:
ARO_9501.jpg
ARO_9506.jpg
IMG_1499.jpg
IMG_1511.jpg
How can I get this text file into an array so I can call .each on it and copy the files to another directory?
This is how I generally do:
fileNameArray = File.read("/path/to/file.txt").split("\n")
Or, if you just need to iterate over the file names and don't necessarily need an array containing the names (it looks like you don't), I usually use this:
File.read("/path/to/file.txt").each_line do |line|
# do operations using line
end
Docs:
IO::read (File extends IO)
String .split() and each_line()
You can go this way also using IO::readlines :
ar = File.open("/home/kirti/ruby/foo.txt","r") do |fil|
fil.readlines.map(&:strip)
end
p ar
# >> ["ARO_9501.jpg", "ARO_9506.jpg", "IMG_1499.jpg", "IMG_1511.jpg"]
As per the #steenslag comments:
ar = File.readlines("/home/kirti/ruby/foo.txt").map(&:chomp)
ar # => [ "ARO_9501.jpg", "ARO_9506.jpg", "IMG_1499.jpg", "IMG_1511.jpg"]
I am writing a function for remote sensing purposes using matlab
the user will enter a folder containing 7 files into the program each file is a band of an image and the names of them is:
"b1.dat"
"b2.dat"
"b3.dat"
"b4.dat"
"b5.dat"
"b6.dat"
"b7.dat"
for example if 2 is entered as the argument of the function it will search in seven file names that are in the access and then will show b2.dat
how do you suggest me to write the code
You can use uigetfiles to select the directory and dir to get a list of the folders contents. Once you have the list, strfind will tell you a file contains a given number.
Or, using uigetdir:
dirName = uigetdir('C:\', 'select a directory');
contents = dir(dirName);
for c = contents
name = c.name;
if strfind(name,'3')
fileToOpen = name{1};
end
end
I used these two lines of codes:
folder = uigetdir('D:\','Select the folder containing bands')
filenames = dir(folder)
the first line returns the path to the folder as I expected:
folder =
D:\RS\911130 TM bands
but the second line not. I have 7 files in my folder and it returns a 9x1 struct
filenames =
9x1 struct array with fields:
name
date
bytes
isdir
datenum
for example the contents of the filenames(1,1) is:
I was wondering if anyone had any advice on parsing a file with fixed length records in Ruby. The file has several sections, each section has a header, n data elements and a footer. For example (This is total nonsense - but has roughly similar content)
1923 000-230SomeHeader 0303030
209231-231992395 MoreData
293894-329899834 SomeData
298342-323423409 OtherData
3 3423942Footer record 9832422
Headers, Footers and Data rows each begin with a specific number (1,2 & 3) in this example.
I have looked at http://rubyforge.org/projects/file-formatter/ and it looks good - except that the documentation is light and I can't see how to have n data elements.
Cheers,
Dan
There are a number of ways to do this. The unpack method of string could be used to define a pattern of fields as follows :-
"209231-231992395 MoreData".unpack('aa5A1A9a4Z*')
This returns an array as follows :-
["2", "09231", "-", "231992395", " ", "MoreData"]
See the documentation for a description of the pack/unpack format.
Several options exist as usual.
If you want to do it manually I would suggest something like this:
very pseudo-code:
Read file
while lines in file
handle_line(line)
end
def handle_line
type=first_char
parse_line(type)
end
def parse_line
split into elements and do_whatever_to_them
end
Splitting the line into elements of fixed with can be done with for instance unpack()
irb(main):001:0> line="1923 000-230SomeHeader 0303030"
=> "1923 000-230SomeHeader 0303030"
irb(main):002:0* list=line.unpack("A1A5A7a15A10")
=> ["1", "923", "000-230", "SomeHeader ", "0303030"]
irb(main):003:0>
The pattern used for unpack() will vary with field lengths on the different kinds of records and the code will depend on wether you want trailing spaces and such. See unpack reference for details.