analitics

Pages

Wednesday, June 6, 2018

Python 3.6.4 : The qrcode python module .

This python module named qrcode is a suite of tools to Generate QR codes.
Let's start the tutorial with the install steep :
c:\Python364\Scripts>pip install qrcode[pil]
Collecting qrcode[pil]
... 
Successfully installed qrcode-6.0 
Let's see the output for dir:
>>> import qrcode
>>> from qrcode import *
>>> dir(qrcode)
['ERROR_CORRECT_H', 'ERROR_CORRECT_L', 'ERROR_CORRECT_M', 'ERROR_CORRECT_Q', 'LU
T', 'QRCode', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
 '__name__', '__package__', '__path__', '__spec__', 'base', 'constants', 'except
ions', 'image', 'main', 'make', 'run_example', 'util']
>>> dir(qrcode.util)
['ALPHA_NUM', 'BCH_digit', 'BCH_type_info', 'BCH_type_number', 'BIT_LIMIT_TABLE'
, 'BitBuffer', 'G15', 'G15_MASK', 'G18', 'LUT', 'MODE_8BIT_BYTE', 'MODE_ALPHA_NU
M', 'MODE_KANJI', 'MODE_NUMBER', 'MODE_SIZE_LARGE', 'MODE_SIZE_MEDIUM', 'MODE_SI
ZE_SMALL', 'NUMBER_LENGTH', 'PAD0', 'PAD1', 'PATTERN_POSITION_TABLE', 'QRData',
'RE_ALPHA_NUM', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__
', '__name__', '__package__', '__spec__', '_data_count', '_lost_point_level1', '
_lost_point_level2', '_lost_point_level3', '_lost_point_level4', '_optimal_split
', 'base', 'create_bytes', 'create_data', 'exceptions', 'length_in_bits', 'lost_
point', 'mask_func', 'math', 'mode_sizes_for_version', 'optimal_data_chunks', 'o
ptimal_mode', 'pattern_position', 're', 'six', 'to_bytestring', 'xrange']
>>> dir(qrcode.image)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__', 'base']
>>> dir(qrcode.run_example)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defau
lts__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
 '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '
__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module_
_', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex_
_', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> 
This is the test python script I used to test this python module:
import qrcode
from  qrcode import *
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('https://python-catalin.blogspot.com/')
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
with open('my_qr_test.png', 'wb') as qrfile:
    img.save(qrfile)
This is the output of QR file:

Saturday, May 26, 2018

Blender 3D and Roblox with Python .

I spend my free time with my son playing Roblox and in the meantime, I try to introduce him to the world of computers.
However, you can download the player as a 3D object and use it as an avatar.
Here's an issue: The 3D object is hard to set with origins for animation but python and Blender 3D can easily solve this.
You can use BMesh.
As you know:
BMesh is the new Blender mesh system in 2.63, with full support for N-sided polygons instead of only triangles and quads.
The result of this download 3D object has a bad origin:

Let's see the source code:
import bpy
import bmesh
import mathutils 
from mathutils import Vector

context = bpy.context

def origin_to_bottom(obj):
    matrix_world = obj.matrix_world
    local_verts = [Vector(v[:]) for v in obj.bound_box]
    blender_mesh = blender_meshesh.new()
    blender_mesh.from_mesh(obj.data)
    x, y, z = 0, 0, 0
    l = len(local_verts)
    z = min([v.z for v in local_verts])
    local_origin = Vector((0, 0, 0))
    global_origin = matrix_world * local_origin
    for v in blender_mesh.verts:
        v.coord = v.coord - local_origin
    blender_mesh.to_mesh(obj.data)
    matrix_world.translation = global_origin

mesh_objs = [mesh_object for mesh_object in context.selected_objects if mesh_object.type == 'MESH']
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

for my_objects in mesh_objs:
    origin_to_bottom(my_objects)
The result is this: