How to reduce Image Size and Dimension - go

I want to process local images to make the dimension as Height = 380 PX, Width = 380 px and size is less than 20kb.
I have the following code which reads the file and then change dimension to 380 by 380 and then try to reduce the size.
But size is sometime more than 20 KB if original file is large.How to make sure that file is always less than 20 KB?
how to check size of image after dimension change and size after quality reduced image ? so that I can run a loop.
package main
import (
"bufio"
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/nfnt/resize"
)
func main() {
// Read Local file
mainFile, err := os.Open("C://c.jpg")
if err != nil {
fmt.Println("Erro Occured")
spew.Dump(err)
return
}
defer mainFile.Close()
originalimage, _, err := image.Decode(mainFile)
if err != nil {
fmt.Println("Error ", err)
originalimage, err2 := png.Decode(mainFile)
print(err2)
print(originalimage)
}
// Create new file with reduced dimentions - height width as 380
newImage := resize.Resize(380, 380, originalimage, resize.Lanczos3)
// Create new file with reduced size - Quality down by 50%
var b bytes.Buffer
w1 := bufio.NewWriter(&b)
jpeg.Encode(w1, newImage, &jpeg.Options{50})
f2, err := os.OpenFile("C://temp5.png", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer f2.Close()
b.WriteTo(f2)
}

Related

Binary Encoding/Decoding File in Golang Gives Different Checksum

I'm working on encoding and decoding files in golang. I specifically do need the 2D array that I'm using, this is just test code to show the point. I'm not entirely sure what I'm doing wrong, I'm attempting to convert the file into a list of uint32 numbers and then take those numbers and convert them back to a file. The problem is that when I do it the file looks fine but the checksum doesn't line up. I suspect that I'm doing something wrong in the conversion to uint32. I have to do the switch/case because I have no way of knowing how many bytes I'll read for sure at the end of a given file.
package main
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"os"
)
const (
headerSeq = 8
body = 24
)
type part struct {
Seq int
Data uint32
}
func main() {
f, err := os.Open("speech.pdf")
if err != nil {
panic(err)
}
defer f.Close()
reader := bufio.NewReader(f)
b := make([]byte, 4)
o := make([][]byte, 0)
var value uint32
for {
n, err := reader.Read(b)
if err != nil {
if err != io.EOF {
panic(err)
}
}
if n == 0 {
break
}
fmt.Printf("len array %d\n", len(b))
fmt.Printf("len n %d\n", n)
switch n {
case 1:
value = uint32(b[0])
case 2:
value = uint32(uint32(b[1]) | uint32(b[0])<<8)
case 3:
value = uint32(uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16)
case 4:
value = uint32(uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24)
}
fmt.Println(value)
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, value)
o = append(o, bs)
}
fo, err := os.OpenFile("test.pdf", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer fo.Close()
for _, ba := range o {
_, err := fo.Write(ba)
if err != nil {
panic(err)
}
}
}
So, you want to write and read arrays of varying length in a file.
import "encoding/binary"
// You need a consistent byte order for reading and writing multi-byte data types
const order = binary.LittleEndian
var dataToWrite = []byte{ ... ... ... }
var err error
// To write a recoverable array of varying length
var w io.Writer
// First, encode the length of data that will be written
err = binary.Write(w, order, int64(len(dataToWrite)))
// Check error
err = binary.Write(w, order, dataToWrite)
// Check error
// To read a variable length array
var r io.Reader
var dataLen int64
// First, we need to know the length of data to be read
err = binary.Read(r, order, &dataLen)
// Check error
// Allocate a slice to hold the expected amount of data
dataReadIn := make([]byte, dataLen)
err = binary.Read(r, order, dataReadIn)
// Check error
This pattern works not just with byte, but any other fixed size data type. See binary.Write for specifics about the encoding.
If the size of encoded data is a big concern, you can save some bytes by storing the array length as a varint with binary.PutVarint and binary.ReadVarint

Scanner.Buffer - max value has no effect on custom Split?

To reduce the default 64k scanner buffer (for microcomputer with low memory), I try to use this buffer and custom split functions:
scanner.Buffer(make([]byte, 5120), 64)
scanner.Split(Scan64Bytes)
Here I noticed that the second buffer argument "max" has no effect. If I instead insert e.g. 0, 1, 5120 or bufio.MaxScanTokenSize, I can' t see any difference.
Only the first argument "buf" has consequences. Is the capacity to small the scan is incomplete and if it's to large the B/op benchmem value increases.
From the doc:
The maximum token size is the larger of max and cap(buf). If max <= cap(buf), Scan will use this buffer only and do no allocation.
I don't understand which is the correct max value. Can you maybe explain this to me, please?
Go Playground
package main
import (
"bufio"
"bytes"
"fmt"
)
func Scan64Bytes(data []byte, atEOF bool) (advance int, token []byte, err error) {
if len(data) < 64 {
return 0, data[0:], bufio.ErrFinalToken
}
return 64, data[0:64], nil
}
func main() {
// improvised source of the same size:
cmdstd := bytes.NewReader(make([]byte, 5120))
scanner := bufio.NewScanner(cmdstd)
// I guess 64 is the correct max arg:
scanner.Buffer(make([]byte, 5120), 64)
scanner.Split(Scan64Bytes)
for i := 0; scanner.Scan(); i++ {
fmt.Printf("%v: %v\r\n", i, scanner.Bytes())
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
max value has no effect on custom Split?
No, without split there is the same result. But this wouldn't be possible without split and ErrFinalToken:
//your reader/input
cmdstd := bytes.NewReader(make([]byte, 5120))
// your scanner buffer size
scanner.Buffer(make([]byte, 5120), 64)
The buffer size from the scanner should be larger. This is how I would set buf and max:
scanner.Buffer(make([]byte, 5121), 5120)

How do I draw a filled Rectangular box on jpeg if given two pixels values like for eg:- pt1(0,0) and pt2(480, 240)?

I want to open a jpeg file from localdisk and draw a filled Rectangular box on it in Go, if i give two pixel points value like pt1(0,0) and pt2(480, 240)
Have a look here. You need to decode the image from JPEG, set the pixels in your rectangle to black, then re-encode to JPEG.
Here is a code example, it works if the image is in the same folder as the executable and you give the path like myimg.jpg:
package main
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"log"
"os"
)
func main() {
err := addBlackRectangle("myimg.jpg", 500, 500, 1000, 1000)
if err != nil {
log.Fatal(err)
}
}
func addBlackRectangle(imgPath string, x1, y1, x2, y2 int) (err error) {
r, err := os.Open(imgPath)
if err != nil {
return
}
img, err := jpeg.Decode(r)
if err != nil {
return
}
rect1 := img.Bounds()
rect2 := image.Rect(x1, y1, x2, y2)
if !rect2.In(rect1) {
err = fmt.Errorf("error: rectangle outside image")
return
}
rgba := image.NewRGBA(rect1)
for x := rect1.Min.X; x <= rect1.Max.X; x++ {
for y := rect1.Min.Y; y <= rect1.Max.Y; y++ {
p := image.Pt(x, y)
if p.In(rect2) {
rgba.Set(x, y, color.Black)
} else {
rgba.Set(x, y, img.At(x, y))
}
}
}
outputFile := "rect-" + imgPath
w, err := os.Create(outputFile)
defer w.Close()
err = jpeg.Encode(w, rgba, nil)
return
}

How to open a image in Go to get binary data of black and white pixels?

I've been trying for sometime to open an image in binary mode with Go. In Python I'd use the Pillow and image.open() (rb mode). Example.
img = Image.open("PNG.png")
pix = img.getdata() #where 0 is black and 1 is white pixel
That would open the image with very clean binary of white and black dots like the image below. In go I've tried os.Open(file.jpg) to open the file.. I've tried decoding it with image.Decode(), I've loaded the file into bytes.Buffer, I've tried fmt.Sprintf("%b", data), all of the solutions give a byte array. Converting that byte array to binary looks nothing like the image above. I've also tried encoding/binary and its the same story with just getting bytes and the binary generated isn't what i want...
Most recently I've tried this
package main
import (
"fmt"
"image"
"image/jpeg"
"io"
"log"
"os"
)
// Pixel struct example
type Pixel struct {
R int
G int
B int
A int
}
func main() {
// You can register another format here
image.RegisterFormat("jpg", "jpg", jpeg.Decode, jpeg.DecodeConfig)
file, err := os.Open("/Users/marcsantiago/Desktop/2033bb1b194adace86f99c7bb7d72e81.jpg")
if err != nil {
log.Fatalln("Error: File could not be opened")
}
defer file.Close()
pixels, err := getPixels(file)
if err != nil {
log.Fatalln("Error: Image could not be decoded")
}
black := Pixel{0, 0, 0, 255}
for i := range pixels {
if pixels[i] == black {
fmt.Print("0")
} else {
fmt.Print("1")
}
}
}
func getPixels(file io.Reader) ([]Pixel, error) {
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
var pixels []Pixel
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
pixels = append(pixels, rgbaToPixel(img.At(x, y).RGBA()))
}
}
return pixels, nil
}
// img.At(x, y).RGBA() returns four uint32 values; we want a Pixel
func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) Pixel {
return Pixel{int(r / 257), int(g / 257), int(b / 257), int(a / 257)}
}
So that I can compare the binary against what I expect I converted the rgba to 1 and 0s where 0 == black... it still doesn't match up not even close. Example
Help please. I'm out of ideas. PS. This site http://www.dcode.fr/binary-image, also opens the image and generates the data I'm expecting.
UPDATE:
This is the image i'm working with..
For example,
package main
import (
"bytes"
"fmt"
"image"
"os"
_ "image/jpeg"
)
func main() {
fName := "ggk3Z.jpg"
f, err := os.Open(fName)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// http://www.dcode.fr/binary-image
var txt bytes.Buffer
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, _ := img.At(x, y).RGBA()
bin := "0"
if float64((r+g+b))/3 > 0.5 {
bin = "1"
}
txt.WriteString(bin)
}
txt.WriteString("\n")
}
fmt.Fprint(os.Stdout, txt.String())
}

