0

my purpose is using pi camera, capture picture of qr code (till here I already done), and align the picture on one platform to compare them are printed well.

so far I can capture the picture and save it on rasberry destop

environment: raspberrypi 4 Model B 32 bit, linux 5.15.61-v8; execute python3 script; logitech USB camera lens ; run in virtual environment

  • below is the cool python script can capture picture, and save on destop
import cv2
import time
 
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret,frame = cap.read() # return a single frame in variable `frame`
 
# run 60 sec x 4 min
time_end = time.time() + 60*4
 
while time.time() < time_end:
 
    while(True):
        cv2.imshow('img1',frame) #display the captured image
        if cv2.waitKey(60) & 0xFF == ord('y'): #save on pressing 'y'

# put the file path you like            
cv2.imwrite('/home/sam/Desktop/pic.png',frame)
            cv2.destroyAllWindows()
            break
   
    cap.release()
    break

And how can I make the picture align like below example, so that I can compare each qr code check there are correct?

expect align product


now I can add two picture together as format 1 x 2 successed align two picture as format 1 x 2

from picamera import PiCamera
import time
from PIL import Image

#Read the two images
image1 = Image.open('/home/joy/Desktop/pic.png')
image1.show()
image2 = Image.open('/home/joy/Desktop/pic_02.png')
image2.show()

#resize, first image
image1 = image1.resize((426, 240))
image2 = image2.resize((426, 240))
image1_size = image1.size
image2_size = image2.size
new_image = Image.new('RGB',(2*image1_size[0], image1_size[1]), (250,250,250))
new_image.paste(image1,(0,0))
new_image.paste(image2,(image1_size[0],0))
new_image.save("/home/joy/Desktop/pic_final.png","PNG")
new_image.show()

just not sure how can layout as 3x3 or 5x10

j ton
  • 229
  • 9

1 Answers1

0

This two not can be the hint to the solution

Combine several images horizontally with Python

Python Image Library: How to combine 4 images into a 2 x 2 grid?


from picamera import PiCamera
import time
from PIL import Image

def join_images(*rows, bg_color=(0, 0, 0, 0), alignment=(0.5, 0.5)):
    rows = [
        [image.convert('RGBA') for image in row]
        for row
        in rows
    ]

    heights = [
        max(image.height for image in row)
        for row
        in rows
    ]

    widths = [
        max(image.width for image in column)
        for column
        in zip(*rows)
    ]

    tmp = Image.new(
        'RGBA',
        size=(sum(widths), sum(heights)),
        color=bg_color
    )

    for i, row in enumerate(rows):
        for j, image in enumerate(row):
            y = sum(heights[:i]) + int((heights[i] - image.height) * alignment[1])
            x = sum(widths[:j]) + int((widths[j] - image.width) * alignment[0])
            tmp.paste(image, (x, y))

    return tmp


def join_images_horizontally(*row, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
    return join_images(
        row,
        bg_color=bg_color,
        alignment=alignment
    )


def join_images_vertically(*column, bg_color=(0, 0, 0), alignment=(0.5, 0.5)):
    return join_images(
        *[[image] for image in column],
        bg_color=bg_color,
        alignment=alignment
    )

images = [
    [Image.open('/home/joy/Desktop/pic.png'), Image.open('/home/joy/Desktop/pic_03.png')],
    [Image.open('/home/joy/Desktop/pic_02.png'), Image.open('/home/joy/Desktop/pic_04.png')],
]


new_image = join_images(
    *images,
    bg_color='green',
    alignment=(0.5, 0.5)
).show()


new_image.save("/home/joy/Desktop/2x2_align_.png","PNG")


and the output:
https://ibb.co/st6dN10

j ton
  • 229
  • 9