Script Issues

Forumite Members General Topics Tech Linux Talk Script Issues

Viewing 16 posts - 1 through 16 (of 16 total)
  • Author
    Posts
  • #29439
    DrezhaDrezha
    Participant
      @drezha
      Forumite Points: 0

      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

      #29441
      Ed PEd P
      Participant
        @edps
        Forumite Points: 39

        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.

        #29448
        DrezhaDrezha
        Participant
          @drezha
          Forumite Points: 0

          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.

          "Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett

          #29451
          Ed PEd P
          Participant
            @edps
            Forumite Points: 39

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

            #29492
            TCBarrettTCBarrett
            Participant
              @tcbarrett
              Forumite Points: 2

              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. にほんご。

              #29494
              DrezhaDrezha
              Participant
                @drezha
                Forumite Points: 0

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

                 

                "Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett

                #29495
                Wheels-Of-FireWheels-Of-Fire
                Participant
                  @grahamdearsley
                  Forumite Points: 4

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

                   

                  #29496
                  DrezhaDrezha
                  Participant
                    @drezha
                    Forumite Points: 0

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

                    "Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett

                    #29508
                    TCBarrettTCBarrett
                    Participant
                      @tcbarrett
                      Forumite Points: 2

                      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. にほんご。

                      #29509
                      Ed PEd P
                      Participant
                        @edps
                        Forumite Points: 39

                        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

                        #29522
                        DrezhaDrezha
                        Participant
                          @drezha
                          Forumite Points: 0

                          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.

                          "Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett

                          #29534
                          D-DanD-Dan
                          Participant
                            @d-dan
                            Forumite Points: 6

                            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

                            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.

                            #29536
                            D-DanD-Dan
                            Participant
                              @d-dan
                              Forumite Points: 6

                              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 ‘

                              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.

                              #29541
                              DrezhaDrezha
                              Participant
                                @drezha
                                Forumite Points: 0

                                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.

                                "Everything looks interesting until you do it. Then you find it’s just another job" - Terry Pratchett

                                #29546
                                D-DanD-Dan
                                Participant
                                  @d-dan
                                  Forumite Points: 6

                                  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.

                                  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.

                                  #29552
                                  Wheels-Of-FireWheels-Of-Fire
                                  Participant
                                    @grahamdearsley
                                    Forumite Points: 4

                                    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 ?

                                  Viewing 16 posts - 1 through 16 (of 16 total)
                                  • You must be logged in to reply to this topic.