Script to find image width x length? (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
trio
Posts: 2076
Joined: Sun 21 Dec 2008, 15:50
Location: अनà¥￾मोदना

Script to find image width x length? (Solved)

#1 Post by trio »

Dear puppians,

Is there a native puppy command to get an image size? Width x Length
Or if not native, can someone please lead me to it?

Thanks a lot

Note: the "file" command won't work with. Jppg

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#2 Post by SFR »

For BMP, GIF, JPG, TGA & TIF you could use exiv2 binary. I think it's present in all of Pups.
As for some other formats, here's a snippet I made some time ago that might be helpful:

Code: Select all

#!/bin/bash

FILENAME="$1"
[ "$FILENAME" ] || exit 1

FILENAME_TEMP=`echo "$FILENAME" | tr '[:upper:]' '[:lower:]'`

case ${FILENAME_TEMP##*.} in
  bmp|gif|png|jpg|jpeg|tga|tif|tiff)
    read X Y <<< `exiv2 "$FILENAME" 2>/dev/null | awk '/Image size/ {print $4,$6}'`
  ;;
  ico)
    read X Y <<< `file -b "$FILENAME" | awk -F',' '{split($2,a,"x"); print a[1]" "a[2]}'`
  ;;
  pcx)
    read X Y <<< `file -b "$FILENAME" | cut -d ']' -f2 | cut -d '[' -f2 | awk -F',' '{print $1+1,$2+1}'`
  ;;
  xpm)
    read X Y <<< `grep -m1 "\"" "$FILENAME" | tr -d "\"" | awk '{print $1,$2}'`
  ;;
  svg)
    X=`cat "$FILENAME" | tr -d ' \t' | grep -m1 -io "width=".[0-9]* | tr -cd '[[:digit:]]'`
    Y=`cat "$FILENAME" | tr -d ' \t' | grep -m1 -io "height=".[0-9]* | tr -cd '[[:digit:]]'`      
  ;;
  ppm|pnm)
    read X Y <<< `grep -v '#' "$FILENAME" | grep -a -m2 .[0-9] | tail -1 | awk '{print $1,$2}'`
  ;;
  *) echo "Not supported..."; exit 1
esac

echo "$FILENAME: ${X}x${Y}"
Doesn't work with certain .ico files, but otherwise it's quite reliable...

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#3 Post by Flash »

mtPaint shows the image size, at the bottom left corner. Right-click on the file, then choose Open with -> mtPaint.

B.K. Johnson
Posts: 807
Joined: Mon 12 Oct 2009, 17:11

#4 Post by B.K. Johnson »

From your browser (Firefox), right-click on the picture and choose the option about view image info or something like that. Gives you the original dimensions and also that if scaled.
I found your picture is 80px × 62px. See screen shot

B.K. Johnson
[EDIT]
Reread your post and realized you were looking for a command. So my suggestion may not be what you want, however, I am leaving the reply in case someone else finds it useful.
Attachments
2Forum-bikeman.jpg
Output- right-click &gt; View image info of your pic.
(63.06 KiB) Downloaded 417 times

User avatar
ASRI éducation
Posts: 3197
Joined: Sat 09 May 2009, 12:10
Location: France
Contact:

Re: How to find image width x length?

#5 Post by ASRI éducation »

