1

Situation: start browsing of the figures at width 1:960 and height full in Terminal
Motivation: Ubuntu's default image viewer opens the figure at full width and full height, making the browsing of very wide figures difficult; its CTRL + - of the full picture is not enough whene you iterate many pictures where width x height
Differential conditions: To Adjust Brightness-Contrast in Plane with Gimp/ImageMagick/…?, ... Image Resolution as Width x Height and Margins in pixels at each iteration

Iteration Image Resolution Left Right Top Bottom
1         960 x    960     110  80    70  100    
2         960 x   1920     230  180   70  100
3         960 x   2880     350  280   70  100
4         960 x   3840     470  380   70  100
5         960 x   4800     590  480   70  100
6         960 x   5760     710  580   70  100
7         960 x   6720     830  680   70  100
8         960 x   7680     950  780   70  100
9         960 x   8640     1070 880   70  100  
10        960 x   9600     1190 980   70  100 

% whtyger and Gimp use to find out the margins http://askubuntu.com/a/803012/25388
  • Dummy test data of Image Resolution 960x1920 at 2nd iteration, 960x4800 at 5th iteration, 960x8640 at 9th iteration, and Resolution 960x9600 at 10th iteration, respectively

    enter image description here enter image description here enter image description here enter image description here

My pseudocode

#!/bin/bash

OUTRES=$1
ITER=$2
IMAGE=$3

top=70
height=960 

width=$(( 960*${ITER} ))
left=$(( 110+${ITER}*120 ))
right=$(( 80+${ITER}*100 ))
x=$(( ${width}-${left}-${right} ))
y=$(( ${height}-${bottom}-${top} ))

display -geometry ${OUTRES}x${OUTRES} \
        -extract ${x}x${y}+${left}+${top} "$IMAGE"

Attempt with unstable margins with display and extract

whtyger's code works but it has some unstable margins i.e. their size differs depending on the image, here 5th iteration

enter image description here enter image description here

  • At Right-hand-side picture, you see that not all text of the colorbar is included in the output.
  • At Left-hand-side picture, you see the margin_left is not equal to margin_top.

Reason: MARGIN_T, MARGIN_B, MARGIN_L, MARGIN_R
Fix: use scientific numbering with larger iterations or increase MARGIN_R; some adjustments in the code

MARGIN_T=60
MARGIN_B=90
MARGIN_L=$(( -5 + $ITER * 119 ))
MARGIN_R=$(( -20 + $ITER * 95 ))

Output: some margin around the data always

Reviewing whtyger's comment for attempt of no margins with convert and crop

To get rid of margins completely you can modify the original image. Use convert image.png -crop SIZE_XxSIZE_Y+SHIFT_X+SHIFT_Y > edited.png and then use any viewer you like. You can adapt my script for that, just replace display+-extract with convert+-crop.

I replace the last line by the following but unsuccessful output

convert "$IMAGE" -crop ${EXTRACT_X}x${EXTRACT_Y}+${MARGIN_L}+${MARGIN_T} \ 
    "${IMAGE%.png}_cropped.png"

display -geometry ${OUTRES}x${OUTRES}+${SHIFT}+${SHIFT} \
    -extract ${EXTRACT_X}x${EXTRACT_Y}+${MARGIN_L}+${MARGIN_T} \
    "${IMAGE%.png}_cropped.png"

rm "${IMAGE%.png}_cropped.png"

Output: about 1 px margin in all directions, left margin chops some info from numbers of >= 1000.

System: Linux Ubuntu 16.04 64 bit
Hardware: Macbook Air 2013-mid

  • Can you use Cntrl + - to view the whole picture? – grooveplex Jul 23 '16 at 8:27
  • @grooveplex No. It is too time-consuming with arbitrary size of figures. I need better control. – Léo Léopold Hertz 준영 Jul 23 '16 at 8:28
  • Does it work to resize? – Pichi Wuana Jul 25 '16 at 18:58
  • @PichiWuana Yes, but it is not what I am looking for. I need terminal control or GV-like GUI. – Léo Léopold Hertz 준영 Jul 25 '16 at 20:29
3
+100

There's a powerful image suite called ImageMagick. Among its tools you can find a display utility, which is used, as it is obvious from its name, to display various graphic formats from the console. Here are several examples of its usage:

display -geometry 500x500 image.png

This will open your image in the window with the selected size. Moreover, a small window representing the full image will be opened nearby, so you can move the selection rectangle to show the desired parts of your image.

display -extract 300x300+50+50 image.png

This command will show you the part of your image with the size of 300x300 pixels and with the offset of 50 pixels from its top left corner.

You can find the full list of its options in man display. Also there's an extensive manual here.

ImageMagick also provides other useful tools. For example, this will show the info about the image (its type, dimensions, size):

identify "my image.png"

In order to display your image without the margins we should measure them to crop them later. I opened your dummy picture in Gimp to make things faster. So the margins are:

Left   - 1070px
Right  -  880px
Top    -   70px
Bottom -  100px

If the original size of you picture is 8640x960 then the image size without the margins will be: X=8640-1070-880=6690 and Y=960-70-100=790, i.e. 6690x790.
And now we'll combine -geometry and -extract options to produce the output:

display -geometry 500x500 -extract 6690x790+1070+70 raw.png