Aligning text in golang with Truetype

I am trying to render some text on a png in a Golang project using freetype/truetype. As you can see from the attachment, I am trying to render 4 letters in columns - each letter centered in the column. Have used the truetype api to get bounds and widths of the glyphs but have been unable to convert these to give me an accurate offset for each glyph. For example with the O glyph, given the font I using. I get the following dimensions:
Hmetric {AdvanceWidth:543 LeftSideBearing:36}
Bounds {XMin:0 YMin:-64 XMax:512 YMax:704}
Advance width: 512
With the last dimension being returned from GlyphBuf.
I rendered it using the following:
size := 125.00
tileOffset := (int(tileWidth) * i) + int(tileWidth/2)
pt := freetype.Pt(tileOffset, (imgH-newCharHeight)-int(size))
How can I use the glyph dimensions returned by truetype to offset the letters correctly? I have tried using the AdvanceWidth as detailed in this plotinum code (line 160) but that does not give me a consistent result across all glyphs.
As suggested by Simon the correct solution is to use AdvanceWidth:
Crude example:
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"image"
"bufio"
"image/draw"
"image/png"
"image/color"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/golang/freetype"
"os"
)
var (
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
fontfile = flag.String("fontfile", "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf", "filename of the ttf font")
hinting = flag.String("hinting", "none", "none | full")
size = flag.Float64("size", 125, "font size in points")
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
wonb = flag.Bool("whiteonblack", false, "white text on a black background")
text = string("JOJO")
)
func main() {
flag.Parse()
fmt.Printf("Loading fontfile %q\n", *fontfile)
b, err := ioutil.ReadFile(*fontfile)
if err != nil {
log.Println(err)
return
}
f, err := truetype.Parse(b)
if err != nil {
log.Println(err)
return
}
// Freetype context
fg, bg := image.Black, image.White
rgba := image.NewRGBA(image.Rect(0, 0, 1000, 200))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(*dpi)
c.SetFont(f)
c.SetFontSize(*size)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
switch *hinting {
default:
c.SetHinting(font.HintingNone)
case "full":
c.SetHinting(font.HintingFull)
}
// Make some background
// Draw the guidelines.
ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
for rcount := 0; rcount < 4; rcount ++ {
for i := 0; i < 200; i++ {
rgba.Set(250*rcount, i, ruler)
}
}
// Truetype stuff
opts := truetype.Options{}
opts.Size = 125.0
face := truetype.NewFace(f, &opts)
// Calculate the widths and print to image
for i, x := range(text) {
awidth, ok := face.GlyphAdvance(rune(x))
if ok != true {
log.Println(err)
return
}
iwidthf := int(float64(awidth) / 64)
fmt.Printf("%+v\n", iwidthf)
pt := freetype.Pt(i*250+(125-iwidthf/2), 128)
c.DrawString(string(x), pt)
fmt.Printf("%+v\n", awidth)
}
// Save that RGBA image to disk.
outFile, err := os.Create("out.png")
if err != nil {
log.Println(err)
os.Exit(1)
}
defer outFile.Close()
bf := bufio.NewWriter(outFile)
err = png.Encode(bf, rgba)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = bf.Flush()
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Println("Wrote out.png OK.")
}

Resources