GIF RGB array to hex array

Viewed 14

I'd like to make an array of hex values of RGB taken from a GIF file.

I am resizing and change the frames of the original GIF file

img = Image.open(gif_file_name)

frames = np.array([np.array(frame.copy().convert('RGB').getdata(),dtype=np.uint8).reshape(frame.size[1],frame.size[0],3) for frame in ImageSequence.Iterator(img)])

print(frames)

in a result I have got

   [ 82  72  64]
   [ 82  72  64]
   ...
   [ 30  15  11]
   [ 30  15  11]
   [ 30  15  11]]

  [[ 82  72  64]
   [ 82  72  64]
   [ 82  72  64] 
...   
...
   [ 25  12   9]
   [ 23  13   9]
   [ 10   4   3]]]]

But I need to have a hex values of it with altered code, like:

#define Frame 42
#define NUMPIXELS 5
#define Div 5

const uint32_t pic [Frame][Div][NUMPIXELS] = {
    {
        {0x010101, 0x090707, 0x0D0808, 0x120D0C, 0x181111},
        {0x010101, 0x0C0A0A, 0x1D1818, 0x1E1818, 0x0C0706},
        {0x010101, 0x0F0D0D, 0x1D1919, 0x2A2323, 0x292221},
        {0x010101, 0x0C0A0A, 0x0A0606, 0x0C0606, 0x120D0D},
        {0x010101, 0x090707, 0x0B0707, 0x100C0B, 0x0E0A09},
    },
...
    {
        {0x000000, 0x050303, 0x080504, 0x0E0808, 0x130D0C},
        {0x000000, 0x070505, 0x120F0E, 0x181413, 0x130F0D},
        {0x000000, 0x090606, 0x191615, 0x272121, 0x292322},
        {0x000000, 0x060403, 0x080505, 0x0F0909, 0x1C1515},
        {0x000000, 0x050303, 0x060202, 0x0C0707, 0x140F0E},
    },

};

I have started from a code to prepare such values of polar coordinate system (www.dfrobot.com):

import cv2
import os
import math
from PIL import Image

#Array setting
NUMPIXELS = 5 
Div = 5 
Bright = 30  
Led0Bright = 3 
#File creation
file = open(graph.h', 'w')
file.write('#define NUMPIXELS ' + str(NUMPIXELS) + '\n')
file.write('#define Div ' + str(Div) + '\n' + '\n')
#file.write('#define Frame ' + str(Frame) + '\n' + '\n')
file.write('const uint32_t pic [Frame][Div][NUMPIXELS] = {' + '\n')
# Read GIF file
gif_file_name = 'name.gif'
gif = cv2.VideoCapture(gif_file_name)
#Image conversion function
def GifConv(pic, i):
   imgOrgin = cv2.imread(pic) #Read image data
   h, w, _ = imgOrgin.shape #Get image size
   imgAltered = Image.new('RGB', (NUMPIXELS, Div))
file.write('\t{\n')
   for j in range(0, Div):
       file.write('\t\t{')
       for i in range(0, h):
           #Get coordinate color
           rP = int(imgOrgin[h + math.ceil(i * math.cos(2*math.pi/Div*j)),
                        w - math.ceil(i * math.sin(2*math.pi/Div*j)), 2]
                    * ((100 - Led0Bright) / NUMPIXELS * i + Led0Bright) / 100 * Bright /100)
           gP = int(imgOrgin[h + math.ceil(i * math.cos(2*math.pi/Div*j)),
                        w - math.ceil(i * math.sin(2*math.pi/Div*j)), 1]
                    * ((100 - Led0Bright) / NUMPIXELS * i + Led0Bright) / 100 * Bright /100)
           bP = int(imgOrgin[h + math.ceil(i * math.cos(2*math.pi/Div*j)),
                        w - math.ceil(i * math.sin(2*math.pi/Div*j)), 0]
                    * ((100 - Led0Bright) / NUMPIXELS * i + Led0Bright) / 100 * Bright /100)
           file.write('0x%02X%02X%02X' % (rP,gP,bP))
           if i == h:
               file.write('},\n')
           else:
               file.write(', ')
           imgAltered.putpixel((i,j), (rP, gP, bP))
   file.write('\t},\n\n')
#Generate directory to save screen capture
dir_name = "screen_caps"
if not os.path.exists(dir_name):
   os.mkdir(dir_name)
i = 0
while True:
   is_success, frame = gif.read()
   # Exit when the file can not be read
   if not is_success:
       break
   # Write out to an image file
   img_name = str(i) + ".jpg"
   img_path = os.path.join(dir_name, img_name)
   cv2.imwrite(img_path, frame)
   #conversion
   GifConv(img_path, i)
   i += 1
file.write('};' + '\n' + '\n')
file.close()
#Inserting the number of frames at the beginning of the file
with open('graph.h') as f:
   l = f.readlines()
l.insert(0, '#define Frame ' + str(i) + '\n')
with open('graph.h', mode='w') as f:
   f.writelines(l)

There is a couple of questions regarding this for image conversion. But none is working. Maybe someone is able to help with that.

The np.array version is very easy to reproduct, what is the easiest way to create a GIF file from RGB hex to test it?

Regards

0 Answers
Related