Ask Your Question
-1

Converting JPEG to PNG Image With Alpha transparency

asked 2014-05-22 05:15:42 -0600

Farrakh Javed gravatar image

updated 2014-05-22 05:25:58 -0600

FLY gravatar image

Hi guys, I'm trying to convert image from jpeg format to png format. I'm also trying to apply Alpha on image so png save imag must be with alpha channel. I am not able to execute this(result.push_back<mat>(mask);) line in the code successfully.

Mat jpegImageMat = Highgui.imread("mnt/sdcard/aaawatermark/bird.jpg");
Mat gray_image = new Mat(), mask = new Mat(), rgba_image = new Mat();
List<Mat> result = new ArrayList<Mat>();
Core.split(jpegImageMat, result);
Imgproc.cvtColor(jpegImageMat, gray_image, Imgproc.COLOR_BGR2RGBA);
Imgproc.threshold(gray_image, mask, 128, 255, Imgproc.THRESH_BINARY);
result.push_back<Mat>(mask);
Core.merge(result, rgba_image);
Highgui.imwrite("mnt/sdcard/aaawatermark1/rgba.png", rgba_image);

Anybody help me will be appreciated.

edit retag flag offensive close merge delete

Comments

you want a grayscale image as input to threshold:

Imgproc.cvtColor(jpegImageMat, gray_image, Imgproc.COLOR_BGR2GRAY); //  correct flag

and then there's no push_back() ,

result.add(mask);

(apart from that, using a thresholded version of the image as alpha-mask will look very weird)

berak gravatar imageberak ( 2014-05-22 05:38:14 -0600 )edit

@berak Thanks, Let me try with this.

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-22 05:45:47 -0600 )edit

I dont want to convert to gray scale, I want png format image, If we simply save jpg image to png format it is saved but without alpha channel. I need alpha channel in it. Can you tell me how to do that?

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-22 05:48:58 -0600 )edit

here's the main problem: you seem to have no idea, what should be in your alpha channel in the 1st place. no use bothering with code, unless you solve that.

berak gravatar imageberak ( 2014-05-22 05:52:28 -0600 )edit

I want png image with alpha channel that is very simple question. PNG is saved as RGB channels. Where is Alpha like in format RGBA?

Farrakh Javed gravatar imageFarrakh Javed ( 2014-05-22 06:23:37 -0600 )edit

please answer: what should be transparent, and what not ?

again, split -> add your channel, -> merge -> save as png. it just works here.

berak gravatar imageberak ( 2014-05-22 06:36:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-22 06:39:13 -0600

Farrakh Javed gravatar image

Okay, thanks.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-22 05:15:42 -0600

Seen: 4,375 times

Last updated: May 22 '14