Belochka
Published © GPL3+

Photo studio for Barbie dolls

Creating a Photo Studio kit for Barbie using OpenMV H7+ and a Thermal printer for Arduino

IntermediateWork in progressOver 2 days152
Photo studio for Barbie dolls

Things used in this project

Hardware components

OpenMV Cam H7 Plus
×1
openmv lcd shield
×1
Adafruit Mini Thermal Receipt Printer
×1
LM2596 DC-DC Buck Converter
×2
12 V 2A Power Adapter
×1
button
×1

Software apps and online services

OpenMV IDE
https://github.com/Fabien-Chouteau/un_pola
https://github.com/AdaCore/Ada_Drivers_Library

Story

Read more

Code

main.py

Python
import sensor, image, pyb, lcd, time, struct
from Adafruit_Thermal import *
from pyb import Pin


#rtc.datetime((2021, 7, 22, 1, 15, 55, 0, 0))

# Feel free to experiment with heatdots, heattime and heatiniterval
# to nail down your printer's sweet spot :)
#printer = Adafruit_Thermal(bus=3, baudrate=19200, heatdots=5, heatinterval=40)
printer = Adafruit_Thermal(bus=3, baudrate=19200, heatdots=1, heattime=155, heatinterval=1)

mask=[[1,1,1,1,1,1,1,1,1],[1,1,1,1,0,1,1,1,1],[0,1,1,1,1,1,1,1,0],
      [0,1,1,1,0,1,1,1,0],[0,1,0,1,0,1,0,1,0],[1,0,1,0,0,0,1,0,1],
      [0,0,1,0,1,0,1,0,0],[0,0,1,0,0,0,1,0,0],[0,0,0,0,1,0,0,0,0],
      [0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]
global arr1;
RED_LED_PIN = 1
GREEN_LED_PIN = 2
BLUE_LED_PIN = 3
rtc = pyb.RTC()
filename=""
startphoto = Pin('P9', Pin.IN, Pin.PULL_UP)

#rtc.datetime((2021, 7, 22, 1, 15, 55, 0, 0))
pyb.LED(RED_LED_PIN).off()
pyb.LED(BLUE_LED_PIN).off()


def save_snaphot():
    pyb.LED(RED_LED_PIN).on()
    dateTime = rtc.datetime()
    year = str(dateTime[0])
    month = '%02d' % dateTime[1]
    day = '%02d' % dateTime[2]
    hour = '%02d' % dateTime[4]
    minute = '%02d' % dateTime[5]
    second = '%02d' % dateTime[6]
    subSecond = str(dateTime[7])
    filename='photo/'+str(year)+"-"+str(month)+"-"+str(day)+"_"+str(hour)+"-"+str(minute)+"-"+str(second)+".bmp" # Image file name based on RTC
    img=sensor.snapshot()
    lcd.display(img)
    pyb.delay(2000)
    sensor.snapshot().save(filename)
    pyb.LED(RED_LED_PIN).off()
    #print("Done! Reset the camera to see the saved image.")
    #img.draw_string(20, 60, "Save photo", color = (255, 0, 0), scale = 2, mono_space = False)
    #img.draw_string(3, 90, filename, color = (255, 0, 0), scale = 1, mono_space = False)
    img.draw_string(3, 60, "Save photo\n"+filename, color = (200, 0, 0), scale = 1, mono_space = False,
                                                                    char_rotation = 0, char_hmirror = False, char_vflip = False,
                                                                    string_rotation = 0, string_hmirror = False, string_vflip = False)
    lcd.display(img)
    pyb.delay(2000)
    lcd.display(img)
    return filename

#
# https://forum.arduino.cc/t/help-converting-rgb565-to-rgb888/275681
#
#
def convertBMP_toBitmap(fbmp):
    global arr1;
    pyb.LED(GREEN_LED_PIN).on()
    img=image.Image(fbmp)
    img1=img
    lcd.display(img1)
    try:
        with open(fbmp, 'rb') as bmp_file:
            filenamebitmap=fbmp.replace(".bmp","")
            #fbitmap=open("bitmap","wb")
            fbitmap=open(filenamebitmap,"wb")
            arr1 = [[0 for x in range(384)] for y in range(3)]
            # BitmapHeader
            headerbmp = bmp_file.read(14)
            filesize=int.from_bytes(headerbmp[2:6], 'little')
            fileoffset=int.from_bytes(headerbmp[-4:], 'little')
            print("filesize = ",filesize)
            print("fileoffset = ",fileoffset)
            # BitmapHeaderInfo
            #header_info = BitmapHeaderInfo(bmp_file.read(BitmapHeaderInfo.SIZE_IN_BYTES))
            width=128
            height=160
            #
            bmp_file.seek(fileoffset)
            col=0;row=0;
            #for d in range (0,(filesize-fileoffset)/2,1):
            print("create bitmap ",filenamebitmap)
            for row in range (0,160,1):
               img1=image.Image(fbmp)
               img1.draw_string(30, 60, str(row)+" of 160", color = (200, 0, 0), scale = 2, mono_space = False,
                                                                    char_rotation = 0, char_hmirror = False, char_vflip = False,
                                                                    string_rotation = 0, string_hmirror = False, string_vflip = False)
               lcd.display(img1)
               for col in range (0,128,1):
                   bytecolor=bmp_file.read(2)
                   color=int.from_bytes(bytecolor,"little")
                   #print("color= ",hex(color))
                   r = ((color >> 11) & 0x1F);
                   g = ((color >> 5) & 0x3F);
                   b = (color & 0x1F);
                   #r = ((color & 0xF800) >> 11) << 3
                   #g = ((color & 0x7E0) >> 5) << 2
                   #b = ((color & 0x1F)) << 3
                   #grey=(2*r+7*g+1*b)/2
                   grey=(3*r+6*g+1*b)/2
                   #print_args = (hex(r), hex(g), hex(b), grey)
                   #print("r= %s, g= %s, b= %s grey= %d" % print_args)
                   setBits(col,grey)
               arr2 = [[0 for x in range(48)] for y in range(3)]
               for b1 in range (0,3):
                   for b2 in range (0,48):
                       bb=0x00;
                       for b3 in range (0,8):
                          bb=bb+(arr1[b1][b2*8+b3]<<(8-b3-1))
                       arr2[b1][b2]=bb;
                       #bt=str(bb).encode()
                       fbitmap.write(bb.to_bytes(1,sys.byteorder));
                       #print(bb)
               #print(arr1[0]);print(arr2[0])
               #print(arr1[1]);print(arr2[1])
               #print(arr1[2]);print(arr2[2])
        fbitmap.close()
        #filenamebitmap=fbmp.replace(".bmp","")
        #printer.printBitmapFromFile(384, 480, filenamebitmap)
    except OSError as e:
        print('error: {}'.format(e))


    pyb.LED(GREEN_LED_PIN).off()


def setBits(col,g):
    global arr1;
    g=int(g // 25)
    #print("g= %d col= %d",g,col)
    for i in range(0,3):
       for j in range(0,3):
          arr1[i][col*3+j]=mask[g][i*3+j]


def print_snaphot(ff):
    pyb.LED(BLUE_LED_PIN).on()
    filenamebitmap=ff.replace(".bmp","")
    img1=image.Image(ff)
    img1.draw_string(20, 60, "PRINTING", color = (200, 0, 0), scale = 2, mono_space = False,
                                                         char_rotation = 0, char_hmirror = False, char_vflip = False,
                                                         string_rotation = 0, string_hmirror = False, string_vflip = False)
    lcd.display(img1)
    printer.printBitmapFromFile(384, 480, filenamebitmap)
    pyb.LED(BLUE_LED_PIN).off()
    img1=image.Image(ff)
    img1.draw_string(10, 60, "PRINT OK", color = (200, 0, 0), scale = 2, mono_space = False,
                                                         char_rotation = 0, char_hmirror = False, char_vflip = False,
                                                         string_rotation = 0, string_hmirror = False, string_vflip = False)
    lcd.display(img1)

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.LCD) # or sensor.QQVGA (or others)
sensor.skip_frames(time = 2000) # Let new settings take affect.
lcd.init()

while(True):
    lcd.display(sensor.snapshot())
    if startphoto.value()==0:
        #
        filename=save_snaphot()
        print(filename)
        #
        convertBMP_toBitmap(filename)
        #
        print_snaphot(filename)

Credits

Belochka

Belochka

3 projects • 7 followers

Comments