Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply vignette and vintage image filter in app

I want to apply vignette and vintage image filters on my image.

I followed this sample code.

I have integrated black &white and sephia filters.This code also provides vignette image filter but it processes it very slow and also app gets crashed for some small images when applying this filter.

Please suggest me how to implement vignette and vintage image filters. Any suggestions will be highly appreciated. Thanks in advance!

like image 647
Gypsa Avatar asked Jan 26 '26 12:01

Gypsa


2 Answers

You can do that fairly easily using GPUImage and its GPUImageSepiaFilter and GPUImageVignetteFilter:

Sepia and vignette filtered image

The above image was generated using the following code:

UIImage *inputImage = [UIImage imageNamed:@"Lambeau.jpg"];

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageSepiaFilter *stillImageFilter = [[GPUImageSepiaFilter alloc] init];
GPUImageVignetteFilter *vignetteImageFilter = [[GPUImageVignetteFilter alloc] init];
vignetteImageFilter.vignetteEnd = 0.6;
vignetteImageFilter.vignetteStart = 0.4;

[stillImageSource addTarget:stillImageFilter];
[stillImageFilter addTarget:vignetteImageFilter];
[stillImageSource processImage];

UIImage *filteredImage = [vignetteImageFilter imageFromCurrentlyProcessedOutput];

There's also a GPUImageGrayscaleFilter, if you wish to apply a grayscale filter to the source image.

As the framework name indicates, this is all GPU-accelerated, so it's far faster than any CPU-bound image processing routines. In fact, you can apply the above filters to live video. An iPhone 4 can run this filter in ~2 ms for 640x480 video frames. Still images are a little slower, due to the round trip through Core Graphics required when going to and from UIImages, but this is still up to 6X faster than CPU-bound filters.

One caveat with the current implementation is that non-A5 devices (iPhone 3G S, iPhone 4, iPad 1) have a texture size limit of 2048x2048, so I can't currently filter images larger than that. I'm working on a way to overcome this limitation, but in the meantime you might need to scale images down to fit within this resolution limit before processing.

like image 86
Brad Larson Avatar answered Jan 29 '26 00:01

Brad Larson


Swift 3. Below the function to apply vintage effect. This function uses Core Image.

Use Core Image Filters to apply any effects for your image.

func processVintageImage(_ image: UIImage) -> UIImage {

    guard let inputImage = CIImage(image: image) else { return image }

    guard let photoFilter = CIFilter(name: "CIPhotoEffectInstant",
                                     withInputParameters: ["inputImage" : inputImage]),
        let photoOutput = photoFilter.outputImage,
        let sepiaFilter = CIFilter(name: "CISepiaTone",
                                   withInputParameters: ["inputImage": photoOutput]),
        let sepiaFilterOutput = sepiaFilter.outputImage,
        let vignetteFilter = CIFilter(name: "CIVignette",
                                      withInputParameters: ["inputImage": sepiaFilterOutput, "inputRadius" : 1.5, "inputIntensity" : 4.0]),
        let vignetteFilterOutput = vignetteFilter.outputImage else { return image }

    let context = CIContext(options: nil)

    let cgImage = context.createCGImage(vignetteFilterOutput, from: inputImage.extent)

    return UIImage(cgImage: cgImage!)
}
like image 41
Kiryl Belasheuski Avatar answered Jan 29 '26 00:01

Kiryl Belasheuski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!