Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
See: https://github.com/JohannesBuchner/imagehash/blob/4.0/imagehash/__init__.py#L125
"""
parser = PIL.ImageFile.Parser()
parser.feed(image_data)
img = parser.close()
target_size = (__class__.IMG_SIG_SIZE, __class__.IMG_SIG_SIZE)
img.thumbnail(target_size, PIL.Image.BICUBIC)
if img.size != target_size:
logging.getLogger("Cover").debug("Non square thumbnail after resize to %ux%u, unable to compute signature" % target_size)
return None
img = img.convert(mode="RGB")
pixels = img.getdata()
pixel_count = target_size[0] * target_size[1]
color_count = 3
r = bitarray.bitarray(pixel_count * color_count)
r.setall(False)
for ic in range(color_count):
mean = sum(p[ic] for p in pixels) // pixel_count
for ip, p in enumerate(pixels):
if p[ic] > mean:
r[pixel_count * ic + ip] = True
return r
def generate_bitarray(self, length):
return bitarray(
''.join('1' if random.random() > 0.5 else '0' for _ in range(length))
)
def set_bits_positions(data, offset=1):
'''
Returns positions of bits set in a byte array
'''
ba = bitarray(endian='little')
ba.frombytes(bytes(data))
bits = set(
idx + 1 for idx, bit in enumerate(ba) if bit
)
return bits
# Special smilie
x0= bitarray('00111100')
x1= bitarray('01000010')
x2= bitarray('10100101')
x3= bitarray('10000001')
x4= bitarray('10100101')
x5= bitarray('10011001')
x6= bitarray('01000010')
x7= bitarray('00111100')
special_smilie=[x0,x1,x2,x3,x4,x5,x6,x7]
# Special degrees
x0= bitarray('00000000')
x1= bitarray('00011100')
x2= bitarray('00010100')
x3= bitarray('00011100')
x4= bitarray('00000000')
x5= bitarray('00000000')
x6= bitarray('00000000')
x7= bitarray('00000000')
special_degrees=[x0,x1,x2,x3,x4,x5,x6,x7]
'''The mapping dictionary is called to translate a character from the message to be displayed
into the relevant bitarray'''
mapping = {}
mapping['A'] = letter_A
mapping['B'] = letter_B
mapping['C'] = letter_C
mapping['D'] = letter_D
mapping['E'] = letter_E
def get_empty_domain(self):
'''
Returns a bitset covering no examples.
'''
return bitarray([False] * self.n_examples())
def to_bitarray(self, meta, val):
"""Return us the val as a bitarray"""
if type(val) is bitarray:
return val
b = bitarray(endian="little")
if type(val) is str:
# We care about when the single quotes aren't here for when we copy output from `lifx unpack` into a `lifx pack`
# This is because we say something like `lifx pack -- '{"thing": ""}'
# And the quotes cancel each other out
if val in (
"",
"",
):
val = ""
try:
b.frombytes(binascii.unhexlify(val))
except binascii.Error as error:
raise BadConversion(
"Failed to turn str into bytes", meta=meta, val=val, error=error
)
fn = filename
else:
fn = self.filename
sys.stderr.write("BLOOM: loading filter from file: %s\n" % fn)
data = self._readfile(fn)
ld = len(data)
if ld >0:
data = self._decompress(data)
self.header=data[0:10]
try:
sys.stderr.write("HEADER: %s\n" % self.header.encode('hex'))
except:
sys.stderr.write("HEADER: %s\n" % self.header.hex())
if self.header[0:6] == b'BLOOM:':
self.bfilter = bitarray.bitarray(endian='little')
#self.hashid = self.hashfunc(data[10:])
self.bfilter.frombytes(data[10:])
del data
self.hashid = self.hashfunc(self.bfilter.tobytes())
else:
sys.stderr.write("BLOOM: HEADER ERROR, FILTER IS NOT REALIABLE!!!\n")
#self.bfilter = bytearray()
self.bfilter = bitarray.bitarray(endian='little')
#self.hashid = self.hashfunc(data)
self.bfilter.frombytes(data)
del data
self.bitcount = len(self.bfilter)
self.bitset = 0
else:
return False
# IP header
ip_ver = Field(
name='ip.version', domain=BitArray(
value=bitarray('0100'))) # IP Version 4
ip_ihl = Field(name='ip.hdr_len', domain=BitArray(bitarray('0000')))
ip_tos = Field(
name='ip.tos',
domain=Data(
dataType=BitArray(nbBits=8),
originalValue=bitarray('00000000'),
svas=SVAS.PERSISTENT))
ip_tot_len = Field(
name='ip.len', domain=BitArray(bitarray('0000000000000000')))
ip_id = Field(name='ip.id', domain=BitArray(nbBits=16))
ip_flags = Field(name='ip.flags', domain=Data(dataType=BitArray(nbBits=3), originalValue=bitarray('000'), svas=SVAS.PERSISTENT))
ip_frag_off = Field(name='ip.fragment', domain=Data(dataType=BitArray(nbBits=13), originalValue=bitarray('0000000000000'), svas=SVAS.PERSISTENT))
ip_ttl = Field(name='ip.ttl', domain=Data(dataType=BitArray(nbBits=8), originalValue=bitarray('01000000'), svas=SVAS.PERSISTENT))
ip_proto = Field(name='ip.proto', domain=Integer(value=self.upperProtocol, unitSize=AbstractType.UNITSIZE_8, endianness=AbstractType.ENDIAN_BIG, sign=AbstractType.SIGN_UNSIGNED))
ip_checksum = Field(name='ip.checksum', domain=BitArray(bitarray('0000000000000000')))
ip_saddr = Field(name='ip.src', domain=IPv4(self.localIP))
ip_daddr = Field(
name='ip.dst', domain=IPv4(self.remoteIP))
ip_payload = Field(name='ip.payload', domain=Raw())
ip_ihl.domain = Size([ip_ver,
ip_ihl,
ip_tos,
ip_tot_len,
ip_id, ip_flags,
ip_frag_off,
ip_ttl, ip_proto,
def load_blooms_from_ananke(timeseriesdata, bloom_garden):
if "bloomfilters" not in timeseriesdata._h5t:
raise IndexError("No bloom filters to load.")
capacity = timeseriesdata._h5t["timeseries/ids"].shape[0]
bfs = timeseriesdata._h55["bloomfilters"]
attrs = bfs.attrs
for bloom_array in attrs:
new_bloom = ExternalHashBloom(capacity)
new_bloom.bitarray = bitarray(bloom_array)
bloom_garden.blooms[bloom] = new_bloom
# Letter M
x0= bitarray('00000000')
x1= bitarray('01111100')
x2= bitarray('01010100')
x3= bitarray('01010100')
x4= bitarray('01010100')
x5= bitarray('01010100')
x6= bitarray('01010100')
x7= bitarray('00000000')
letter_M=[x0,x1,x2,x3,x4,x5,x6,x7]
# Letter N
x0= bitarray('00000000')
x1= bitarray('00100100')
x2= bitarray('00100100')
x3= bitarray('00110100')
x4= bitarray('00101100')
x5= bitarray('00100100')
x6= bitarray('00100100')
x7= bitarray('00000000')
letter_N=[x0,x1,x2,x3,x4,x5,x6,x7]
# Letter O
x0= bitarray('00000000')
x1= bitarray('00111100')
x2= bitarray('00100100')
x3= bitarray('00100100')
x4= bitarray('00100100')
x5= bitarray('00100100')
x6= bitarray('00111100')
x7= bitarray('00000000')
letter_O=[x0,x1,x2,x3,x4,x5,x6,x7]