resize an image before writing to png

598 views
Skip to first unread message

will

unread,
Mar 17, 2015, 5:34:00 AM3/17/15
to golan...@googlegroups.com
Hi,

It is possible to resize this image to 500 wide x 400 pixels? See code below 

package main

import (
    "image"
    "image/color"
    "image/draw"
    "image/png"
    "os"

)

var (
    white color.Color = color.RGBA{255, 255, 255, 255}
    black color.Color = color.RGBA{0, 0, 0, 255}
    blue  color.Color = color.RGBA{0, 0, 255, 255}
    grey  color.Color = color.RGBA{150, 150, 150, 255}
)

// reference => http://golang.org/doc/articles/image_draw.html
func main() {

    m := image.NewRGBA(image.Rect(0, 0, 640, 480)) 

    // fill m in grey
    draw.Draw(m, m.Bounds(), &image.Uniform{grey}, image.ZP, draw.Src)

    // draw a line
    for i := m.Bounds().Min.X; i < m.Bounds().Max.X; i++ {
        m.Set(i, m.Bounds().Max.Y/2, white)
        m.Set(i, m.Bounds().Max.Y/3, black)
        m.Set(i/2, m.Bounds().Max.Y/4, blue)
    }


    w, _ := os.Create("new.png")
    defer w.Close()
    png.Encode(w, m) 

}

Nigel Tao

unread,
Mar 18, 2015, 12:12:15 AM3/18/15
to will, golan...@googlegroups.com
Import "golang.org/x/image/draw" instead of "image/draw" and you can
use the new Scale methods.

----
package main

import (
"image"
"image/color"
"image/png"
"log"
"os"

"golang.org/x/image/draw"
)

var (
white color.Color = color.RGBA{255, 255, 255, 255}
black color.Color = color.RGBA{0, 0, 0, 255}
blue color.Color = color.RGBA{0, 0, 255, 255}
grey color.Color = color.RGBA{150, 150, 150, 255}
)

// reference => http://golang.org/doc/articles/image_draw.html
func main() {
old := image.NewRGBA(image.Rect(0, 0, 640, 480))
b := old.Bounds()

// fill old in grey
draw.Draw(old, b, &image.Uniform{grey}, image.ZP, draw.Src)

// draw a line
for i := b.Min.X; i < b.Max.X; i++ {
old.Set(i, b.Max.Y/2, white)
old.Set(i, b.Max.Y/3, black)
old.Set(i/2, b.Max.Y/4, blue)
}

new := image.NewRGBA(image.Rect(0, 0, 500, 400))
draw.BiLinear.Scale(new, new.Bounds(), old, old.Bounds(), nil)

w, err := os.Create("new.png")
if err != nil {
log.Fatal(err)
}
defer w.Close()
err = png.Encode(w, new)
if err != nil {
log.Fatal(err)
}
}
----

will

unread,
Mar 18, 2015, 1:31:10 AM3/18/15
to golan...@googlegroups.com, wma...@gmail.com
I get the following:

cannot find package "golang.org/x/image/draw" in any of:
/usr/local/go/src/golang.org/x/image/draw (from $GOROOT)

Nigel Tao

unread,
Mar 18, 2015, 2:02:07 AM3/18/15
to will, golang-nuts
Run:
go get golang.org/x/image/draw
to get that package. (It's not part of the standard library).

will

unread,
Mar 18, 2015, 2:29:27 AM3/18/15
to golan...@googlegroups.com, wma...@gmail.com
Thanks nigel. it worked. Just wondering, why is it not part of the standard library?  

Several observations:
  • I once tried to use "code.google.com/p/draw2d/draw2d", it took a while to go get, which meant the package was big with a lot of dependancies.
  • I tried to draw text using functions(as there is no way of drawing text using the standard libraries) . i really wished it was easy to draw text from golang standard libraries.
  • I tried drawing wave functions using the standard library, somehow, it just doesnt work, there is no smoothen curve option (wished there was one), so as part of the solution i had to cast to float, divide by 1000 then cast back to int and even then the graph  was messy(with gaps in between). I wished the standard library used float instead of int for m.Set 
  • i noticed the y axis increases vertically down in m.Set, is it not possible to increase it vertically up?  mathlab and all graphs increase y vertically up

var x2 int =0
var y2 int =0



        for xn <= int(float64(xz)-float64(length)*0.6) {
            fx = math.Pow(xf, 1)
            xn = x2 + int(fx)
            //fy = math.Cos(math.Pi / (10 - float64(x)))
            fy = math.Pow(float64(fx), 0.3)
            //fy = 5 * math.Pow(float64(fx), 0.5)
            yn = y2 - int(fy)
            x = x + 1
            xf = xf + float64(x)/1000
            m.Set(xn, yn, Green)

        }

I am giving all these observations as i love go and i think the drawing library needs a lot of reworking.

Dan Kortschak

unread,
Mar 18, 2015, 3:40:05 AM3/18/15
to will, golan...@googlegroups.com, wma...@gmail.com
Have you had a look at github.com/gonum/plot for these things?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

will

unread,
Mar 18, 2015, 7:24:22 AM3/18/15
to golan...@googlegroups.com, wma...@gmail.com
Thanks kortschak, i will look at the package

Nigel Tao

unread,
Mar 19, 2015, 2:29:18 AM3/19/15
to will, golang-nuts
On Wed, Mar 18, 2015 at 5:29 PM, will <wma...@gmail.com> wrote:
> Thanks nigel. it worked. Just wondering, why is it not part of the standard
> library?

It's not part of the standard library because its API might still
change, with more experience, and the standard library's API is
frozen.


> I tried to draw text using functions(as there is no way of drawing text
> using the standard libraries) . i really wished it was easy to draw text
> from golang standard libraries.

The code.google.com/p/freetype-go/freetype library might help you
here. Again, "go get code.google.com/p/freetype-go/freetype".


> I tried drawing wave functions using the standard library, somehow, it just
> doesnt work, there is no smoothen curve option (wished there was one), so as
> part of the solution i had to cast to float, divide by 1000 then cast back
> to int and even then the graph was messy(with gaps in between). I wished
> the standard library used float instead of int for m.Set
> i noticed the y axis increases vertically down in m.Set, is it not possible
> to increase it vertically up? mathlab and all graphs increase y vertically
> up

The Y axis traditionally points up in math, but traditionally points
down in computer graphics. The pixel grid is quantized to integers;
you can't set a fractional pixel. Computer graphics is influenced by
math but isn't math.

As Dan suggested, a plotting package might be more appropriate for your work.
Reply all
Reply to author
Forward
0 new messages