How to code this pixel art in matlab?
조회 수: 43 (최근 30일)
이전 댓글 표시
How do you create a pixel art minimum 15 by 15 array of pixels using at least 3 colors? using this pic?
댓글 수: 0
답변 (1개)
DGM
2023년 2월 26일
편집: DGM
2023년 4월 11일
This is a good use for indexed color.
% define the colors you want to use
% these colors should be unit-scale (scaled WRT [0 1])
CT = [39 181 39;
234 197 197;
160 63 63;
255 255 255;
255 85 85]/255;
% create the image by specifying which colors go where
myberry = [4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 1 1 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 1 4 4 4 1 1 4 4 4;
4 4 4 4 4 5 5 1 1 1 1 1 1 4 4 4 4;
4 4 4 4 5 3 5 5 3 1 1 1 1 1 1 1 4;
4 4 4 5 5 5 5 3 5 5 5 5 5 1 4 4 4;
4 4 4 5 3 5 5 3 5 5 5 3 5 5 4 4 4;
4 4 4 5 5 5 3 5 5 5 5 5 5 5 4 4 4;
4 4 5 5 3 5 5 5 5 5 3 5 5 5 4 4 4;
4 4 5 5 5 5 5 5 5 5 5 5 3 5 4 4 4;
4 4 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4;
4 4 5 5 3 5 5 5 5 3 5 5 5 4 4 4 4;
4 4 4 5 5 5 5 3 5 5 5 5 4 4 4 4 4;
4 4 4 5 5 5 5 5 5 5 5 2 2 2 4 4 4;
4 4 2 2 5 5 5 5 5 5 2 2 2 2 4 4 4;
4 2 2 2 2 5 5 5 2 2 2 2 2 2 4 4 4;
4 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4;
4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4];
% you can display or save indexed-color images directly,
% but you have to always use the appropriate map
imwrite(myberry,CT,'myberry.png') % save it if you want
imshow(myberry,CT) % just display it
% or you can convert the image to RGB
myberryrgb = ind2rgb(myberry,CT);
imwrite(myberryrgb,'myberryrgb.png') % save it if you want
imshow(myberryrgb) % just display it
If you wanted to add grid lines, you could. Here is one way.
% define the colors you want to use
% these colors should be unit-scale (scaled WRT [0 1])
CT = [39 181 39;
234 197 197;
160 63 63;
255 255 255;
255 85 85;
100 100 100]/255; % last entry is the grid color
% create the image by specifying which colors go where
myberry = [4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 1 1 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 1 4 4 4 1 1 4 4 4;
4 4 4 4 4 5 5 1 1 1 1 1 1 4 4 4 4;
4 4 4 4 5 3 5 5 3 1 1 1 1 1 1 1 4;
4 4 4 5 5 5 5 3 5 5 5 5 5 1 4 4 4;
4 4 4 5 3 5 5 3 5 5 5 3 5 5 4 4 4;
4 4 4 5 5 5 3 5 5 5 5 5 5 5 4 4 4;
4 4 5 5 3 5 5 5 5 5 3 5 5 5 4 4 4;
4 4 5 5 5 5 5 5 5 5 5 5 3 5 4 4 4;
4 4 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4;
4 4 5 5 3 5 5 5 5 3 5 5 5 4 4 4 4;
4 4 4 5 5 5 5 3 5 5 5 5 4 4 4 4 4;
4 4 4 5 5 5 5 5 5 5 5 2 2 2 4 4 4;
4 4 2 2 5 5 5 5 5 5 2 2 2 2 4 4 4;
4 2 2 2 2 5 5 5 2 2 2 2 2 2 4 4 4;
4 2 2 2 2 2 2 2 2 2 2 2 4 4 4 4 4;
4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4];
% upscale the image
tiling = size(myberry);
scale = 10; % upscale amount
myberry = imresize(myberry,tiling*scale,'nearest');
myberry([1:10:end end],:) = 6; % add grid lines
myberry(:,[1:10:end end]) = 6; % add grid lines
imshow(myberry,CT) % display it
If you want to recreate the given image, you can do so, but it might help to start with a copy that's not been completely trashed. See the attached file.
inpict = imread('billybob.png'); % RGB image
imshow(inpict) % download the attachment, not the image being shown!
Do with that what you will.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!