1

I am using the following code in Go to resize my images in JPEG and PNG format. So, how do I convert them to progressive and optimized using Imagick. I'm using ImageMagick 6.9.3-8 Q16 x86_64 on ubuntu 14.04

I am saying optimized reason being I used the following command to test whether image size gets reduced or not. But, it increases the output file size.

Command :

convert -strip -interlace Plane input-file.jpg output-file.jpg

Go code :

        size = fmt.Sprintf("%dx%d^+0+0", w, h)
        tx := mw.TransformImage("", size)
        tx.SetImageGravity(imagick.GRAVITY_CENTER)
        offsetX := -(int(w) - int(tx.GetImageWidth())) / 2
        offsetY := -(int(h) - int(tx.GetImageHeight())) / 2
        err := tx.ExtentImage(w, h, offsetX, offsetY)
TheHippo
  • 61,720
  • 15
  • 75
  • 100
Naresh
  • 5,073
  • 12
  • 67
  • 124

2 Answers2

3

Your convert command line strips the image and gives it a planar interlace scheme. An equivalent Go code should call mw.StripImage() and mw.SetImageInterlaceScheme(INTERLACE_PLANE).

[edit] Are you trying to follow this example ? If so, -interlace Plane is responsible for making the image progressive, but this doesn’t make it any smaller. The part that does that is -quality 80, which you can implement in Go by calling mw.SetImageCompressionQuality(80).

Zoyd
  • 3,449
  • 1
  • 18
  • 27
  • I did that but it does it make the images to be progressive by just setting the Interlacescheme to be `INTERLACE_PLANE`? – Naresh Mar 17 '17 at 14:43
  • Yes, but this has nothing to do with size (see edit). – Zoyd Mar 17 '17 at 15:04
  • Yes, my primary goal was to create the image to be progressive and i was trying to follow the same example. So, I didn't use CompressionQuality as mentioned in the example. So, my doubt was why it increased the size compared to original image – Naresh Mar 17 '17 at 17:59
  • Making an image progressive can result in a smaller file, or in a bigger one. There is no rule. – Zoyd Mar 17 '17 at 18:11
  • Ok, thanks. So, doing the same would work for rest of the image format also. Is it correct? Why i am asking is because. I couldn't understand why does rest of the interlace means here `https://godoc.org/gopkg.in/gographics/imagick.v3/imagick#InterlaceType` i.e. `JPEGInterlace` or `pngInterlace` – Naresh Mar 17 '17 at 18:29
  • I tested the code after setting `ImageInterlaceScheme` to be `plane`. But in the browser, it still doesn't show in progressive manner after throttling the bandwidth. – Naresh Mar 20 '17 at 06:08
0

Use ResizeImage instead

tx.ResizeImage(w, h, imagick.FILTER_LANCZOS, 1)

See the resizing example

Benjamin Kadish
  • 1,472
  • 11
  • 20