Using wildcards with "convert". Or "convert"ing a group of files - bash

I regularly scan in my Homework for class. My scanner exports raw jpg files to usb, and from there I can use gimp to edit and save the files as a pdf. One time saver I've found is to export my multi-page homeworks as a .mng file and then use the convert function to turn it into a pdf. I do it this way because Gimp automatically merges all layers when exporting to a pdf.
convert HW.mng HW.pdf
this works well for individual files, but at the end of every week I can have dozens of files to convert.
I have tried using wildcards in the filenames for convert:
convert *.mng *.pdf
This always runs successfully and never throws an error, but never produces any pdfs.
Both
convert HW*.mng HW*.pdf
and
convert "HW*.mng" "HW*.pdf"
yeild the error
convert: unable to open image `HW*.pdf': Invalid argument # error/blob.c/OpenBlob/2712.
which I think means the error lies in exporting with a wildcard.
Is there any way to convert all of a specific file type to another using convert? Or should I try using a different program?

You can see this StackExchange post. The accepted answer basically does what you want.
for file in *.mng; do convert -input "$file" -output "${file/%mng/pdf}"; done

For convert in particular, use mogrify (which is part of ImageMagick as well) as suggested by Mark Setchell in a comment. mogrify can be used to edit/convert files in batches. The command for your case would be
mogrify -format pdf -- *.mng

Related

Concatenating multiple pbm files, cmd

So we have, let's say, 10 files, called 01.pbm, 02.pbm, 03.pbm, etc. Using cmd with commands like paste or clip (or of course any you know) how could you merge all 10 of them keeping the corresponding file size? (if each of them is 1kb, the output should be 10kb). Keep in mind I use windows... (ugh, I know)
Use ImageMagick v6 to accomplish the task.
To append (merge) left-to-right:
convert *.pbm +append lr_out.pbm
To append top-to-bottom:
convert *.pbm -append tb_out.pbm
For more information read the documentation
Update
As fmw42 pointed out for IM v7 then the syntax should be:
magick *.pbm +append lr_out.pbm
magick *.pbm -append tb_out.pbm
So at the end it's just the simple function "type"...

GhostScript output files with original file name

I'm converting a PDF file to separate images using this command:
-sDEVICE=jpeg -o page-%02d.png X.pdf
This outputs the files as:
page-01.jpeg, page-02.jpeg, and so on.
However, I want to output the files with this file name:
X-page-01.jpeg, X-page-02.jpeg, and so on.
Is it possible to do this with GhostScript?
Well yes, clearly its possible:
-sDEVICE=jpeg -o X-page%02d.jpeg X.pdf
(I presume that you actually meant page-%02d.jpeg, rather than .png, since you are specifying the jpeg device).
No, Ghostscript won't automagically prepend the input filename to the output filename, you have to do that yourself.

ImageMagick Warning (TIF Conversion)

When I convert a Multi-Image TIF File to Individual TIF Files using ImageMagick I'm getting a Warning stating "Invalid TIFF Directory; tags are not sorted in ascending order'.
convert.exe: Invalid TIFF directory; tags are not sorted in ascending order.
'TIFFReadDirectoryCheckorder' # warning/tiff.c/TIFFWarnings/847
Hoping for Any advice I can get on this error. I am trying to take a Multi-Image TIF File and Turn it into Individual Files, however I need them to be in the order in which they were listed in the Original TIF File for this to work.
If you still have the issue check the limitation disk:
convert -list resource
Example for debian 9 /etc/ImageMagick-6/policy.xml.
I hope that could be helpful.

shellscript to convert .TIF to a .PDF

I'm wanting to progress through a directory's subdirectories and either convert or place .TIF images into a pdf. I have a directory structure like this:
folder
item_one
file1.TIF
file2.TIF
...
fileN.TIF
item_two
file1.TIF
file2.TIF
...
...
I'm working on a Mac and considered using sips to change my .TIF files to .PNG files and then use pdfjoin to join all the .PNG files into a single .PDF file per folder.
I have used:
for filename in *; do sips -s format png $filename --out $filename.png; done
but this only works for the .TIF files in a single directory. How would one write a shellscript to progress through a series of directories as well?
once the .PNG files were created I'd do essentially the same thing but using:
pdfjoin --a4paper --fitpaper false --rotateoversize false *.png
Is this a valid way of doing this? Is there a better, more efficient way of performing such an action? Or am I being an idiot and should be doing this with some sort of software, like ImageMagick or something?
Try using the find command with the exec switch to call your image conversion solution. Alternatively, instead of using the exec switch, you could pipe the output of find to xargs. There is lots of information online about using find. Here's one example from StackOverflow.
As far as the image conversion, I think that really depends on your requirements for speed and efficiency. If you've verified the process you described, and this is a one-time process, and it only takes seconds or minutes to run, then you're probably fine. On the other hand, if you need to do this frequently, then it might be worth investing the time to find a one-step conversion solution that takes less time than your current, two-pass solution.
Note that, instead of two passes, you may be able to pipe the output of sips to pdfjoin; however, that would require some investigation to verify.

What is the fastest way to unzip textfiles in Matlab during a function?

I would like to scan text of textfiles in Matlab with the textscan function. Before I can open the textfile with fid = fopen('C:\path'), I need to unzip the files first. The files have the extension: *.gz
There are thousands of files which I need to analyze and high performance is important.
I have two ideas:
(1) Use an external program an call it from the command line in Matlab
(2) Use a Matlab 'zip'toolbox. I have heard of gunzip, but don't know about its performance.
Does anyone knows a way to unzip these files as quick as possible from within Matlab?
Thanks!
You could always try the Matlab unzip() function:
unzip
Extract contents of zip file
Syntax
unzip(zipfilename)
unzip(zipfilename, outputdir)
unzip(url, ...)
filenames = unzip(...)
Description
unzip(zipfilename) extracts the archived contents of zipfilename into the current folder and sets the files' attributes, preserving the timestamps. It overwrites any existing files with the same names as those in the archive if the existing files' attributes and ownerships permit it. For example, files from rerunning unzip on the same zip filename do not overwrite any of those files that have a read-only attribute; instead, unzip issues a warning for such files.
Internally, this uses Java's zip library org.apache.tools.zip. If your zip archives each contain many text files it might be faster to drop down into Java and extract them entry by entry, without explicitly unzipped files. look at the source of unzip.m to get some ideas, and also the Java documentation.
I've found 7zip-commandline(Windows) / p7zip(Unix) to be somewhat speedier for this.
[edit]From some quick testing, it seems making a system call to gunzip is faster than using MATLAB's native gunzip. You could give that a try as well.
Just write a new function that imitates basic MATLAB gunzip functionality:
function [] = sunzip(fullfilename,output_dir)
if ~exist('output_dir','var'), output_dir = fileparts(fullfilename); end
app_path = '/usr/bin/7za';
switches = ' e'; %extract files ignoring directory structure
options = [' -o' output_dir];
system([app_path switches options '_' fullfilename]);
Then use it as you would use gunzip:
sunzip('/data/time_1000.out.gz',tmp_dir);
With MATLAB's toc timer, I get the following extraction times with 6 uncompressed 114MB ASCII files:
gunzip: 10.15s
sunzip: 7.84s
worked well, just needed a minor change to Max's syntax calling the executable.
system([app_path switches ' ' fullfilename options ]);

Resources