Downloading site images through terminal using curl - image

I'm trying to download a large batch of images from a website onto my Mac. I can download the smaller images with DownloadThemAll, SiteSucker, etc but they don't quite dig deep enough. So I've had to jump into Terminal which is a little bit out of my comfort zone and my skills are a bit rusty.
I've had a try with the script below:
curl -O http://www.domain.co.uk/system/images/[1-1000]/original/*.jpg
This script works and I can see the Terminal downloading the image files however the issue I'm having is that the files are being overwritten with *.jpg and not producing them sequentially such as 1.jpg, 2.jpg, 3.jpg etc or even with their original names. The original jpg names use random numbers/letters (such as LIC0145_websource.jpg) which is why I've tried to supplement it with the *.jpg. I'm wondering which piece of code I'm missing to tell the Terminal to download these images.
I've also tired calling the shell script below but run into the 'Unexpected end of file'
#!/bin/bash
for i in `seq 1 1000`;
do
input=http://www.domain.co.uk/system/images/$i/original/*.jpg
output=$i.jpg
# echo $input, $output
curl --output $output --remote-name $input
done
I think the curl option might still be a better option but if anyone has any fixes or other solutions let me know.

You can do something like this with wget (I know that's not curl):
wget --no-parent --accept=jpg,jpeg,htm,html --mirror http://somedomain/
Then CD to the directory and issue a
find ./ \( -iname '*.htm' -o -iname '*.html' \) -exec rm {} \;

Related

How to batch convert RAW images in several subfolders to DNG

So I have 20 subfolders full of files in my main folder and have around 200 files in every subfolder. I've been trying to write a script to convert every picture in every subfolder to DNG.
I have done some research and was able to batch convert images from the current folder.
I've tried developping the idea to get it to work for subfolders but to no success.
Here is the code I've written:
for D in 'find . -type d'; do for i in *.RW2; do sips -s format jpeg $i --out "${i%.*}.jpg"; cd ..; done; done;
The easiest and fastest way to do this is with GNU Parallel like this:
find . -iname \*rw2 -print0 | parallel -0 sips -s format jpeg --out {.}.jpg {}
because that will use all your CPU cores in parallel. But before you launch any commands you haven't tested, it is best to use the --dry-run option like this so that it shows you what it is going to do, but without actually doing anything:
find . -iname \*rw2 -print0 | parallel --dry-run -0 sips -s format jpeg --out {.}.jpg {}
Sample Output
sips -s format jpeg --out ./sub1/b.jpg ./sub1/b.rw2
sips -s format jpeg --out ./sub1/a.jpg ./sub1/a.RW2
sips -s format jpeg --out ./sub2/b.jpg ./sub2/b.rw2
If you like the way it looks, remove the --dry-run and run it again. Note that the -iname parameter means it is insensitive to upper/lower case, so it will work for ".RW2" and ".rw2".
GNU Parallel is easily installed on macOS via homebrew with:
brew install parallel
It can also be installed without a package manager (like homebrew) because it is actually a Perl script and Macs come with Perl. So you can install by doing this in Terminal:
(wget pi.dk/3 -qO - || curl pi.dk/3/) | bash
Your question seems confused as to whether you want DNG files like your title suggests, or JPEG files like your code suggests. My code generates JPEGs as it stands. If you want DNG output, you will need to install Adobe DNG Converter, and then run:
find . -iname \*rw2 -print0 | parallel --dry-run -0 \"/Applications/Adobe DNG Converter.app/Contents/MacOS/Adobe DNG Converter\"
There are some other options you can append to the end of the above command:
-e will embed the original RW2 file in the DNG
-u will create the DNG file uncompressed
-fl will add fast load information to the DNG
DNG Converter seems happy enough to run multiple instances in parallel, but I did not test with thousands of files. If you run into issues, just run one job at a time by changing to parallel -j 1 ...
Adobe DNG Converter is easily installed under macOS using homebrew as follows:
brew install caskroom/cask/adobe-dng-converter

Batch convert PNGs to individual PDFs while maintaining deep folder hierarchy in bash

I've found a solution that claims to do one folder, but I have a deep folder hierarchy of sheet music that I'd like to batch convert from png to pdf. What do my solutions look like?
I will run into a further problem down the line, which may complicate things. Maybe I should write a script? (I'm a total n00b fyi)
The "further problem" is that some of my sheet music spans more than one page, so if the script can parse filenames that include "1of2" and "2of2" to be turned into a single pdf, that'd be neat.
What are my options here?
Thank you so much.
Updated Answer
As an alternative, the following should be faster (as it does the conversions in parallel) and also able to handle larger numbers of files:
find . -name \*.png -print0 | parallel -0 convert {} {.}.pdf
It uses GNU Parallel which is readily available on Linux/Unix and which can be simply installed on OSX with homebrew using:
brew install parallel
Original Answer (as accepted)
If you have bash version 4 or better, you can use extended globbing to recurse directories and do your job very simply:
First enable extended globbing with:
shopt -s globstar
Then recursively convert PNGs to PDFs:
mogrify -format pdf **/*.png
You can loop over png files in a folder hierarchy, and process each one as follows:
find /path/to/your/files -name '*.png' |
while read -r f; do
g=$(basename "$f" .png).pdf
your_conversion_program <"$f" >"$g"
done
To merge pdf-s, you could use pdftk. You need to find all pdf files that have a 1of2 and 2of2 in their name, and run pdftk on those:
find /path/to/your/files -name '*1of2*.pdf' |
while read -r f1; do
f2=${f1/1of2/2of2} # name of second file
([ -f "$f1" ] && [ -f "$f2" ]) || continue # check both exist
g=${f1/1of2//} # name of output file
(! [ -f "$g" ]) || continue # if output exists, skip
pdftk "$f1" "$f2" output "$g"
done
See:
bash string substitution
Regarding a deep folder hierarchy you may use find with -exec option.
First you find all the PNGs in every subfolder and convert them to PDF:
find ./ -name \*\.png -exec convert {} {}.pdf \;
You'll get new PDF files with extension ".png.pdf" (image.png would be converted to image.png.pdf for example)
To correct extensions you may run find command again but this time with "rename" after -exec option.
find ./ -name \*\.png\.pdf -exec rename s/\.png\.pdf/\.pdf/ {} \;
If you want to delete source PNG files, you may use this command, which deletes all files with ".png" extension recursively in every subfolder:
find ./ -name \*\.png -exec rm {} \;
if i understand :
you want to concatenate all your png files from a deep folders structure into only one single pdf.
so...
insure you png are ordered as you want in your folders
be aware you can redirect output of a command (say a search one ;) ) to the input of convert, and tell convert to output in one pdf.
General syntax of convert :
convert 1.png 2.png ... global_png.pdf
The following command :
convert `find . -name '*'.png -print` global_png.pdf
searches for png files in folders from cur_dir
redirects the output of the command find to the input of convert, this is done by back quoting find command
converts works and output to pdf file
(this very simple command line works fine only with unspaced filenames, don't miss quoting the wild char, and back quoting the find command ;) )
[edit]Care....
be sure of what you are doing.
if you delete your png files, you will just loose your original sources...
it might be a very bad practice...
using convert without any tricky -quality output option could create an enormous pdf file... and you might have to re-convert with -quality "60" for instance...
so keep your original sources until you do not need them any more

Bash Use Origin File as Destination File

I'm working with the ImageMagick bash tool, which uses commands of the form:
<command> <input filename> <stuff> <output filename>
I'm trying to do the following command:
<command> x.png <stuff> x.png
but for every file in a directory. I tried:
<command> *.png <stuff> *.png
But that didn't work. What's the correct way to perform such a command on every file in a directory?
Many Unix command-line tools, such as awk, sed, grep are stream-based or line-oriented, which means they process files one line at a time. When using these, it is necessary to write output to an intermediate file and then rename that (with mv) back over the original input file. The reason for that is that you may be writing over the input file before you have read it all, so you will clobber your inputs. In that case, #sjsam's answer is absolutely correct - especially since he is careful to use the && in between so that the mv is not done if the command is not successful.
In the specific case of ImageMagick however, and its convert, compare, compose commands, this is NOT the case. These programs are file-oriented, not line-oriented, so they read the entire input file into memory before writing any outputs. The reason is that they often need to know if there are any transparent pixels in an image before they can start processing, or how many colours there are in an image and they cannot know this till they have read the entire file. As such, it is perfectly safe, and in fact idiomatic to use the same input filename as output filename, like this:
convert image.jpg ... -crop 100x100 -colors 16 ... image.jpg
In answer to your question, when you have multiple files to process, the preferred method is to use the mogrify tool (also part of the ImageMagick suite) which is specifically intended for processing multiple files. So, you would do this:
mogrify -crop 100x100 -colors 16 *.jpg
which would overwrite your input files with the results - which is what you asked for. If you did not want it to overwrite your input files, you would add a -path telling ImageMagick the path to an output directory like this:
mogrify -path /path/to/thumbnails -thumbnail -colors 16 *.jpg
If you had too many files for the Windows COMMAND.EXE/CMD.EXE to expand and pass to mogrify, you would let ImageMagick expand the glob (the asterisk) internally like this and be able to deal with unlimited numbers of files:
mogrify -crop 100x100 '*.jpg'
The point is that not only is the mogrify syntax concise, and portable across Windows/OSX/Linux, it also means you only need to create a single process to do all your images, rather than having to write a FOR loop and create and execute a convert process and a mv process for each and every one of potentially thousands of files - so it is much more efficient and less error-prone.
For a single file do like this :
<command> x.png <stuff> temp.png && mv temp.png x.png
For a set of files do like this :
#!/bin/bash
find `pwd` -name \*.png | while read line
do
<command> "$line" <stuff> temp.png && mv temp.png "$line"
done
Save the script in the folder containing the png files as processpng. Make it an executable and run it.
./processpng
A general version of the above script which takes the path to folder containing the above files as argument is below :
#!/bin/bash
find $1 -name \*.png | while read line
do
<command> "$line" <stuff> temp.png && mv temp.png "$line"
done
Save the script as processpng anywhere in the computer. Make it an executable and run it like :
./processpng /path/to/your/png/folder
Edit:
Incorporating #anishsane's solution, a compact way of achieving the same
results would be
#!/bin/bash
find $1 -name \*.png -exec bash -c "<command> {} <stuff> temp.png && mv temp.png {}"
In the context of find .... -exec:
{} indicates (contains) the result(s) from the find expression. Note
that empty curly braces {} have no special meaning to shell so we can
get away without escaping {}
Note: Emphasis mine.
Reference: This AskUbuntu Question.

Can someone please explain clearly how to use "pngcrush" for multiply items

I have a stack of hundreds of pictures and i want to use pngcrush for reducing the file sizes.
I know how to crush one file with terminal, but all over the web i find parts of explanations that assume previous knowledge.
can someone please explain how to do it clearly.
Thanks
Shani
You can use following script:
#!/bin/bash
# uncomment following line for more aggressive but longer compression
# pngcrush_options=-reduce -brute -l9
find . -name '*.png' -print | while read f; do
pngcrush $pngcrush_options -e '.pngcrushed' "$f"
mv "$f" "${f/%.pngcrushed/}"
done
Current versions of pngcrush support this functionality out of the box.
( I am using pngcrush 1.7.81)
pngcrush -dir outputFolder inputFolder/*.png
will create "outputFolder" if it does not exist and process all the .png files in the "inputFolder" placing them in "outputFolder".
Obviously you can add other options e.g.
pngcrush -dir outputFolder -reduce -brute -l9 inputFolder/*.png
Being in 2023, there are better tools to optimize png images like OptiPNG
install
sudo apt-get install optipng
use for one picture
optipng imagen.png
use for all pictures in folder
find /path/to/files/ -name '*.png' -exec optipng -o7 {} \;
optionally the command -o defines the quality, being possible from 1 to 7, where 7 is the maximum compression level of the image.
-o7
The high rated fix appears dangerous to me; it started compressing all png files in my iMac; needed is a command restricted to a specified directory; I am no UNIX expert; I undid the new files by searching for all files ending in .pngcrushed and deleting them

finding and renaming large *.jpg to *.jpeg

I have a large and messy collection of file--hey who doesn't--some of these are large JPGs (large in this case is an arbitrary number, say 2.5MB) that I want to rename--I want to change the extension from *.jpg to *.jpeg.
I'd love to do this with a shell script, I'm running BASH 3.2.39(1), and I have a feeling this is "simple" task with find, alas I find find's syntax difficult to remember and the man page impossible to read.
Any and all help with be most appreciated.
Finding and renaming large files could be done like this:
find . -size +2500k -exec rename -s .jpg .jpeg '{}' ';'
What OS are you using? In most repositories there is an app called mmv which is perfect for these kinds of things..
usage:
mmv \*.jpg \#1.jpeg
Install rename (standard tool in your linux installation or with homebrew for mac), then:
rename -s .jpg .jpeg *
or, if you have files in subdirectories too:
rename -s .jpg .jpeg $(find . -name '*.jpg')
for i in *.jpg
do
new_name= $(echo $i|sed 's/.jpg/.jpeg/')
mv $i $new.name
done

Resources