Xfce Forum

Sub domains
 

You are not logged in.

#1 2014-09-16 02:24:22

birch
Member
Registered: 2014-07-12
Posts: 63

[Solved] Thunar Custom Action - Sort Images by Dimension

Using the command line, this is the command I use to sort images by the largest width:

identify -format "%w %h %f\n" *.jpg | sort -n -r -k 1

Using the command line, this is the command I use to sort images by the largest height:

identify -format "%w %h %f\n" *.jpg | sort -n -r -k 2

I'd like two separate Custom Actions, one to sort images by greatest width and the other to sort images by greatest height. It could be that the thumbnails would not be sortable. A text output (possibly using "zenity --text-info") would be acceptable.


EDIT: Incidentally, when I say that the commands above sort the images, I don't mean for people to think that the thumbnails are sorted, but that the command outputs text that tells me which files are widest or highest in order of precedence.

Last edited by birch (2014-09-16 16:55:37)

Offline

#2 2014-09-16 22:26:58

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

The difficulty with creating a custom action with this command is that the identify command contains a %f in its parameter which is interpreted by the custom action. You need to create a separate script to do the work and call this script from the custom action.

The contents of the script:

#!/bin/bash
for item in "$@"
do
	identify -format "%w %h %f\n" $item
done | sort -n -r -k 1 

The custom action:
- command = "/path/to/script %F | zenity --text-info"
- appearance condition = Image Files

Note: This script does not work with files with spaces in them. Haven't been able to figure out a way around this yet. Probably will need to add some more processing logic in the script.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2014-09-17 01:56:16

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Tx! Your Sort by Width script was creating a blank output. So I added the names of the formats after "$item", in uppercase and lowercase. And it works, even for .gifs.

This is the modified script:

#!/bin/bash
for item in "$@"
do
    identify -format "%w %h %f\n" $item *.jpg *.JPG *.JPEG *.jpeg *.png *.PNG *.tiff *.TIFF *.gif *.GIF
done | sort -n -r -k 1

It doesn't work with webp files, but that's not surprising as tumbler and identify hasn't added webp support yet.

I have another problem. In the text-info output each file is repeated as many times as the total number of images for which I'm trying to find dimensions.

For example, if I select 22 images and run the Sort by Width custom action it will output the dimensions of all images, but repeat each one 22 times.

DqfNkiBs.jpg

Another odd thing that happens is that if I only select some images in a folder of many images it will still output all the images in the folder and not just the ones I selected.

Last edited by birch (2014-09-17 04:01:41)

Offline

#4 2014-09-17 12:38:26

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

Tx! Your Sort by Width script was creating a blank output.

That's odd because it works here.
ca118.png
ca218.png
Can you give an example of the number and types of files you are selecting?

My custom action looks like this:
ca313.png
ca411.png

So I added the names of the formats after "$item", in uppercase and lowercase. And it works, even for .gifs.

This is the modified script:

#!/bin/bash
for item in "$@"
do
    identify -format "%w %h %f\n" $item *.jpg *.JPG *.JPEG *.jpeg *.png *.PNG *.tiff *.TIFF *.gif *.GIF
done | sort -n -r -k 1

It doesn't work with webp files, but that's not surprising as tumbler and identify hasn't added webp support yet.

I have another problem. In the text-info output each file is repeated as many times as the total number of images for which I'm trying to find dimensions.

For example, if I select 22 images and run the Sort by Width custom action it will output the dimensions of all images, but repeat each one 22 times.

http://i.imgur.com/DqfNkiBs.jpg

Another odd thing that happens is that if I only select some images in a folder of many images it will still output all the images in the folder and not just the ones I selected.

When you add the extra extensions to the command, you are defeating the purpose of the Custom Action's %F, which is doing exactly that. And which explains why you are getting so many repeats of the filename.

Can you confirm that your custom action looks like mine from the above screenshots?


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#5 2014-09-17 16:41:49

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

I switched back to your command and did some testing. It seems that it doesn't work if I'm using the Custom Action on images within folders with two or more words separated by spaces. You noted that this was the case with filenames, but it appears to be the case with folder names as well. I'll continue using your command and just delete spaces from folder names, though it would be helpful to find a solution to that.

Btw, I notice that you don't have the ".sh" extension in your command. Is that a mistake? I always include the extension.

Last edited by birch (2014-09-17 16:44:42)

Offline

#6 2014-09-17 18:11:12

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

Btw, I notice that you don't have the ".sh" extension in your command. Is that a mistake? I always include the extension.

