[Go] get image width height (image: unknown format)

2022. 9. 15. 23:46· Language/Go

Go 에서 image 의 width , height 가져 올 때,

image: unknown format

이런 오류가 발생한다면..

 

https://pkg.go.dev/image

 

image package - image - Go Packages

Documentation ¶ Package image implements a basic 2-D image library. The fundamental interface is called Image. An Image contains colors, which are described in the image/color package. Values of the Image interface are created either by calling functions

pkg.go.dev

거의 내장된 image package 를 사용할거고..

package main

import (
	"fmt"
	"image"
	"os"
)


func main() {
	imagePath := "gopher.jpg"
	
	file, err := os.Open(imagePath)
	if err != nil {
		panic(err)
	}
	defer file.Close()
    
	image, _, err := image.DecodeConfig(file)
	if err != nil {
		panic(err)
	}
	fmt.Println("Width:", image.Width, "Height:", image.Height)    
}

image.DecodeConfig 함수를 사용하여 쉽게 가져 올 수 있는데..

보통 에디터를 사용하여 자동완성으로

import 되어 저렇게 함수 작성시

"fmt", "image", "os" 3개의 package 를 볼 수 있는데


여기서 "image" 만 있으면

image: unknown format

오류 발생한다.

이 부분때문에 시간을 소요했는데.. 이래서 꼭 Document 를 보는게 낫다.

 

위에 링크 들어가면

import _ "image/png"

이렇게 따로 또 선언해줘야한다.

그래야 unknown format 오류 안남!

import (
	"image"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
)

 

 

이렇게 전부 import 해서 사용하자!

 

 

만약에 이 방법으로 안된다?

그러면 File Seek 가 잘못되었을 확률이 높다.

func (*os.File).Seek(offset int64, whence int) (ret int64, err error)
Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.

If f is a directory, the behavior of Seek varies by operating system; you can seek to the beginning of the directory on Unix-like operating systems, but not on Windows.

파일을 읽고 있는 위치를 잡아 줘야 한다.

 

file, err := os.Open(imagePath)
defer file.Close()
if err != nil {
	panic(err)
}
file.Seek(0, 0)

Seek 를 첫 위치로 잡아주면 해결 될 것이다.

'Language > Go' 카테고리의 다른 글

[Go] Contains method for a slice (go version 1.18)  (0) 2022.09.16
[Go] filetype (MIME type)  (0) 2022.09.16
[Go] get file extension  (0) 2022.09.14
[Go] timestamp Asia/Seoul  (0) 2022.09.11
[Go] Gin vs Echo vs Fiber Framework  (0) 2022.08.21
'Language/Go' 카테고리의 다른 글
  • [Go] Contains method for a slice (go version 1.18)
  • [Go] filetype (MIME type)
  • [Go] get file extension
  • [Go] timestamp Asia/Seoul
임쟌
임쟌
임쟌
Jian's Blog
임쟌
전체
오늘
어제

공지사항

  • [자기소개]
  • 쟌's Blog (227)
    • Language (32)
      • Python (8)
      • Go (24)
      • Java (0)
    • Framework (10)
      • Django (9)
      • Gin (1)
      • Spring boot (0)
      • Fiber (0)
    • Database (10)
      • PostgreSQL (8)
      • MySQL (0)
      • Redis (2)
    • Server (51)
      • Linux (16)
      • Git (12)
      • Oracle Cloud Infrastructure (13)
      • Mac (4)
      • Docker (4)
      • RabbitMQ (0)
      • ETC (2)
    • Operating System (0)
      • OS (0)
    • Algorithm (22)
      • Go (22)
      • Python (0)
    • Exam Certification (4)
    • Daily Life (27)
      • Review (21)
      • Diary (6)
    • 이공계전문기술연수 (71)
      • Java (17)
      • Database (8)
      • HTML | CSS (13)
      • JavaScript | jQuery (6)
      • Servlet | JSP (16)
      • Spring Framework (11)

인기 글

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
임쟌
[Go] get image width height (image: unknown format)
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.