2

I am trying to convert an image into grayscale one using GPUImage. I wrote an extension to get my work done. Grayscale thing is okay. But output image has become doubled in size. In my case I need the image to be in exact size. Can someone please help me on this? Any help would be highly appreciated.

This is the extension I wrote

import UIKit
import GPUImage

extension UIImage {

    public func grayscale() -> UIImage?{

        var processedImage = self

        print("1: "+"\(processedImage.size)")

        let image = GPUImagePicture(image: processedImage)

        let grayFilter = GPUImageGrayscaleFilter()
        image?.addTarget(grayFilter)

        grayFilter.useNextFrameForImageCapture()
        image?.processImage()
        processedImage = grayFilter.imageFromCurrentFramebuffer()

        print("2: "+"\(processedImage.size)")

        return processedImage
    }

}

This is the output in console

enter image description here

Edit: I know the image can be resized later on. But need to know why is this happening and is there anything to do to keep the image size as it is using GPUImage.

Hanushka Suren
  • 723
  • 3
  • 10
  • 32

1 Answers1

1

Try to scale the image later:

if let cgImage = processedImage.cgImage {
     //The scale value 2.0 here should be replaced by the original image's scale. 
     let scaledImage = UIImage(cgImage: cgImage, scale: 2.0, orientation: processedImage.imageOrientation)
     return scaledImage
}
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • I know it can be resized. But I need a perfect solution other than resizing. – Hanushka Suren Sep 12 '17 at 06:55
  • As @Kevin Ballard said, GPUImage is giving an image of scale 1. That's why you need to change the scale if the original scale is not 1. Check the original scale and apply it into the method in my answer. Otherwise, you need to change source code of GPUImage to do the same thing, I don't think it's a good option. And my answer is not about resizing, it's for creating a new image with a good scale value. – Yun CHEN Sep 12 '17 at 08:15
  • Oops Im sorry. It is not resizing, yes. You are correct. I do resize the original image before calling this extension method. And there i have set the scale to 2. Since GPUImage outputs scale 1 image, final image has become double in size. That is the reason. Thanks friend. – Hanushka Suren Sep 12 '17 at 10:08