Open a file in a program or application and save in another format

11 views (last 30 days)
How can a file (i.e. a .jpg image) be opened in a program (i.e. MS. Paint) and saved in a different format (i.e., TIF)?
I'd also appreciate hints to perform more tasks such as resizing the image or using other functions of the program in which the file is opened.
  2 Comments
Geoff Hayes
Geoff Hayes on 23 Dec 2022
@Vahid Atashbari - please clarify how your question relates to MATLAB. Why do you want the file to be opened in MS Paint? Are you also asking how to resize (or do whatever) the image using MS Paint too?
Vahid Atashbari
Vahid Atashbari on 8 Jan 2023
Geoff,
The question is broadly asking about loading other programs/applications in Matlab script and performing tasks on such applications as part of the code. The MS Paint was an example; you can make any other case/example.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 8 Jan 2023
Windows, MacOS and Linux versions of MATLAB can all use system() to invoke command lines. However system() does not permit any interactive control.
All three versions can use the Java interface to call the Java Robot class. That allows simulating key presses and button pushes and moving the cursor, and so allows control of programs which have a window open and visible. However the Robot is poor at figuring out what is currently showing, so it is poor at reading what is currently displayed and reacting to it.
Windows MATLAB offers calls to activeX controls, which uses a formal data communication interface. Programs such as Excel offer a fair bit of control through the interface. But the program has to have been specifically written to support the interface.
Windows MATLAB offers an interface to .NET interfaces. These are in some ways similar to activeX but the range of interfaces offered is improved.
In particular there is an available .net interface System.Diagnostics.Process which offers some flexibility in creating processes and communicating with them. But Windows only.
Some software packages offer DLL that give interfaces to control the program from C or C++. All versions of MATLAB offer the ability to call into precompiled C or C++ DLL provided the interface is defined properly.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 Dec 2022
You can try with the following functions to save image data - saveas() in an image file and open in MS applications using winopen():
(1) To save your image data displayed in a Figure window using: saveas(gcf, 'IMAGE', 'jpg')
(2) To open the file from MATLAB: winopen('IMAGE.jpg')
  3 Comments
DGM
DGM on 8 Jan 2023
Save images using imwrite() instead of figure capture.
imwrite(myimagevariable,'outputfile.png')
Using figure capture will usually result in the image being crudely resized with nearest-neighbor interpolation, some amount of excess white padding added, and the image potentially changing type (e.g. a binarized, indexed, or grayscale image being converted to RGB). It's destructive, poorly-controlled, and typically unnecessary.
JPG is also destructive and often unnecessary. Using JPG as an intermediate step between the workspace and a (typically) lossless format like TIFF largely defeats the purpose of using a lossless format.
For the specific case of MSPaint, I imagine most of the work that can hypothetically be done programmatically in Paint can also just be done in MATLAB.
For the general case of controlling other applications, I don't know if an arbitrary application supports desired operations through a command line interface or if there is some API to do it. That would require specifics. If you can write an external script to do it, then there you can always run that script via MATLAB.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!