Concatenating images from a folder - image

I have a series of images saved in a folder, and I have written a short program to open two of these image files, concatenate them (preferably vertically, although for now I am trying horizontally), then save this new image to the same folder. This is what I have written so far:
function concatentateImages
%this is the folder where the original images are located path='/home/packremote/SharedDocuments/Amina/zEXAMPLE/';
file1 = strcat(cr45e__ch_21', '.pdf');
[image1,map1] = imread(graph1);
file2 = strcat('cr45f__ch_24', '.jpg');
[image2,map2] = imread(graph2);
image1 = ind2rgb(image1,map1);
image2 = ind2rgb(image2,map2);
image3 = cat(2,image1,image2);
%this is the directory where I want to save the new images
dircase=('/home/packremote/SharedDocuments/Amina/zEXAMPLE/');
nombrejpg=strcat(dircase, 'test', jpgext)
saveas(f, nombrejpg, 'jpg')
fclose('all');
However, I keep getting an error that my files do not exist, though I am certain the names are copied correctly.
I am currently using jpg files, but the format can be easily converted.
Any input on how to fix this error, or a nicer way of preforming this task is greatly appreciated!
Cheers,
Amina

Replace
[image1,map1] = imread(graph1);
and
[image2,map2] = imread(graph2);
by
[image1,map1] = imread(file1);
and
[image2,map2] = imread(file2);
Also check that you are in the right working directory.

In addition to the answer by #Simon, you also need to change
file1 = strcat(cr45e__ch_21', '.pdf');
to
file1 = strcat('cr45e__ch_21', '.pdf');
I.e. you forgot a '. Also your function doesn't seem to include a definition of jpgext. I expect you want a line like
jpgext = '.jpg';
Lastly, mostly a coding practice issue, but you might want to switch to using fullfile to build your full file path.
Also, instead of worrying about being in the correct working directory, if you use full paths you save yourself from having to keep track of what directory you're in.
SO I would suggest:
dir1 ='/home/packremote/SharedDocuments/Amina/zEXAMPLE/';
file1 = fullfile(dir1, 'cr45e__ch_21.pdf');
etc

Related

Replace all images in folder with another image but keep filename

Is there a way in OSX terminal to replace all images in a folder (they all have incremental filenames) with another image, but to maintain the original file name?
I've been googling for a long time but have not found anything useful.
For example, the following files need replacing:
1.jpg
239.jpg
213.jpg
5678.jpg
I need the file names to stay the same but say default.jpg to overwrite the image and then be renamed to match the image it had just replaced. So in theory:
n = file
x = filename
default = c://download/default.jpg
folder = c://downloads
For each n in folder
x = get filename
copy default to folder
delete n
rename default to x
Loop
So it will programmatically replace all of the files with the new image?
I really do not know where to start?
Maybe :
for i in *.jpg
do
cp default.jpg "$i"
done
That will issue an error once (when processing default.jpg) but should do the job.
Ok, So I tweaked the code a bit and got it to work without an error. for example:
for i in /Users/User/downloads/listings/m/folder4/newfolder/*.jpg; do cp -R /Users/User/downloads/listings/m/folder4/newfolder/default.jpg "$i"; done
Just in case anyone else needs to do this. Thanks for your comments and pointing me in the right direction.
Regards
D

Autoit replace string in multiple text files

I have this code:
$szFile = "text.txt"
$szText = FileRead($szFile,FileGetSize($szFile))
$szText = StringReplace($szText, "before", "after")
FileDelete($szFile)
FileWrite($szFile,$szText)
It works but I have loads of text files to do this with and to configure the script each time is a problem. Does any one know how I could make it so it would do it for every file in that dir. I have tried $szFile = "*.txt" but that doesn't work. Thanks.
FileFindFirstFile supports wildcards. Note that the function returns a file handle which could then be used in your FileOpen call.

writing a image to a folder via imwrite in Matlab

I try to write a image to a folder but imwrite has no input for specifying a folder name. Is there any other func. or way to store image to a folder. Maybe I amiss on imwrite if I do, I am sorry about it.
When I use a file name parameter like "folder1/folder2/img.jpg" it does not create folders accordingly and it just create a file named as "folder1/folder2/img.jpg"
I also try to use mkdir ahead in time but it also gives "Permission Denied" on console.
By the way I am using Linux. May be about permission problem?
The filename parameter doesn't mean just the name - you can give it a path, either absolute or relative, as part of the string.
imwrite(img,'my/folder/images/file','jpg')
for example.
The folder would just be specified in the file name, like this
imwrite(img, 'folder1/filename.tif','tif');
or if you want to use an absolute path.
imwrite(img, 'C:/Users/UserName/folder1/filename.tif','tif');
Check out wikipedia on absolute and relative paths.
If you want to write the image into destination folder use the following code in MATLAB.
destinationFolder = 'C:\Users\Deepa\Desktop\imagefolder';
if ~exist(destinationFolder, 'dir')
mkdir(destinationFolder);
end
baseFileName = sprintf('%d.png', i); % e.g. "1.png"
fullFileName = fullfile(destinationFolder, baseFileName);
imwrite(img, fullFileName); % img respresents input image.
I hope this answer might help someone.
this will open new figure to choose the path and format.
[filename, ext, user_canceled] = imputfile

How to read images from folders in matlab

I have six folders like this >> Images
and each folder contains some images. I know how to read images in matlab BUT my question is how I can traverse through these folders and read images in abc.m file (this file is shown in this image)
So basically you want to read images in different folders without putting all of the images into one folder and using imread()? Because you could just copy all of the images (and name them in a way that lets you know which folder they came from) into a your MATLAB working directory and then load them that way.
Use the cd command to change directories (like in *nix) and then load/read the images as you traverse through each folder. You might need absolute path names.
The easiest way is certainly a right clic on the forlder in matlab and "Add to Path" >> "Selected Folders and Subfolders"
Then you can just get images with imread without specifying the path.
if you know the path to the image containing directory, you can use dir on it to list all the files (and directories) in it. Filter the files with the image extension you want and voila, you have an array with all the images in the directory you specified:
dirname = 'images';
ext = '.jpg';
sDir= dir( fullfile(dirname ,['*' ext]) );;
sDir([sDir.isdir])=[]; % remove directories
% following is obsolete because wildcarded dir ^^
b=arrayfun(#(x) strcmpi(x.name(end-length(ext)+1:end),ext),sDir); % filter on extension
sFiles = sDir(b);
You probably want to prefix the name of each file with the directory before using:
sFileName(ii) = fullfile(dirname, sFiles(ii));
You can process this resulting files as you want. Loading all the files for example:
for ii=1:numel(sFiles)
data{i}=imread(sFiles(ii).name)
end
If you also want to recurse the subdirectories, I suggest you take a look at:
How to get all files under a specific directory in MATLAB?
or other solutions on the FEX:
http://www.mathworks.com/matlabcentral/fileexchange/8682-dirr-find-files-recursively-filtering-name-date-or-bytes
http://www.mathworks.com/matlabcentral/fileexchange/15505-recursive-dir
EDIT: added Amro's suggestion of wildcarding the dir call

Find txt files in a Folder with matlab and save plot as jpg

I have the followinf situation. I have in a folder different *.txt files that contains numeric data. I'd like to save the picture of the out coming plot in a jpg file.
The pseudocode would be something like this.
while(! more *.txt files in the folder)
{
plot(data)
save_plot_data(plot1.jpg)
}
Best regards
I would go for something like this:
files = dir('*.txt');
for i=1:numel(files)
h = plot(load(files(i).name));
saveas(h,sprintf('plot%d.jpg',i),'jpg');
end
Best

Resources