1
ランダムに生成されたバーコードを背景画像に貼り付けています。変換後の貼り付け画像の座標を保存
このバーコードは、ランダムに回転、傾き、および拡大縮小されています。
このバーコードは、ランダムに背景画像に配置されます。
実際のバーコードの座標を見つけようとしていますが、展開した黒いマスクを無視しています。
私は行列や画像操作の初心者ですので、特に数学の助けに感謝します。
これは、バーコードの座標とともにpdf417genライブラリを使用してバーコードを生成する場所です。
import numpy as np
import os
import random
import sys
from pdf417gen import encode, render_image
from PIL import Image
def generate_barcode(self):
barcode = encode("random text data", columns=5, security_level=5)
scale = 5
ratio = 3
padding = 5
barcode_image = render_image(barcode, scale=scale, ratio=ratio, padding=padding)
barcode_coords = np.array([
[(barcode_image.width - padding)/float(barcode_image.width), (barcode_image.height - padding)/float(barcode_image.height)],
[padding/float(barcode_image.width), (barcode_image.height - padding)/float(barcode_image.height)],
[padding/float(barcode_image.width), padding/float(barcode_image.height)],
[(barcode_image.width - padding)/float(barcode_image.width), padding/float(barcode_image.height)]
])
return (barcode_coords, barcode_image)
バーコードの画像と座標を取得したら、次の操作を行います。
- 次に座標
使用赤のアウトラインを描き、背景画像
ここで私はバーコードイメージを変換し、それをバックグラウンドイメージに貼り付けます。
def composite_images(self, background_image, barcode_coords, barcode_image):
coords = barcode_coords
barcode = barcode_image
# instantiating the transformation variables
scale = random.randrange(4, 50)/100.0
size = int(min(background_image.size) * scale) # background_image.size returns (width, height)
barcode = barcode.resize((int(size * 2.625), size)) # width:height ratio is 2.625:1
rotation = random.randrange(0, 360)
xstretch = random.randrange(0, 100)/100.0
ystretch = random.randrange(0, 100)/100.0
xshear = random.randrange(0, 100)/100.0
yshear = random.randrange(0, 100)/100.0
# set affine transform on the barcode coordinates
affine_transform = get_affine_transform(rotation, xstretch, ystretch, xshear, yshear)
coords = transform_coords(coords, affine_transform, True)
expand_mask = transform_coords(np.array([ # shifts expand mask based on transformation
[0.0, 0.0],
[float(size * 2.625), 0.0],
[float(size * 2.625), float(size)],
[0.0, float(size)]
]), mat, False)
minx = min(expand_mask[:,0])
maxx = max(expand_mask[:,0])
miny = min(expand_mask[:,1])
maxy = max(expand_mask[:,1])
mat_inv = np.linalg.inv(np.array([ # the inverse matrix
[mat[0,0], mat[0,1], -minx],
[mat[1,0], mat[1,1], -miny],
[0,0,1.0]
]))
image_matrix = (mat_inv[0,0], mat_inv[0,1], mat_inv[0,2],
mat_inv[1,0], mat_inv[1,1], mat_inv[1,2])
new_size = (int(maxx-minx), int(maxy-miny))
# set affine transform on the barcode image using data from coordinates affine transformation
barcode = barcode.transform(new_size, method=Image.AFFINE, data=image_matrix)
# paste the barcode image onto a random position on background image
region_x = random.randrange(0, background_image.width - size)
region_y = random.randrange(0, background_image.height - size)
background_image.paste(barcode, (region_x, region_y))
coords *= scale
coords += [region_x/float(background_image.width), region_y/float(background_image.height)]
return(coords, background_image)
def get_affine_transform(self, rotation, xstretch, ystretch, xshear, yshear):
theta = -(rotation/180.0) * np.pi
return np.array([
[np.cos(theta) * xstretch, -np.sin(theta) * xshear],
[np.sin(theta) * ystretch, np.cos(theta) * yshear]
])
def transform_coords(self, coords, affine_transform, center):
if center:
coords -= (.5, .5) # center on origin
coords = np.dot(coords, affine_transform.T)
if center:
coords += (.5, .5) # reset centering
return coords
は、今私はcomposite_images()
から返さCOORDSと(貼り付けられたバーコード付き)画像を使用して赤のアウトラインを描きます。 def draw_red_outline(self, box_coords, image):
outline = box_coords * [image.width, image.height]
outline = outline.astype(int)
outline = tuple(map(tuple, outline))
draw = ImageDraw.Draw(image)
draw.poly(outline, outline=(255,0,0,0))
del draw
image.show()
私の数学がどこで間違っているのか分かりません。