What ImageMagick command will display the orientation of a JPG file?
Introduction
Images stores more than just pixel information. A lot of information is stored in form of metadata. Images can have multiple metadata in multiple metadata directories. Some examples are: Exif, IPTC, JFIF, Ducky, etc.
Orientation is one of such metadata tag in Exif directory. This metadata informs your display devices on how to orient an Image after decoding the pixel data. This metadata has valid values from 1-8. This metadata is not always present in Images, as one can remove the metadata from images. It is also possible to set this metadata to wrong values such as 0, 9, 17, etc, since this metadata supports 16-bit unsigned values(0-65535).
The image below shows how display of image is affected using this metadata. Image source: https://me94.me/2316.html
Imagemagick Solution
Fetching orientation from Imagemagick(IM) can mean two things. You might be (mostly) interested in fetching what orientation the image has, and the second would be to know the exact value of orientation metadata tag(in rare cases, I assume).
To know the orientation, you thus have two IM commands(small variation in what they output).
identify -format '%[EXIF:orientation]' <InputFileName> [To get exact metadata value.]
identify -format '%[orientation]' <InputFileName> [To get image orientation value, a mapping of exif value to readable term.]
For command 1:-
The output is the exact value even when value may not be valid. For example:- 0,1,5 or even 65535. However, in the absence of this metadata, the output(IMHO ambiguous output) is:
identify: unknown image property "%[EXIF:orientation]" # warning/property.c/ InterpretImageProperties/3785.
For command 2:-
The output is as following:-
Undefined - 0
Undefined - [When no metadata]
TopLeft - 1
TopRight - 2
BottomRight - 3
BottomLeft - 4
LeftTop - 5
RightTop - 6
RightBottom - 7
LeftBottom - 8
Unrecognized - any value between 9-65535, since
there is no mapping from value 9-65535
to some geometry like 'LeftBottom'
Tested on Mac and Ubuntu(EC2)
You can use
identify -format '%[EXIF:Orientation]' <image.jpg>
as per identify -format docs (It's the bit further down about exif metadata).
Try
identify -verbose <image.jpg>
To see what metadata is in the image (for example if the image was not taken with a camera, the orientation tag will not be set).
Alternatively you could do something like
identify -format '%wx%h' <image.jpg>
which gives you the width by height (e.g. '800x598', '1936x2592') and use these to determine whether the image is upright or not (not sure how reliable this is though - sometimes you take a portrait image with a camera and the EXIF data will correctly record the orientation, but the image may still appear landscape).
Related
I am using GIMP 2.10.24. I have some image and I need to change Print Size Width to 21mm and Height to 30mm.
I can do that with Set Image Print Resolution Dialog (Menu->Image->Print Size):
screenshot
But there is my question: how could I do that using script-fu or python-fu?
Print size, size in pixels, and print definition are completely related:
print size = size in pixels รท print definition
So to change the image print definition you use
In Python:
pdb.gimp_image_set_resolution(image, xresolution, yresolution)
In Script-fu:
(gimp-image-set-resolution image xresolution yresolution)
In both case the X/Y resolutions are in dots per inch.
However if you are using Gimp just for this creating a Gimp script is overkill (the learning curve is quite steep). If the image is in a common format (JPEG, PNG, TIFF) the print definition is part of the image metadata (JPEG header, or EXIF data) and can be changed directly without decoding/reencoding the image using CLI utilities. For instance with ExifTool:
exiftool ${your_image} -xResolution=321 -yResolution=321
I am using opencv in python to rotate an image and the original and the resulted images are differrent is somethings, I am doing my transformation through this part of code:
img = cv2.imread("image.tif")
new_image = cv2.getRotationMatrix2D((cols / 2, rows / 2), correction_angle, 1)
dst = cv2.warpAffine(img, new_image , (cols, rows))
cv2.imwrite("Rotated_image.tif", dst)
The original image's size is 1.7 Mb, The image's resolution is 300
dpi, and the color space is YCbCr.
The issue is that the resulting image with 12.5 Mb size, 96 dpi, the color space is RGB, and with compression "LZW"!
My question is that: Can I keep the main properties of the original image? and why rotating an image changes the size this way?
Note: The bit depth is 24 in both images.
Calling cv2.imread with only the name of the file uses the default value cv.IMREAD_COLOR for the second parameter, about which the documentation says:
If set, always convert image to the 3 channel BGR color image.
So, your image is always converted to RGB. You can try using cv.IMREAD_ANYCOLOR for the second parameter of imread, but I don't think you can use cv2.warpAffine on it trivially then.
The difference in the stored DPI information stems from the fact that you write the new image without any meta data. imwrite allows you to specify parameters (see here), but, unfortunately, they are not very well documented. I am not sure if this kind of data can be written out of the box with OpenCV.
I am trying to open a greyscale image in *.data format in GIMP, but the only options that I get are multichannel (RGB, RGB alpha, etc.). Is there a way to change this? Thanks.
File->Open
Choose Raw image data as the file type and find your .data file and open it.
In the proceeding Load Image From Raw Data dialog, set the Image Type to Indexed. Set the Offset to 0 and the Width and Height to the correct dimensions for your data. Leave everything under the Palette section as defaults.
Open in RGB and change Image > Mode > Greyscale?
I need to calculate the image width and height from the actual image file, so I'm reading the image with open file. so I have bunch of characters and numbers and everything that seems meaningless and they are presenting rgb information probably.
I just want to calculate the size of the image with the raw file information
I am programming in Erlang language but the code in any language will help as we are working with raw file as long as we don't use built-in libraries.
Thank you all in advance for help
I found the answer by going to details of each format,
So it works like this
JPG : you can find the width and height after the bytes "255,192,0,17,8" after that its the information for size
PNG : you can find it after "IHDR"
GIF : you can find it after "GIF89a"
there are information for more but this is the most common image types on internet
Thank you all for your time
I assume when you say 'raw' you mean you only have the pixel values.
In this case there isn't always a way to know the width and height.
Say you read 400 pixels. In this case a valid image side may be any whole factorization of 400, e.g. 1x400, 2x200, 4x100, 8x50, 20x20 etc. and transposed as well.
Not to mention the fact that many image formats include some padding for pixel rows that are not multiples of 4, 8 or 16...
The way it is coded in the image file depend on the image type, which hopefully is also coded in the image file. you can have a look at the question Getting Image size of JPEG from its binary for an example with JPEG coding.
If your data is unknown, use Octave and load the image. Then take a look at this page:
http://www.gnu.org/software/octave/doc/interpreter/Displaying-Images.html
for commands to display images. Hopefully with some manipulation it will work. This works for raw images, though there are specific decoders. Once you understand how the image is, you can write the equivalent C code.
I'm running Embedded Linux on an evaluation kit (Zoom OMAP35x Torpedo Development Kit). The board has an LCD and I would like to be able to take screen shots convert them into a gif or png. I can get the raw data by doing the following: "cp /dev/fb0 screen.raw", but I am stumped on how to convert the image into a gif or png format.
I played around with convert from ImageMagick (example: "convert -depth 8 -size 240x320 rgb:./screen.raw -swap 0,2 -separate -combine screen.png"), but have been unable to get an image that looks right.
Does anyone know of any other tools that I could try out? Or does anyone have tips for using ImageMagick?
Take a look at fbgrab, an application that does just that (it saves the framebuffer content as a png).
You can simply capture the framebuffer to a file and open it in any raw image viewer or try online eg: https://rawpixels.net/
cat /dev/fb0 > fbdump
It might not be possible / easy to do it directly with ImageMagick.
The Linux kernel 4.2 documentation https://github.com/torvalds/linux/blob/v4.2/Documentation/fb/api.txt#45 says:
Pixels are stored in memory in hardware-dependent formats. Applications need
to be aware of the pixel storage format in order to write image data to the
frame buffer memory in the format expected by the hardware.
Formats are described by frame buffer types and visuals. Some visuals require
additional information, which are stored in the variable screen information
bits_per_pixel, grayscale, red, green, blue and transp fields.
Visuals describe how color information is encoded and assembled to create
macropixels. Types describe how macropixels are stored in memory. The following types and visuals are supported.
A list of visuals and types follows, but the description is not enough for me to understand the exact formats immediately.
But it seems likely that it might not be a format that ImageMagick will understand directly, or at least you'd have to find out the used format to decide the ImageMagick options.