Script Issues

HOME Forums Tech Linux Talk Script Issues

This topic contains 15 replies, has 5 voices, and was last updated by Wheels-Of-Fire 7 months, 2 weeks ago.

  • Creator
    Topic
  • #29439

    Drezha
    Participant
    @drezha

    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]

Spread the love
Viewing 15 replies - 1 through 15 (of 15 total)
  • Author
    Replies
  • #29441

    Ed P
    Participant
    @edps
    Forumite Points: 3,715

    I 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.

    0
    0
    #29448

    Drezha
    Participant
    @drezha
    Forumite Points: 982

    Infile 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.

    0
    0
    #29451

    Ed P
    Participant
    @edps
    Forumite Points: 3,715

    Perhaps there is a ‘write-behind’ when dragging and dropping? Possibly starting the script off with a short ‘wait may help.

    0
    0
    #29492

    TCBarrett
    Participant
    @tcbarrett
    Forumite Points: 164

    Are you sure that $RANDOM is 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. にほんご。

    0
    0
    #29494

    Drezha
    Participant
    @drezha
    Forumite Points: 982

    Considering 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…).

     

    0
    0
    #29495

    Wheels-Of-Fire
    Participant
    @grahamdearsley
    Forumite Points: 1,640

    $RANDOM looks like it should be a value. Are you sure it stays in scope ?

     

    0
    0
    #29496

    Drezha
    Participant
    @drezha
    Forumite Points: 982

    It 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).

    0
    0
    #29508

    TCBarrett
    Participant
    @tcbarrett
    Forumite Points: 164

    I 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:

    1. Used a timestamp instead of using /dev/random (usually better unless you’re doing more than 1 per second)
    2. Set your desktop, infile and temp file as variables
    3. Check that the infile has been specific and exists
    4. Added a help argument

    A couple of questions that might help narrow things down are:

    1. You switch between ‘convert’ and ‘magic convert’ – is there a reason for that?
    2. It’s just the ‘320 x 240’ conversion that fails?

    Family. Coffee. CrossFit. WordPress. にほんご。

    0
    0
    #29509

    Ed P
    Participant
    @edps
    Forumite Points: 3,715

    I 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

    0
    0
    #29522

    Drezha
    Participant
    @drezha
    Forumite Points: 982

    Managed 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.

    • This reply was modified 7 months, 3 weeks ago by Drezha.
    1
    0
    #29534

    D-Dan
    Participant
    @d-dan
    Forumite Points: 1,294

    try 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
    fi
    • This reply was modified 7 months, 2 weeks ago by D-Dan.
    • This reply was modified 7 months, 2 weeks ago by D-Dan.

    Ryzen 7 1800X, 16 GB, 6 (yes - 6) HDs inc 2 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, NV GTX 1060 6GB, and a whopping 32" AOC 1440P monitor.

    0
    0
    #29536

    D-Dan
    Participant
    @d-dan
    Forumite Points: 1,294

    Oh, and the config file for the parameters:

    
    /path/to/pictures/
    180
    160x120

    Hell, 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 ‘

    • This reply was modified 7 months, 2 weeks ago by D-Dan.
    • This reply was modified 7 months, 2 weeks ago by D-Dan.

    Ryzen 7 1800X, 16 GB, 6 (yes - 6) HDs inc 2 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, NV GTX 1060 6GB, and a whopping 32" AOC 1440P monitor.

    0
    0
    #29541

    Drezha
    Participant
    @drezha
    Forumite Points: 982

    Dan, 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.

    0
    0
    #29546

    D-Dan
    Participant
    @d-dan
    Forumite Points: 1,294

    I’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.

    Ryzen 7 1800X, 16 GB, 6 (yes - 6) HDs inc 2 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, NV GTX 1060 6GB, and a whopping 32" AOC 1440P monitor.

    0
    0
    #29552

    Wheels-Of-Fire
    Participant
    @grahamdearsley
    Forumite Points: 1,640

    The 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 😁

    0
    0
Viewing 15 replies - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.

Spread the love