Forumite Members › General Topics › Tech › Linux Talk › Script Issues
- This topic has 15 replies, 5 voices, and was last updated 7 years, 2 months ago by
Wheels-Of-Fire.
-
AuthorPosts
-
December 24, 2018 at 11:36 am #29439
Trying to figure out why this script doesn’t work – any ideas?
The first convert command works – but the second convert command doesn’t seem to work, even by itself with the first section missing or in full. The command works when called from the command line but from dragging and dropping a file on to a .desktop file doesn’t seem to get it going.
[CODE]
# Relies on Imagemagick being installed
# Set Random String to rename images to
n=$RANDOM
# Resizes images to 1,228,800 pixels (1280×960, assuming a standard lanscape iPhone image of 4032×3024 pixels)
convert $1 -resize 1228800@ -format jpg /home/chris/Desktop/$n.jpg
# Thumbnail creation – 76,800 pixels (320 x 240)
convert $1 -resize 76800@ -format jpg /home/chris/Desktop/Temp.jpg
# Creates white background so that images are all the same size (320×320)
magick convert /home/chris/Desktop/Temp.jpg -gravity center -background white -extent 330×330 /home/chris/Desktop/Thumbnail_$n.jpg
# Removes temp file
rm /home/chris/Desktop/Temp.jpg[/CODE]
"Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett
December 24, 2018 at 2:26 pm #29441I have not used IM in years but don’t you need both an infile and an outfile for EACH case. I’m not sure what infile is used after the first conversion.
December 24, 2018 at 5:25 pm #29448Infile for both is $1 – I assume it can be used for both variables. However, even removing the first line and running the script with only the second line, it doesn’t work nicely. However, run the command manually with the input set within terminal and it works.
"Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett
December 24, 2018 at 6:32 pm #29451Perhaps there is a ‘write-behind’ when dragging and dropping? Possibly starting the script off with a short ‘wait may help.
December 26, 2018 at 2:02 pm #29492Are you sure that
$RANDOMis an actual value?Have you tried some oldskool debugging:
echo "convert $1 -resize 1228800@ -format jpg /home/chris/Desktop/$n.jpg"Family. Coffee. CrossFit. WordPress. にほんご。
December 26, 2018 at 4:13 pm #29494Considering that the first command gives me a picture with a random number, it would appear to be a value.
It’s when the second command (resizing to the smaller image) tries to run it doesn’t. That’s even if it’s the only command in the script. Annoyingly, Windows runs the command fine, but I’ve been trying to get away from it recently (and the Mac, as I’m not sure I want to pay out £1100 for a new Mac Mini…).
"Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett
December 26, 2018 at 5:02 pm #29495$RANDOM looks like it should be a value. Are you sure it stays in scope ?
December 26, 2018 at 6:19 pm #29496It does appear on some reading that the RANDOM variable gives a different number each time it is called, though I am assured I can get it be stored as variable (see here).
The temp file isn’t being created but with some playing, I think I could probably abolish that (in fact, playing about with it on the Windows laptop, I’ve managed to do just that – now to try that command on Linux and see what happens).
"Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett
December 28, 2018 at 6:37 am #29508I don’t have Image Magic installed, so I can’t test that. Sorry.
I can maybe help your scripting a little by example:
https://gist.github.com/tcbarrett/db6aed034f6f5fef076598a41a3b44a3
Overview of what I did:
- Used a timestamp instead of using /dev/random (usually better unless you’re doing more than 1 per second)
- Set your desktop, infile and temp file as variables
- Check that the infile has been specific and exists
- Added a help argument
A couple of questions that might help narrow things down are:
- You switch between ‘convert’ and ‘magic convert’ – is there a reason for that?
- It’s just the ‘320 x 240’ conversion that fails?
Family. Coffee. CrossFit. WordPress. にほんご。
December 28, 2018 at 7:43 am #29509I think we may be barking up the wrong tree
The command works when called from the command line but from dragging and dropping a file on to a .desktop file doesn’t seem to get it going.
It seems like the script is OK, it is the drag & drop that fails. What OS are you using to run the drag and drop part? If Ubuntu there may be a simple reason and fix. Link
December 28, 2018 at 7:39 pm #29522Managed to solve it – not entirely sure how, but it looks like combining the last two commands in to one did the job.
Working script can be found here. Creates the thumbnails on the desktop by dragging the photo on to the .desktop file (working in Mint at least, yet to try on Kubuntu). Gif shows it working. previously, it would run the first command and therefore create only the first image, but not the smaller thumbnail image. All seems to be working now.

TCBarrett – thanks for the effort in putting that together, I’ll have to read through it, as I’ve not done bash scripting for a while.
"Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett
December 30, 2018 at 7:59 pm #29534try mogrify (which I’m pretty sure, though not 100% certain) is a part of the imagemagic suite). This is a script I use to resize images for a small slideshow in a conky display (oddly, one of the few scripts I wrote for myself that I actually documented).
#!/bin/bash # Have the script recognise spaces in directory paths and filenames #set -x IFS=' ' # Read the config file exec 3<> ~/.conkypic read <&3 dirpath read <&3 timeout read <&3 as exec 3>&- # Now get a list of files cd $dirpath files=(<code>ls -1 *.[Jj]??</code>) len=${#files[*]} if [ ! -d /dev/shm/slideshow ]; then mkdir /dev/shm/slideshow fi if [ "$len" != "0" ]; then # Pick a file at random number=$RANDOM let "number %= $len" # Now we copy it a temporary location. The following copies to RAM. If you are short # on memory change the location to somewhere in /home #echo "${wd}${files[$number]}" cp "${wd}${files[$number]}" /dev/shm/conkypic.jpg #ls /dev/shm #Convert the image for conky mogrify -format png -resize $as /dev/shm/*.jpg # echo "Converted" mv /dev/shm/conkypic.png /dev/shm/slideshow/ rm /dev/shm/conkypic.jpg fiArch Linux, on a Ryzen 7 1800X, 32 GB, 5 (yes -5) HDs inc 5 SSDs, 4 RPi 3Bs + 1 RPi 4B - one as an NFS server with two more drives, PiHole (shut yours), Plex server, cloud server, and other random Pi stuff. Nice CoolerMaster case, 2 x NV GTX 1070 8GB, and a whopping 32" AOC 1440P monitor.
December 30, 2018 at 8:02 pm #29536Oh, and the config file for the parameters:
/path/to/pictures/ 180 160x120Hell, just noticed in the script post the forum corrupted the script by replacing “
" with "” tags.`OK. Dunno how to fix this. the < code > tags need to be replaced by back leaning ‘
Arch Linux, on a Ryzen 7 1800X, 32 GB, 5 (yes -5) HDs inc 5 SSDs, 4 RPi 3Bs + 1 RPi 4B - one as an NFS server with two more drives, PiHole (shut yours), Plex server, cloud server, and other random Pi stuff. Nice CoolerMaster case, 2 x NV GTX 1070 8GB, and a whopping 32" AOC 1440P monitor.
December 30, 2018 at 8:31 pm #29541Dan, convert is part of the Imagemagick suite like mogrify as well – only difference I believe is that mogrify edits the file in place, whilst convert creates a new image. I had to check Imagemagick, but that’s what the website says and must have been what I’d read and chose convert.
"Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett
December 31, 2018 at 9:37 am #29546I’ve only ever used convert for changing the filetype. It’s no issue to copy out the file for mogrify (as my script does). The original slideshow folder is untouched, and I can’t say I’ve ever noticed any speed issues caused by the copy. That’s probably more to do with modern hardware making it trivial, but there you go.
Arch Linux, on a Ryzen 7 1800X, 32 GB, 5 (yes -5) HDs inc 5 SSDs, 4 RPi 3Bs + 1 RPi 4B - one as an NFS server with two more drives, PiHole (shut yours), Plex server, cloud server, and other random Pi stuff. Nice CoolerMaster case, 2 x NV GTX 1070 8GB, and a whopping 32" AOC 1440P monitor.
December 31, 2018 at 1:03 pm #29552The speed of modern hardware certainly helps. The C++ book I just got for christmas goes to great lengths to encourage me to pass large data sets by reference (or const reference if I dont want the original to be modifiable) to avoid copying. To be honest though I would rather just get things working before worrying about that ?
-
AuthorPosts
- You must be logged in to reply to this topic.