(set the size of output window to 500x500, crop the image to the new size 6690x790 and shift the crop area by 1070px horizontally and by 70px vertically from the top left corner of the image).

Here's what I've got (I moved the selection to the right for more clearness):

Fragment

All these calculations are a bit tedious, but if you've got static margins (it seems that your images are produced automatically), then you can calculate them once and apply to all your images. Or even make some script to automate this (that's an improved version of the script - it calculates iteration using identify command of ImageMagick):

#!/bin/bash

if [ $# -lt 2 ] ; then echo "usage: reviewimage output_resolution image_name" ; exit ; fi

OUTRES=$1
IMAGE=$2

# Some sanity or error checks, change as you see fit
if [ $OUTRES -lt 100 ] || [ $OUTRES -gt 1000 ] ; then echo "error: Invalid resolution" ; exit ; fi
if ! [ -e "$IMAGE" ] ; then echo "error: Image doesn't exist" ; exit ; fi

SHIFT=50    # Relative position of output window from top left corner of the desktop
IMAGE_X=$(identify "$IMAGE" | grep -o '[0-9]*x960 ' | cut -d'x' -f1)
IMAGE_Y=960
ITER=$(( $IMAGE_X / 960 ))
MARGIN_T=70
MARGIN_B=100
MARGIN_L=$(( -5 + $ITER * 119 ))
MARGIN_R=$(( -40 + $ITER * 102 ))
EXTRACT_X=$(( $IMAGE_X - $MARGIN_L - $MARGIN_R ))
EXTRACT_Y=$(( $IMAGE_Y - $MARGIN_T - $MARGIN_B ))

display -geometry ${OUTRES}x${OUTRES}+${SHIFT}+${SHIFT} -extract ${EXTRACT_X}x${EXTRACT_Y}+${MARGIN_L}+${MARGIN_T} "$IMAGE"

Save this script under the name reviewimage somewhere. Then run the commands below:

sudo cp reviewimage /usr/local/bin/
sudo chmod 755 /usr/local/bin/reviewimage

Now you can call this command from every folder with your images. For example, to open a preview of your image raw10.png with the size of 700x700:

reviewimage 700 raw10.png

If the name have spaces in it, use quotes:

reviewimage 700 "raw 2.png"

If the margin calculation formula becomes imprecise with high iterations you can use array of margin values. Adjust each margin value as you see fit. Script below shows the principle. It is filled with the values for iterations 1-10, add more inside the brackets with the space as a divider:

#!/bin/bash

if [ $# -ne 1 ] ; then echo "usage: convertimage image_name" ; exit ; fi
if ! [ -e "$1" ] ; then echo "error: Image doesn't exist" ; exit ; fi

IMAGE=$1

IMAGE_X=$(identify "$IMAGE" | grep -o '[0-9]*x960 ' | cut -d'x' -f1)
IMAGE_Y=960
ITER=$(( $IMAGE_X / 960 ))
MARGIN_T=70
MARGIN_B=100
MARGIN_L=(0 114 233 352 471 590 709 828 947 1066 1185)
MARGIN_R=(0 80 180 280 380 480 580 680 780 880 980)
EXTRACT_X=$(( $IMAGE_X - ${MARGIN_L[$ITER]} - ${MARGIN_R[$ITER]} ))
EXTRACT_Y=$(( $IMAGE_Y - $MARGIN_T - $MARGIN_B ))

convert "$IMAGE" -crop ${EXTRACT_X}x${EXTRACT_Y}+${MARGIN_L[$ITER]}+${MARGIN_T} "${IMAGE%.png}_cropped.png"

This script performs cropping of the image instead of displaying it, so this converted image can be viewed in any program. It needs only one parameter - image name:

convertimage "raw 9.png"
  • 1
    @Masi display -geometry treats offsets as a horizontal and vertical shift of the output window. Did you try display -extract? It treats offsets as a relative position of the extracted part of your image. – whtyger Jul 26 '16 at 15:08
  • 1
    @Masi Just open your image with GIMP. Turn on rulers (Ctrl+Shift+R). When you move the cursor to the borders of your image, you'll see the markers on the rulers, this helps to determine pixel width of margins. As regards your 10th iteration dummy, its top and bottom margins hasn't changed, the left will be 1190, the right will be 980. The rest iterations are up to you. It looks like an arithmetic progression, which can be easily calculated. – whtyger Jul 27 '16 at 15:11
  • 1
    @Masi I added the script to automate the preview. I hope that it will be enough at last. – whtyger Jul 28 '16 at 9:30
  • 1
    @Masi My system is 12.04LTS. But that's not important in this case. Have you tried my script? – whtyger Jul 29 '16 at 12:20
  • 1
    @Masi I'm not sure that I understand. That's not ImageMagick, which uses functions, it just displays an area which was requested by Bash script. You can apply -extract option with some arbitrary dimensions, and it will show it. Experiment with it from console, check manpage I mentioned. And the last piece of advice. To get rid of margins completely you can modify the original image. Use convert image.png -crop SIZE_XxSIZE_Y+SHIFT_X+SHIFT_Y > edited.png and then use any viewer you like. You can adapt my script for that, just replace display+-extract with convert+-crop. – whtyger Jul 30 '16 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.