I generally don't use the .sh extension - mostly out of habit.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#7 2014-09-17 18:14:13

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

I switched back to your command and did some testing. It seems that it doesn't work if I'm using the Custom Action on images within folders with two or more words separated by spaces. You noted that this was the case with filenames, but it appears to be the case with folder names as well. I'll continue using your command and just delete spaces from folder names, though it would be helpful to find a solution to that.

The way that custom actions passes a list of files is like this: "/path/to/file/1 /path/to/file/2 /path/to/file/3 /path/to/file/4". The problem is that if there is a space anywhere in that path or the file name, the script won't be able to easily determine the separator between file names (currently a space).


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#8 2014-09-17 22:03:52

jansucan
Member
From: Prague, Czech Republic
Registered: 2014-07-23
Posts: 11
Website

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

It seems that the script gets filenames correctly (one absolute path in one parameter) even if they contain spaces. That's good. Thus, quoting an argument to the identify command should fix the bug. smile

#!/bin/bash
for item in "$@"
do
	identify -format "%w %h %f\n" "$item"
done | sort -n -r -k 1 

Last edited by jansucan (2014-09-17 22:06:58)

Offline

#9 2014-09-17 22:23:59

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Nice catch jansucan.

I wonder how it is doing that. If you create a custom action that does:

echo %F | zenity --text-info

...it only displays a long string of the selected files. If there is a space in the filename or the directory, it shows up. The script then should see those as separate items.

Hmm.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#10 2014-09-18 00:03:24

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Thanks! That solves the space problem. One feature that would be really cool is if you could double-click on the filename in the Zenity text box and the associated image would open. Probably not possible.

Offline

#11 2014-09-18 00:12:42

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Would it be possible to combine the height and width actions into that same bash script, so that when you click the action a button pops up asking you which of the two you prefer? It would be better than having two buttons.

This is bash script might be good for ideas. It's for converting video, and it has pop ups that allow you to input the video format and a custom bitrate.

!# /bin/bash



