Find width and height of .jpg image
14 views (last 30 days)
Show older comments
I try to find the size of the images, resize them and sort by what kind of picture it is.
I am using the following script:
direction = strcat('/Users/simireizen/Canada Only (30) (up to date 21)/*.jpg')
srcFiles = dir(direction);
table = table;
% for i = 1 : length(srcFiles)
for i = 21
filename = strcat(srcFiles(i).folder,'/',srcFiles(i).name);
im = imread(filename);
[h,w,idk] = size(im);
table.name(i) = string(srcFiles(i).name);
table.width(i) = w;
table.height(i) = h;
table.ratio(i) = table.width(i) / table.height(i);
table.idk(i) = idk;
ratio = w/h;
name = strcat('/Users/simireizen/TestDion/Nieuw/');
if ratio < 1
k=imresize(im,[1101,826]);
newfilename=strcat(name,'verti/',srcFiles(i).name);
imwrite(k,newfilename,'jpg');
elseif ratio > 1.3 && ratio < 1.4
k=imresize(im,[900,1200]);
newfilename=strcat(name,'site/',srcFiles(i).name);
imwrite(k,newfilename,'jpg');
else
k=imresize(im,[2000,3000]);
newfilename=strcat(name,'horizontal/',srcFiles(i).name);
imwrite(k,newfilename,'jpg');
end
end
But I can not find differences between vertical en horizontal pictures.
See pictures:
Vertical:
Horizontal:
Can anyone help me?
6 Comments
DGM
on 24 Nov 2022
Two things to remember:
- The reported geometry depends on the state of the Orientation tag.
- The geometry reported by many applications is WxH instead of HxW, and they generally make no effort to clarify.
I've updated my answer. See if that helps. If not, it would help to have an example image to work from. As I mentioned, I have seen a few images with dumb tag configurations that nothing handles correctly.
Answers (2)
DGM
on 24 Nov 2022
Edited: DGM
on 24 Nov 2022
See this
That said, how do you find the aspect ratio?
A = imread('peppers.png');
[h,w,c,~] = size(A)
ar = w/h
If the image is landscape, the AR is >1. If AR<1, it's portrait. If AR = 1, then it's square. If you're reading this info from metadata alone, you may run into traps depending on how the orientation/rotation tags are set.
EDIT:
Okay, I was wrong about imread() handling orientation tags internally. I forget that I process my photos to get rid of that garbage beforehand. I'm sure there are edge cases, especially with older cameras, but you should be able to determine whether the image is landscape/portrait by using the Orientation and geometry tags.
fname = 'exif-orientation/Portrait_1.jpg';
A = imread(fname);
S = imfinfo(fname);
orient = S.Orientation;
geometry = [S.Height S.Width];
AR = geometry(2)/geometry(1);
imageislandscape = xor(orient <= 4, AR < 1)
Bear in mind that this will report a square image as portrait. You're free to handle that case if you want, but I doubt you're going to run across many snapshots taken with cameras that have square sensors.
You can try this on your own images, or you can try the full set of test images here:
Of course, if your goal is to automatically rotate/flip the image into the correct orientation, that's just a bit more work.
switch orient
case 1
% nothing to do
case 2
A = fliplr(A);
case 3
A = rot90(A,2); % or two flips
case 4
A = flipud(A);
case 5
A = rot90(A,-1);
A = fliplr(A);
case 6
A = rot90(A,-1);
case 7
A = rot90(A,1);
A = fliplr(A);
case 8
A = rot90(A,1);
end
I've attached a simple function that can read a JPG file and do this set of transformations conditionally.
0 Comments
Image Analyst
on 24 Nov 2022
I think @Rik is right. You can't just look at the rows and columns in the image array (in the most general and robust case). You may (for some images) also need to check the orientation metadata. A lot of programs look at this and present the image accordingly. For example if you have a sensor that is wider than it is tall (like all digital SLR cameras) and you turn your camera vertical to snap a "tall" image, you'd want it to look tall in your operating system (File Explorer) or other program. If you were to ignore that then you'd get a wide picture because that is the shape of the sensor. I don't believe imread() pays attention to the orientation value so if your DSLR had been turned 90 degrees, you'd still get a wide image. With a DSLR you'll always get a wide image in MATLAB regardless of what angle you held the camera at since imread, as far as I can tell, ignores the orientation value. So many porgrams look at the orientation flag and display the image to you so that the image has the orientation you took the photo with, rather than the wide/landscape orientation that is intrinsic to the sensor. If they ignored it, "vertical" pictures would look rotated 90 degrees on their side.
Of course if the image does not have an orientation flag you can just look at the number of rows and columns like you did
[h,w,idk] = size(im);
though I'd prefer to use more descriptive variable names. "idk" often means "I don't know". And height and width are built in functions so you don't want to use those either. So what I do is
[rows, columns, numberOfColorChannels] = size(im);
which is really clear and explicit.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!