writing a image to a folder via imwrite in Matlab - image

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

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

How do I display local image in markdown?

Does anyone know how to display a local image in markdown? I don't want to set up a webserver for that.
I try the following in markdown, but it doesn't work:
![image](files/Users/jzhang/Desktop/Isolated.png)
I suspect the path is not correct. As mentioned by user7412219 ubuntu and windows deal with path differently. Try to put the image in the same folder as your Notebook and use:
![alt text](Isolated.png "Title")
On windows the desktop should be at: C:\Users\jzhang\Desktop
The following works with a relative path to an image into a subfolder next to the document:
![image info](./pictures/image.png)
Solution for Unix-like operating system.
STEP BY STEP :
Create a directory named like Images and put all the images that will be rendered by the Markdown.
For example, put example.png into Images.
To load example.png that was located under the Images directory before.
![title](Images/example.png)
Note : Images directory must be located under the same directory of your markdown text file which has .md extension.
To add an image in markdown file the .md file and the image should be in the same directory. As in my case my .md file was in doc folder so i also moved the image into the same folder. After that write the following syntax in .md file
![alt text](filename)
like ![Car Image](car.png)
This has worked for me.
The best solution is to provide a path relative to the folder where the md document is located.
Probably a browser is in trouble when it tries to resolve the absolute path of a local file. That can be solved by accessing the file trough a webserver, but even in that situation, the image path has to be right.
Having a folder at the same level of the document, containing all the images, is the cleanest and safest solution.
It will load on GitHub, local, local webserver.
images_folder/img.jpg < works
/images_folder/img.jpg < this will work on webserver's only (please read the note!)
Using the absolute path, the image will be accessible only with a url like this: http://hostname.doesntmatter/image_folder/img.jpg
if image has bracket it won't display
![alt text](Isolated(1).png)
rename the image and remove brackets
![alt text](Isolated-1.png)
Update:
if you have spaces in the file path, you should consider renaming it too or if you use JavaScript you can encode it using
encodeURIComponent(imagePath)
Also, always try to save images and files alike with lowercase, please develop that habit, just my personal view though
Adding a local image worked for me by like so: ![alt text](file://IMG_20181123_115829.jpg)
Without the file:// prefix it did not work (Win10, Notepad++ with MarkdownViewer++ addon)
Edit: I found out it also works with html tags, and that is way better:
<img src="file://IMG_20181123_115829.jpg" alt="alt text" width="200"/>
Edit2: In Atom editor it only works without the file:// prefix. What a mess.
Depending on your tool - you can also inject HTML into markdown.
<img src="./img/Isolated.png">
This assumes your folder structure is:
├── img
└── Isolated.jpg
├── README.md
Edited:
Working for me ( for local image )
![system schema](doc/systemDiagram.jpg)
tree
├── doc
  └── jobsSystemSchema.jpg
├── README.md
markdown file README.md is at the same level as doc directory.
In your case ,your markdown file should be at the same level as the directory files.
Working for me (absolute url with raw path)
![system schema](https://server/group/jobs/raw/master/doc/systemDiagram.jpg)
NOT working for me (url with blob path)
![system schema](https://server/group/jobs/blob/master/doc/systemDiagram.jpg)
Just add the relative image file route from the markdown file
![localImage](./client/src/assets/12.png)
This worked for me in ubuntu:
![Image](/home/gps/Pictures/test.png "a title")
Markdown file is in:
/home/gps/Documents/Markdown/
Image file is in:
/home/gps/Pictures/
To my knowledge, for VSCode on Linux, the local image can be normally displayed only when you put the image into the same folder as your .md post file.
i.e. only ![](image.jpg) or ![](./image.jpg) will work.
Even the absolute path like ![](/home/bala/image.jpg)also doesn't work.
In Jupyter Notebook Markdown, you can use
<img src="RelPathofFolder/File" style="width:800px;height:300px;">
Another possibility for not displayed local image is unintentional indent of the image reference - spaces before ![alt text](file).
This makes it 'code block' instead of 'image inclusion'. Just remove the leading spaces.
You may find following the syntax similar to reference links in markdown handy, especially when you have a text with many displays of the same image:
![optional text description of the image][number]
[number]: URL
For example:
![][1]
![This is an optional description][2]
[1]: /home/jerzy/ComputerScience/Parole/Screenshot_2020-10-13_11-53-29.png
[2]: /home/jerzy/ComputerScience/Parole/Screenshot_2020-10-13_11-53-30.png
I've had problems with inserting images in R Markdown. If I do the entire URL: C:/Users/Me/Desktop/Project/images/image.png it tends to work. Otherwise, I have to put the markdown in either the same directory as the image or in an ancestor directory to it. It appears that the declared knitting directory is ignored when referencing images.
Either put the image in the same folder as the markdown file or use a relative path to the image.
just copy the image and then paste it, you will get the output
![image.png](attachment:image.png)
The basic syntax is ![Image description](Any_Image_of_your_choice.png "title"). In my case, I used image name as Any\ Image\ of\ your\ choice.png in ![Image description](Any\ Image\ of\ your\ choice.png) instead of ![Image description](Any_Image_of_your_choice.png) and it was not working. So I would say make sure to check the image directory and also image name doesn't contain spaces if so use underscore(_) instead of space.
Faced issue while using markdown in Jupyter notebook in Ubuntu 18.04.
I got a solution:
a) Example Internet:
![image info e.g. Alt](URL Internet to Images.jpg "Image Description")
b) Example local Image:
![image Info](file:///<Path to your File><image>.jpg "Image Description")
![image Info](file:///C:/Users/<name>/Pictures/<image>.jpg "Image Description")
TurboByte

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

importing multiple images matlab

I have a set of 100 jpg images named consecutively and I want to add them up to get a single image. I have seen the answer from here, but it does not run with me, what happened?
Here is the code:
im = imread('C:\Documents and Settings\1026175117_1.jpg');
for i = 2:10
im = imadd(im,imread(sprintf('C:\Documents and Settings\1026175117_%d.jpg',i)));
end
im = im/1000;
imshow(im,[]);
Here's the error message:
Error using ==> imread
Can't open file "C:" for reading;
you may not have read permission.
Backslash is a special character for sprintf() and needs to be escaped. Either use "\\" instead of "\" or try constructing your file paths another way. fullfile() is a good way to do it, so you only have to use sprintf for the file name part. Also see help sprintf.

Concatenating images from a folder

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

Resources