trio wrote:Is there a native puppy command to get an image size? Width x Length
Hello Flash and B.K. Johnson.
Trio is not a novice.
It is probable that he is trying to develop a script.
Graphical interfaces are not going to help ...
Regards
Projet ASRI éducation => [url=http://asri-education.org/]Association[/url] | [url=http://forum.asri-education.org/]Forum[/url] | [url=http://dl01.asri-education.org/]Dépôt[/url] | [url=http://kids.asri-education.org/]Espace kids[/url]

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#6 Post by dejan555 »

Not sure about native puppy command, but exiv2 can get image size from exif (not that every image has exif saved though)
EDIT: Googled and found an answer on stackoverflow that says that exiv2 will display size even if exif isn't saved, and I just tested it, see second output for image with no exif.

Code: Select all

13 root:960px$ data=$(exiv2 IMG_5834-960.JPG)
14 root:960px$ echo "$data" | grep "^Image size"
Image size      : 640 x 960

Code: Select all

1 root:IMG$ exiv2 2rVyRKI.jpg
File name       : 2rVyRKI.jpg
File size       : 600887 Bytes
MIME type       : image/jpeg
Image size      : 2576 x 1719
2rVyRKI.jpg: No Exif data found in the file
It would be interesting to know though.
If you want to embed image into gtkdialog there are <width></width> and <height></height> tags that will scale image of any size to given values.
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#7 Post by slavvo67 »

See 2005 script posted by Tomchuk

http://ubuntuforums.org/archive/index.php/t-49309.html

I only tested it on one JPG but it seemed to work fine.


Slavvo67

User avatar
trio
Posts: 2076
Joined: Sun 21 Dec 2008, 15:50
Location: अनà¥￾मोदना

#8 Post by trio »

Thanks all for replying, I think I'm going to use eviv2 or SFR's script

Regards,

Trio

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#9 Post by 01micko »

You can try netpbm tools

Eg:

Code: Select all

 jpegtopnm barrow-v-bolt-meme20.jpg|pnmfile
jpegtopnm: WRITING PPM FILE
stdin:	PPM raw, 600 by 800  maxval 255
### for jpeg
pngtopnm stamp99.png|pnmfile|grep [0-9]|awk '{print $4,$6}' 
300 200
There is also giftopnm, winicontoppm, ppmtogif, bmptoppm ..and so on.

http://netpbm.sourceforge.net


Working example, noisy on the console though

Code: Select all

#!/bin/sh

usage() {
	echo "$0 somepicture|.png|.jpg|.gif|.ico|.bmp"
		exit
}

[ "$1" ] || usage

case "$1" in
	*.jpg|*.JPG|*.jpeg*|*.JPEG)
			APP="jpegtopnm -nosmooth $*" ;;
	*.png)
			APP="pngtopnm $*" ;;
	*.gif)
			APP="giftopnm $*" ;;
	*.ico)  ## doesn't handle compressed .ico
			## and is inaccurate because they contain several sizes
			APP="winicontoppm $*|ppmtogif|giftopnm" ;;
	*.bmp|*.BMP)
			APP="bmptoppm $*|ppmtojpeg|jpegtopnm" ;;
	*) echo "unsupported" && exit ;;
esac

eval "$APP" |pnmfile|grep [0-9]|awk '{print $4,$6}'

Puppy Linux Blog - contact me for access

User avatar
trio
Posts: 2076
Joined: Sun 21 Dec 2008, 15:50
Location: अनà¥￾मोदना

#10 Post by trio »

Thanks bro

SFR's script with a little modification just fits my need

Regards,


Trio

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

Since this is a programming thread, I will give some insight on how these tools work to identify the image type and various properties.

First the program has to determine the image type. This is done by looking at specific file offsets for a "magic" string or number. I wrote a reimplementation of file --mime-type that does this pretty efficiently here:
https://github.com/technosaurus/MIMEtype

Once the image type is determined, you can get the image properties from predefined offsets for that image type.
for png the magic at offset 0 is (decimal representation of each byte):

here is a basic template

Code: Select all

#include <stdio.h>
union{
	struct { char hdr[8]; int width; int height; }png;
	struct { char hdr[5]; char width; char height; }dummy;
}img;

char png_magic[8]={137, 80, 78, 71, 13, 10, 26, 10};
char dummy_magic[5]="dummy";

#define CHECK(x) if(!memcmp(img.x.hdr,x##_magic,sizeof(x##_magic))) \
	return !printf("%s %dx%d\n",#x,img.x.width,img.x.height)

int main(void) {
	read(0,&img,sizeof(img));
	CHECK(png);
	else CHECK(dummy);
	return 1;
}
thisprogram <mypic.png

There is another way to do this using a loop and sets of structs here:
http://landley.net/hg/toybox/file/tip/t ... er/blkid.c
... but with this way you would need extra parameters for the size of each property (some widths may be 16 bits while other formats use 32 ... some audio formats even use only 7 bits of each byte for packet lengths)
see http://www.aivosto.com/vbtips/imageformats.html
XPM is a special case since the text format causes the offset for width and height to be at various offsets due to the variable length name field that is used for embedding in C.

edit - simplified for little endian (x86 and x86_64) example:

Code: Select all

#include <stdio.h>
union{
 struct{short m;char p[16];int w;int h;char P[2];short b;}bmp;
 struct{int m;char p[2];short w;short h;char P;char b;}gif;
 struct{int m;char p[12];int w;int h;char b;}png;
}i;
int bmp='B'|'M'<<8, gif='G'|'I'<<8|'F'<<16|'8'<<24, png=137|80<<8|78<<16|71<<24;
#define _(x) if(i.x.m==x) return !printf("%s %dx%d@%d\n",#x,i.x.w,i.x.h,i.x.b)
int main(void) {
	fread(&i,1,sizeof(i),stdin);
	_(png); else _(gif); else _(bmp); else return 1;
}
Last edited by technosaurus on Sat 08 Nov 2014, 06:00, edited 2 times in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post by technosaurus »

01micko wrote:You can try netpbm tools
Its pretty easy to turn all of those little netpbm programs into a single multicall binary (couple lines of awk on one of the header files and ~ a minute or two of editing) ... I think I sent them a patch already, but I could look into it again. I cut the on disk size drastically even when statically compiled.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply