Golang : Fix image: unknown format error
Problem :
You want to find out the dimension of an image file with image.DecodeConfig()
function - to get the color model, width and height. However, while doing so, you get this strange error message :
image : unknown format error
How to fix this problem?
Solutions :
1) The image.DecodeConfig()
function is either reading an empty file or pointer to empty file. Before calling image.DecodeConfig(inputFile)
, perform sanity check on the input file. Is the size zero? Is the file is actually a directory? A common mistake is that the inputFile
parameter only contains the filename, but not the full path. i.e missing the directory.
You may need to use the path/filepath.Abs()
function. See https://www.socketloop.com/references/golang-path-filepath-abs-function-example
2) Also remember, to import image/jpeg
, image/png
and image/gif
and initialize them at the init()
function. See https://www.socketloop.com/references/golang-image-registerformat-function-example
Reference :
https://www.socketloop.com/tutorials/golang-get-dimension-width-and-height-of-image-file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+17.5k Golang : Iterate linked list example
+7.3k Golang : Handling Yes No Quit query input
+13.8k Golang : Fix image: unknown format error
+12.2k Elastic Search : Return all records (higher than default 10)
+12k Golang : calculate elapsed run time
+15.7k Golang : How to reverse elements order in map ?
+8.2k Golang : Generate Datamatrix barcode
+7.2k Golang : Gorrila set route name and get the current route name
+8.7k Golang : automatically figure out array length(size) with three dots
+7.1k Golang : Process json data with Jason package
+21.9k Golang : Repeat a character by multiple of x factor
+6.5k Golang : Derive cryptographic key from passwords with Argon2