How to code this pixel art in matlab?

28 views (last 30 days)
Jamie Manalo
Jamie Manalo on 26 Feb 2023
Edited: DGM on 11 Apr 2023
How do you create a pixel art minimum 15 by 15 array of pixels using at least 3 colors? using this pic?

Answers (1)

DGM
DGM on 26 Feb 2023
Edited: DGM on 11 Apr 2023
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.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!