2017-05-03 419 views
3

我想調整圖像的大小,但得到一個常量0.8截斷爲整數編譯錯誤。這是我的代碼Golang我怎麼能乘以一個整數和一個浮點數

b := img.Bounds() 

heightImg := b.Max.Y // height of image in pixels 
widthImg := b.Max.X // width of image in pixels 
const a = .80 
height := int(heightImg * a) // reduce height by 20% 
width := int(widthImg * a) // reduce width by 20% 

// resize image below which take in type int, int in the 2nd & 3rd parameter 
new_img := imaging.Resize(img,width,height, imaging.Lanczos) 

我是新來golang但在這裏這個代碼給我的錯誤

height := int(heightImg * a) 
    width := int(widthImg * a) 

如果你想乘花車任何建議將是巨大

+0

您可能會發現閱讀「常量」是如何表達的有用信息。具體來說,在這種情況下,因爲您的原始代碼將整數「a」相乘,常數將轉換爲「int」,以便兼容。 https://blog.golang.org/constants – RayfenWindspear

+0

感謝您的鏈接將讀取,現在 –

回答

11

,你需要將該數字轉換爲浮點數:

height := int(float64(heightImg) * a) 
width := int(float64(widthImg) * a) 
+0

謝謝,這是缺少的東西 –

相關問題