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
+18.9k Golang : Display list of time zones with GMT
+6k Fontello : How to load and use fonts?
+6.3k PHP : Proper way to get UTF-8 character or string length
+11.7k Golang : convert(cast) float to string
+5.4k How to check with curl if my website or the asset is gzipped ?
+10.1k Golang : How to get quoted string into another string?
+11.6k How to tell if a binary(executable) file or web application is built with Golang?
+22.1k Golang : Securing password with salt
+35.8k Golang : Integer is between a range
+9.9k Golang : Check if user agent is a robot or crawler example
+20.5k Android Studio : AlertDialog and EditText to get user string input example
+10.2k Golang : Embed secret text string into binary(executable) file