ConvertTo=$(zenity --list --column="Select One" --title="Video 

Converter" --height=300 --width=250 --text="Select the format 

to convert to" avi 3gp flv mov mp4 asf wmv mpg mov)

   if [ $? == 1 ]; then

      exit

   fi

Video=$(zenity --entry --entry-text="1000" --title="Video 

Converter" --text="Enter video bitrate (Kb/sec)")

   if [ $? == 1 ]; then

      exit

   fi

Audio=$(zenity --entry --entry-text="128" --title="Video 

Converter" --text="Enter audio bitrate (Kb/sec)")

   if [ $? == 1 ]; then

      exit

   fi



tail -f ~/Scripts/convert-video.sh | zenity --progress --pulsate

 --auto-close --auto-kill --title="Converting" --text="Converting

 video to $ConvertTo ... Please wait." &

name=$(echo $1 | cut -f1 -d.)



ffmpeg -i "$1" -ab "$Audio"K -vb "$Video"K "$name.$ConvertTo"  



killall -KILL tail



zenity --info --title="Video Converter" --text="Conversion to 

$ConvertTo finished.\n\nPlease check file to insure\nno errors 

on conversion."



exit 0

Offline

#12 2014-09-18 00:25:56

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

Thanks! That solves the space problem. One feature that would be really cool is if you could double-click on the filename in the Zenity text box and the associated image would open. Probably not possible.

I don't think zenity has that level of functionality. You'd have to write a program to do that.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#13 2014-09-18 00:32:49

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

Would it be possible to combine the height and width actions into that same bash script, so that when you click the action a button pops up asking you which of the two you prefer? It would be better than having two buttons.

Well, this works (somewhat).
First, change the Custom Action to read:

/path/to/script %F

...and we'll have the script do all of the work. And the script could read:

#!/bin/bash

if $(zenity --question --ok-label="Width" --cancel-label="Height" --text="Do you
 want to sort by height or width?");
then
        sort=2
else
        sort=1
fi

for item in "$@"
do
        identify -format "%w %h %f\n" "$item"
done | sort -n -r -k $sort | zenity --text-info

The only problem is the icons on the "Height" and "Width" buttons. I don't see anything in the man pages to change them.

EDIT: Looks like you can use YAD in place of zenity to have better control of the button icons.

Last edited by ToZ (2014-09-18 00:47:52)


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#14 2014-09-18 01:58:28

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Thanks! It mostly works, but will need some tweaking. I compared the results of the first script with the results of the second script:

First Width Script

2605 1851 buf000153.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg
1920 1222 buf000150.jpg

Second Width Script

640 3360 buf000436.jpg
640 3360 buf000435.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg


First Height Script:

640 3360 buf000436.jpg
640 3360 buf000435.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg


Second Height Script:

2605 1851 buf000153.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg
1920 1222 buf000150.jpg

So something needs to be changed in the second script.

Last edited by birch (2014-09-18 02:00:30)

Offline

#15 2014-09-18 06:16:02

jansucan
Member
From: Prague, Czech Republic
Registered: 2014-07-23
Posts: 11
Website

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

ToZ wrote:

I wonder how it is doing that. If you create a custom action that does:

echo %F | zenity --text-info

...it only displays a long string of the selected files.

I think it's caused by difference in how information is passed to a process. The script containing identify command gets its input data by argument mechanism so the work of analyzing command line and filling the "argument boxes" ($0 $1 $2 ...) is done by its caller.

In the quoted code above, zenity gets its input data from standard input. It reads a sequence of characters from pipe and waits for EOF char.

Offline

#16 2014-09-18 13:00:37

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

Thanks! It mostly works, but will need some tweaking. I compared the results of the first script with the results of the second script:

First Width Script

2605 1851 buf000153.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg
1920 1222 buf000150.jpg

Second Width Script

640 3360 buf000436.jpg
640 3360 buf000435.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg


First Height Script:

640 3360 buf000436.jpg
640 3360 buf000435.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg


Second Height Script:

2605 1851 buf000153.jpg
2461 3233 buf000176.jpg
2461 3233 buf000175.jpg
2461 3233 buf000174.jpg
2461 3233 buf000173.jpg
1920 1222 buf000150.jpg

So something needs to be changed in the second script.

Oops, looks like the sorting got reversed. Try exchanging the sort values in the if statement:

#!/bin/bash

if $(zenity --question --ok-label="Width" --cancel-label="Height" --text="Do you
 want to sort by height or width?");
then
        sort=1
else
        sort=2
fi

for item in "$@"
do
        identify -format "%w %h %f\n" "$item"
done | sort -n -r -k $sort | zenity --text-info

I've also had a look at YAD and it is definitely better and more configurable. Here is the same script written using YAD:

#!/bin/bash

if $(yad --question --button="Height:0" --button="Width:1" --text="Do you want t
o sort by height or width?");
then
        sort=2
else
        sort=1
fi

for item in "$@"
do
        identify -format "%w %h %f\n" "$item"
done | sort -n -r -k $sort | yad --text-info

... no icons on the height/width buttons!
yad12.png
yad20.png


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#17 2014-09-18 13:49:23

Simon_P
Member
From: United Kingdom
Registered: 2014-09-18
Posts: 15

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

If you use zenity --list it can do the sorting for you (click the column headers), and then you can select a file for display. Since sorting is limited to alphabetic, it's necessary to include leading zeros on numbers.

#!/bin/bash

function selectImage {
	for file in "$@"
	do
		p=(`identify -format "%w %h" "$file"`)
		printf "%05d\n%05d\n%s\n" ${p[0]} ${p[1]} "$file"
	done | zenity --list --print-column=3 --column "width" --column "height"  --column="file name" 2>/dev/null
}

while
	target=`selectImage "$@"`
	[ -f "$target" ]
do
	display "$target"
done

If you want to display several images at the same time (eg to compare), edit the script to run the display command in the background, then choose one file each time the list is displayed.

Offline

#18 2014-09-18 14:56:41

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

@Simon_P, very nice.
That might be exactly what the OP is looking for.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#19 2014-09-18 15:13:38

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Thanks ToZ! Your YAD script is better, but Simon_P's script is the best yet. Simon_P, you write "Since sorting is limited to alphabetic, it's necessary to include leading zeros on numbers." I've been able to use your script on files with any type of naming.

EDIT: I realize that this script allows images to be opened. You click on one of the images in the results list, click OK, and it opens. When you close the image the sorting box opens up again. Very useful!

EDIT 2: Can the script be modified so that the images open up in XNViewMP, instead of ImageMagick?

Is there any way to customize the size of the pop-up box? It opens small and so I have to manually resize it by adjusting the borders with the cursor. I don't want it maximized, but a bit bigger.

Last edited by birch (2014-09-18 15:28:57)

Offline

#20 2014-09-18 17:48:39

Simon_P
Member
From: United Kingdom
Registered: 2014-09-18
Posts: 15

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

I agree about the size of zenity's window. I don't think zenity has any size options, but here's a variant with way to set the size & position of the list window using wmctrl (make sure you have it installed).

#!/bin/bash

ZENITY_X=0
ZENITY_Y=0
ZENITY_W=500
ZENITY_H=400

function selectImage {
	{ 	while 
			! wmctrl -l -G|grep "ZenityImageSorter" >/dev/null 
		do 
			sleep 0.05
		done
		wmctrl -r ZenityImageSorter -e 0,$ZENITY_X,$ZENITY_Y,$ZENITY_W,$ZENITY_H
	} &
	for file in "$@"
	do
		p=(`identify -format "%w %h" "$file"`)
		printf "%05d\n%05d\n%s\n" ${p[0]} ${p[1]} "$file"
	done | zenity --list --title="ZenityImageSorter" --print-column=3 --column "width" --column "height"  --column="file name" 2>/dev/null
}

while
	target=`selectImage "$@"`
	[ -f "$target" ]
do
	display "$target"
done

The lines at the top beginning ZENITY_ set the window's position x, position y, width, height. Customise these to your taste.

I don't know XNViewMP, but if you know the command to open an image with it, use it instead of display.

Offline

#21 2014-09-18 19:33:48

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Nice! I was also able to get the images to open with XNViewMP by replacing display with "xnviewmp" (without quotes). You mentioned that the display can run in the background. I notice that after I've closed an image the Zenity window has to recreate the list each time. Is this what you're referring to? If so, could you tell me how to get it to run in the background so that it doesn't have to recreate the list everytime I close an image?

Offline

#22 2014-09-18 20:33:54

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Another question: When working with more than 1488 images a warning pops up:

Failed to execute child process "/bin/sh" (Argument list is too long).

This warning pops up when using the other scripts in this thread as well. Same number limit of 1488. Any code I could use to make it work with an infinite number of images?

And I wonder why no more than 1488 images. Why not no more than 2000? Or 1499? Seems random. The images round up to 165 megabytes. Does it have something to do with the size of the files, as well as the number of files?

Last edited by birch (2014-09-18 20:40:10)

Offline

#23 2014-09-19 09:53:25

jansucan
Member
From: Prague, Czech Republic
Registered: 2014-07-23
Posts: 11
Website

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

birch wrote:

And I wonder why no more than 1488 images. Why not no more than 2000? Or 1499? Seems random. The images round up to 165 megabytes. Does it have something to do with the size of the files, as well as the number of files?

It's related to the operating system kernel. Simply said, reaching the limit depends on how long the names of files are and on their number. It seems that the only solution would be to patch Thunar to use mechanism of passing data other than argument list (e.g. pipe or file).

Last edited by jansucan (2014-09-19 14:18:56)

Offline

#24 2014-09-19 22:27:22

birch
Member
Registered: 2014-07-12
Posts: 63

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

jansucan wrote:
birch wrote:

And I wonder why no more than 1488 images. Why not no more than 2000? Or 1499? Seems random. The images round up to 165 megabytes. Does it have something to do with the size of the files, as well as the number of files?

It's related to the operating system kernel. Simply said, reaching the limit depends on how long the names of files are and on their number. It seems that the only solution would be to patch Thunar to use mechanism of passing data other than argument list (e.g. pipe or file).

Tx! I transferred the folder to my desktop and shortened the folder name to 1 letter, and shortened the filenames to 5 characters. Now I'm able to sort at least 4775 images. So that's an acceptable workaround.

I think the only improvement to the script left is to be able to open and close multiple pictures without have to create the list every time.

Offline

#25 2014-09-20 07:23:46

jansucan
Member
From: Prague, Czech Republic
Registered: 2014-07-23
Posts: 11
Website

Re: [Solved] Thunar Custom Action - Sort Images by Dimension

Or there is %D parameter in Edit Action window in Thunar. It could be used to increase the number of files the script can process.

%D directories containing the files that are passed in %F

"Size" of directory names is certainly lower than "size" of file names. The script could be carefully modified to use this advantage. From the caller it will only get a directory names and create a list of files itself. The more files are in the single directory the more of them you can process in one invocation of the script. If all files are in a single directory the number of files is limited (practically) only by used filesystem.

EDIT: But in that case you will lose the possibility of selective processing.

Last edited by jansucan (2014-09-20 07:30:44)

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.013 seconds, 7 queries executed - Memory usage: 696.13 KiB (Peak: 744.98 KiB) ]