imwrite saving image incorrectly

I am trying to use app designer to create an app where you can load an image from your desktop, compress or decompress the image and then save the image. I have been able to load the image without any issues however when I save the image it removes the background or makes the whole image black. Heres my code:
[file, path] = uiputfile('.png');
image = imread(app.imagePath);
imwrite(image,fullfile(path,file))
app.imagePath is the path to the original image that I want to save

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 12월 8일

0 개 추천

Does your png image have transparent components? Try saving with the alpha channel.
[file, path] = uiputfile('.png');
[image, ~, amap] = imread(app.imagePath);
imwrite(image,fullfile(path,file), 'Alpha', amap)

댓글 수: 5

Harry West
Harry West 2020년 12월 8일
I added this feture to the code but it gave this error:
Error using writepng>parseInputs (line 349)
The value of 'alpha' is invalid. Expected input to be of size 16x16, but it is of size 0x0.
Ameer Hamza
Ameer Hamza 2020년 12월 8일
That shows your png images do not have an alpha mask. But in that case, the background issues shouldn't happen. Can you attach one of your images?
Image Analyst
Image Analyst 2020년 12월 8일
And, Harry, don't use "image" as the name of your variable because it's an important built-in function that you just blew away.
Harry West
Harry West 2020년 12월 8일
The images with alpha seem to have been fixed however this image also appear to turn black when saved. What issue would this be?
Ok, the issue is not the alpha map; rather, the png file is saved as an indexed image.
Either convert it to rgb image before using imwrite()
[img, cmap] = imread('block.png');
img_rgb = ind2rgb(img, cmap);
imwrite(img_rgb, 'test.png')
or specify colormap in imwrite()
[img, cmap] = imread('block.png');
imwrite(img, cmap, 'test.png')

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 12월 8일

댓글:

2020년 12월 8일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by