Related
hi guys im doing shiftrows aes encryption
def rotate(word, n):
return word[n:]+word[0:n]
def shiftRows(state):
for i in range(4):
state[i*4:i*4+4] = rotate(state[i*4:i*4+4],i)
state = [0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255]
shiftRows(state)
print("row: ", state)
Expected output:
[0, 85, 170, 255, 68, 153, 238, 51, 136, 221, 34, 119, 204, 17, 102, 187]
Current output:
[0, 17, 34, 51, 85, 102, 119, 68, 170, 187, 136, 153, 255, 204, 221, 238]
in order to buy something on MagicEden, I'm using an endpoint called buy_now that sends me back a payload with the following format :
with ['tx']['data'] the message and ['txSigned']['data'] a partially signed transaction. I'm trying to sign the message and to insert it in
the first slot of signatures[] of the transaction.
Payload in entry :
{'tx': {'type': 'Buffer', 'data': [2, 1, 9, 21, 196, 1, 121, 246, 8, 50, 175, 233, 165, 26, 58, 31, 47, 169, 127, 105, 114, 246, 195, 127, 107, 150, 107, 81, 27, 242, 42, 139, 211, 125, 28, 252, 5, 127, 54, 85, 153, 40, 206, 27, 171, 173, 182, 91, 139, 93, 158, 49, 186, 39, 248, 83, 155, 236, 96, 44, 203, 26, 220, 42, 251, 159, 70, 112, 234, 229, 246, 49, 247, 171, 199, 192, 75, 0, 164, 243, 164, 173, 173, 204, 108, 103, 77, 32, 29, 248, 152, 212, 87, 233, 255, 150, 147, 163, 18, 20, 46, 102, 223, 57, 126, 136, 59, 186, 161, 206, 130, 78, 143, 99, 68, 124, 54, 187, 28, 214, 169, 184, 137, 146, 121, 188, 11, 38, 234, 75, 163, 227, 159, 245, 230, 90, 36, 4, 85, 130, 248, 34, 4, 215, 246, 88, 214, 129, 157, 51, 165, 199, 101, 224, 234, 73, 209, 32, 159, 190, 135, 97, 212, 111, 105, 68, 93, 31, 113, 62, 39, 206, 222, 140, 109, 115, 71, 173, 36, 186, 212, 191, 186, 139, 47, 118, 15, 86, 147, 62, 225, 155, 19, 124, 188, 32, 130, 24, 74, 93, 25, 73, 136, 231, 60, 239, 217, 165, 75, 201, 251, 81, 250, 184, 172, 180, 74, 170, 178, 26, 93, 235, 115, 244, 5, 241, 178, 37, 12, 158, 58, 228, 224, 183, 152, 74, 250, 18, 157, 96, 7, 160, 158, 224, 142, 150, 46, 161, 202, 218, 73, 218, 230, 18, 50, 147, 194, 191, 195, 125, 8, 175, 246, 228, 16, 89, 36, 102, 175, 155, 72, 107, 229, 118, 121, 242, 246, 139, 65, 205, 220, 49, 224, 32, 146, 119, 74, 143, 99, 98, 237, 19, 164, 241, 157, 127, 75, 73, 147, 5, 131, 61, 229, 232, 186, 71, 117, 202, 167, 81, 61, 69, 95, 107, 207, 2, 115, 207, 210, 53, 247, 102, 81, 2, 23, 197, 60, 244, 161, 168, 23, 23, 33, 75, 127, 220, 222, 157, 73, 117, 58, 207, 101, 174, 28, 121, 154, 190, 255, 161, 186, 205, 218, 172, 143, 144, 113, 213, 119, 12, 213, 213, 20, 42, 184, 68, 163, 179, 252, 28, 238, 92, 2, 7, 247, 226, 10, 211, 129, 107, 192, 1, 198, 251, 136, 30, 2, 103, 195, 27, 24, 204, 62, 20, 138, 10, 82, 147, 129, 137, 32, 237, 250, 237, 171, 57, 30, 73, 51, 108, 11, 116, 219, 102, 157, 16, 71, 3, 66, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 136, 115, 139, 250, 198, 10, 13, 26, 130, 255, 192, 253, 225, 104, 17, 209, 176, 195, 48, 48, 68, 144, 55, 86, 179, 38, 200, 206, 101, 253, 239, 8, 209, 122, 249, 173, 94, 22, 105, 150, 178, 108, 233, 210, 121, 80, 93, 128, 143, 222, 100, 131, 254, 248, 217, 106, 235, 99, 8, 128, 8, 203, 113, 6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225, 70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133, 126, 255, 0, 169, 6, 167, 213, 23, 25, 44, 92, 81, 33, 140, 201, 76, 61, 74, 241, 127, 88, 218, 238, 8, 155, 161, 253, 68, 227, 219, 217, 138, 0, 0, 0, 0, 140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142, 13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216, 219, 233, 248, 89, 0, 11, 227, 225, 235, 161, 122, 71, 63, 137, 176, 247, 232, 226, 73, 64, 242, 10, 235, 142, 188, 167, 26, 136, 253, 233, 93, 75, 131, 183, 26, 9, 5, 33, 159, 137, 154, 129, 212, 255, 132, 251, 89, 61, 46, 223, 138, 144, 172, 27, 58, 179, 66, 88, 247, 223, 35, 62, 165, 3, 2, 177, 189, 46, 205, 33, 64, 224, 186, 130, 65, 227, 93, 10, 193, 134, 47, 123, 19, 30, 70, 242, 146, 250, 186, 131, 119, 230, 223, 15, 6, 200, 52, 135, 241, 92, 3, 20, 6, 0, 1, 2, 8, 12, 13, 17, 242, 35, 198, 137, 82, 225, 242, 182, 255, 0, 23, 100, 7, 0, 0, 0, 0, 20, 12, 0, 1, 14, 15, 2, 8, 12, 3, 8, 16, 13, 17, 34, 102, 6, 61, 18, 1, 218, 235, 234, 255, 255, 0, 23, 100, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 20, 22, 0, 4, 1, 5, 14, 15, 2, 6, 8, 12, 7, 3, 8, 9, 8, 16, 13, 18, 19, 17, 10, 11, 42, 37, 74, 217, 157, 79, 49, 35, 6, 255, 250, 0, 23, 100, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]}, 'txSigned': {'type': 'Buffer', 'data': [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 236, 72, 248, 210, 27, 195, 225, 195, 236, 128, 200, 183, 128, 195, 174, 10, 166, 181, 191, 241, 121, 21, 87, 232, 145, 169, 146, 168, 241, 1, 66, 204, 164, 143, 99, 121, 7, 163, 201, 176, 220, 191, 220, 99, 140, 228, 151, 111, 113, 138, 82, 86, 148, 253, 143, 194, 250, 145, 241, 152, 57, 242, 0, 2, 1, 9, 21, 196, 1, 121, 246, 8, 50, 175, 233, 165, 26, 58, 31, 47, 169, 127, 105, 114, 246, 195, 127, 107, 150, 107, 81, 27, 242, 42, 139, 211, 125, 28, 252, 5, 127, 54, 85, 153, 40, 206, 27, 171, 173, 182, 91, 139, 93, 158, 49, 186, 39, 248, 83, 155, 236, 96, 44, 203, 26, 220, 42, 251, 159, 70, 112, 234, 229, 246, 49, 247, 171, 199, 192, 75, 0, 164, 243, 164, 173, 173, 204, 108, 103, 77, 32, 29, 248, 152, 212, 87, 233, 255, 150, 147, 163, 18, 20, 8, 175, 246, 228, 16, 89, 36, 102, 175, 155, 72, 107, 229, 118, 121, 242, 246, 139, 65, 205, 220, 49, 224, 32, 146, 119, 74, 143, 99, 98, 237, 19, 46, 102, 223, 57, 126, 136, 59, 186, 161, 206, 130, 78, 143, 99, 68, 124, 54, 187, 28, 214, 169, 184, 137, 146, 121, 188, 11, 38, 234, 75, 163, 227, 159, 245, 230, 90, 36, 4, 85, 130, 248, 34, 4, 215, 246, 88, 214, 129, 157, 51, 165, 199, 101, 224, 234, 73, 209, 32, 159, 190, 135, 97, 212, 111, 105, 68, 93, 31, 113, 62, 39, 206, 222, 140, 109, 115, 71, 173, 36, 186, 212, 191, 186, 139, 47, 118, 15, 86, 147, 62, 225, 155, 19, 124, 188, 32, 130, 24, 74, 93, 25, 73, 136, 231, 60, 239, 217, 165, 75, 201, 251, 81, 250, 184, 172, 180, 74, 170, 178, 26, 93, 235, 115, 244, 5, 241, 178, 37, 12, 158, 58, 228, 224, 183, 152, 74, 250, 18, 157, 96, 7, 160, 158, 224, 142, 150, 46, 161, 202, 218, 73, 218, 230, 18, 50, 147, 194, 191, 195, 125, 164, 241, 157, 127, 75, 73, 147, 5, 131, 61, 229, 232, 186, 71, 117, 202, 167, 81, 61, 69, 95, 107, 207, 2, 115, 207, 210, 53, 247, 102, 81, 2, 23, 197, 60, 244, 161, 168, 23, 23, 33, 75, 127, 220, 222, 157, 73, 117, 58, 207, 101, 174, 28, 121, 154, 190, 255, 161, 186, 205, 218, 172, 143, 144, 113, 213, 119, 12, 213, 213, 20, 42, 184, 68, 163, 179, 252, 28, 238, 92, 2, 7, 247, 226, 10, 211, 129, 107, 192, 1, 198, 251, 136, 30, 2, 103, 195, 27, 24, 204, 62, 20, 138, 10, 82, 147, 129, 137, 32, 237, 250, 237, 171, 57, 30, 73, 51, 108, 11, 116, 219, 102, 157, 16, 71, 3, 66, 75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 136, 115, 139, 250, 198, 10, 13, 26, 130, 255, 192, 253, 225, 104, 17, 209, 176, 195, 48, 48, 68, 144, 55, 86, 179, 38, 200, 206, 101, 253, 239, 8, 209, 122, 249, 173, 94, 22, 105, 150, 178, 108, 233, 210, 121, 80, 93, 128, 143, 222, 100, 131, 254, 248, 217, 106, 235, 99, 8, 128, 8, 203, 113, 6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225, 70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133, 126, 255, 0, 169, 6, 167, 213, 23, 25, 44, 92, 81, 33, 140, 201, 76, 61, 74, 241, 127, 88, 218, 238, 8, 155, 161, 253, 68, 227, 219, 217, 138, 0, 0, 0, 0, 140, 151, 37, 143, 78, 36, 137, 241, 187, 61, 16, 41, 20, 142, 13, 131, 11, 90, 19, 153, 218, 255, 16, 132, 4, 142, 123, 216, 219, 233, 248, 89, 0, 11, 227, 225, 235, 161, 122, 71, 63, 137, 176, 247, 232, 226, 73, 64, 242, 10, 235, 142, 188, 167, 26, 136, 253, 233, 93, 75, 131, 183, 26, 9, 5, 33, 159, 137, 154, 129, 212, 255, 132, 251, 89, 61, 46, 223, 138, 144, 172, 27, 58, 179, 66, 88, 247, 223, 35, 62, 165, 3, 2, 177, 189, 46, 205, 33, 64, 224, 186, 130, 65, 227, 93, 10, 193, 134, 47, 123, 19, 30, 70, 242, 146, 250, 186, 131, 119, 230, 223, 15, 6, 200, 52, 135, 241, 92, 3, 20, 6, 0, 1, 2, 3, 12, 13, 17, 242, 35, 198, 137, 82, 225, 242, 182, 255, 0, 23, 100, 7, 0, 0, 0, 0, 20, 12, 0, 1, 14, 15, 2, 3, 12, 4, 3, 16, 13, 17, 34, 102, 6, 61, 18, 1, 218, 235, 234, 255, 255, 0, 23, 100, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 20, 22, 0, 5, 1, 6, 14, 15, 2, 7, 3, 12, 8, 4, 3, 9, 3, 16, 13, 18, 19, 17, 10, 11, 42, 37, 74, 217, 157, 79, 49, 35, 6, 255, 250, 0, 23, 100, 7, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]}}
and here is my code :
txSigned = tx_data['txSigned']['data']
tx = bytes(tx_data['tx']['data'])
client = AsyncClient("https://api.mainnet-beta.solana.com")
payer = Keypair.from_secret_key(b58decode(mypkey))
sig = payer.sign(tx)
tra = Transaction.deserialize(bytes(txSigned))
tra.signatures[0].signature = sig.signature
tra = Transaction.serialize(tra)
txn = await client.send_raw_transaction(tra)
I keep encountering : 'transaction has not been signed correctly'
Someone with a similar problem but using C# solnet : https://github.com/bmresearch/Solnet/issues/399
is this a bug? thanks a lot
This makes sense, since the previous signature from Magic Eden is getting removed during tra.signatures[0].signature = sig.signature
Instead, you should deserialize the transaction and then sign it yourself, e.g.:
tra = Transaction.deserialize(bytes(txSigned))
tra.sign(payer)
tra = Transaction.serialize(tra)
txn = await client.send_raw_transaction(tra)
Suppose the image x consists of floats in the range [0, 1],
Torchvision adopts the transform of clip(x*255+0.5, 0, 255).as(uint8) .
Skimage seems similar to torch
TensorFlow uses an asymmetric approach
Details on the conversion follow below.
However, while investigating a few things, I found that this method gives an unfairly small chance for values of 0 and 255 compared to other values.
Why do these machine learning libraries use these unfair transformations?
pytorch
https://pytorch.org/vision/main/_modules/torchvision/utils.html#save_image
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(X * 255 + 0.5, 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {17: [0, 255], 32: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 190, 191, 192, 193, 194, 195, 196, 198, 199, 200, 201, 202, 203, 204, 205, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 246, 248, 249, 250, 251, 252, 253, 254], 33: [8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247]})
skimage https://scikit-image.org/docs/dev/user_guide/data_types.html
from skimage.util import img_as_ubyte
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
for K, V in Counter(img_as_ubyte(np.linspace(0/256, 256/256, 32 * 256).reshape(-1, 1, 1)).reshape(-1)).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {17: [0, 255], 32: [1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 108, 109, 110, 111, 112, 113, 114, 116, 117, 118, 119, 120, 121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 157, 158, 159, 160, 161, 162, 163, 164, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 182, 183, 184, 185, 186, 187, 188, 190, 191, 192, 193, 194, 195, 196, 198, 199, 200, 201, 202, 203, 204, 205, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 223, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 246, 248, 249, 250, 251, 252, 253, 254], 33: [8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247]})
tensorflow https://www.tensorflow.org/api_docs/python/tf/image/convert_image_dtype
import tensorflow as tf
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
img = tf.convert_to_tensor(np.linspace(0/256, 256/256, 32 * 256).reshape(-1, 1, 1))
img = tf.image.convert_image_dtype(img, dtype=tf.uint8, saturate=False)
img = tf.reshape(img, -1).numpy()
for K, V in Counter(img).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {33: [0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238], 32: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254], 17: [255]})
my suggestion 1
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(np.rint(X * 256 - 0.5), 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {32: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]})
my suggestion 2
from collections import Counter, defaultdict
import numpy as np
DICT = defaultdict(list)
def as_uint8(X):
return np.clip(X * 256, 0, 255).astype(np.uint8)
for K, V in Counter(as_uint8(np.linspace(0/256, 256/256, 32 * 256))).items():
DICT[V].append(K)
print(DICT)
defaultdict(<class 'list'>, {32: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]})
I'm trying to convert fibonacci series to rgb image. so
import matplotlib.pyplot as plt
import numpy as np
N = int(input("Number of elements in Fibonacci Series, N, (N>=2) : "))
#starting elements: 0, 1
fibonacciSeries = [0,1]
if N>2:
for i in range(2, N):
nextElement = fibonacciSeries[i-1] + fibonacciSeries[i-2]
fibonacciSeries.append(nextElement)
print(fibonacciSeries)
fib_arr = np.array(fibonacciSeries)
fib_arr
img =np.zeros((100,100,4))
rgb = []
for i in fibonacciSeries:
rgb.append(i % 255)
print(rgb)
all this process ı have a list of mod each index of fib_Arr like that!
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 122, 100, 222, 67, 34, 101, 135, 236, 116, 97, 213, 55, 13, 68, 81, 149, 230, 124, 99, 223, 67, 35, 102, 137, 239, 121, 105, 226, 76, 47, 123, 170, 38, 208, 246, 199, 190, 134, 69, 203, 17, 220, 237, 202, 184, 131, 60, 191, 251, 187, 183, 115, 43, 158, 201, 104, 50, 154, 204, 103, 52, 155, 207, 107, 59, 166, 225, 136, 106, 242, 93, 80, 173, 253, 171, 169, 85, 254, 84, 83, 167, 250, 162, 157, 64, 221]
now how to convert this value to RGB image
I try to
plt.imshow(rgb)
plt.savefig("rgb.png")
but doesn't work
edited:
rgb_arr = np.array(rgb)
rgb_arr
array([ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
233, 122, 100, 222, 67, 34, 101, 135, 236, 116, 97, 213, 55,
13, 68, 81, 149, 230, 124, 99, 223, 67, 35, 102, 137, 239,
121, 105, 226, 76, 47, 123, 170, 38, 208, 246, 199, 190, 134,
69, 203, 17, 220, 237, 202, 184, 131, 60, 191, 251, 187, 183,
115, 43, 158, 201, 104, 50, 154, 204, 103, 52, 155, 207, 107,
59, 166, 225, 136, 106, 242, 93, 80, 173, 253, 171, 169, 85,
254, 84, 83, 167, 250, 162, 157, 64])
from PIL import Image
img = Image.fromarray(rgb_arr, 'RGB')
img.save('test.png')
img.show()
picture
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do I convert my byte array to a picture? I want it to be in saved JPG or BMP format, not just displayed as text or on the console.
This is sample array:
[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, 0, 72, 0, 0, 255, 219, 0, 67, 0, 14, 10, 11, 13, 11, 9, 14, 13, 12, 13, 16, 15, 14, 17, 22, 36, 23, 22, 20, 20, 22, 44, 32, 33, 26, 36, 52, 46, 55, 54, 51, 46, 50, 50, 58, 65, 83, 70, 58, 61, 78, 62, 50, 50, 72, 98, 73, 78, 86, 88, 93, 94, 93, 56, 69, 102, 109, 101, 90, 108, 83, 91, 93, 89, 255, 219, 0, 67, 1, 15, 16, 16, 22, 19, 22, 42, 23, 23, 42, 89, 59, 50, 59, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 255, 192, 0, 17, 8, 0, 67, 0, 90, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 27, 0, 0, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 0, 2, 4, 6, 1, 7, 255, 196, 0, 46, 16, 0, 2, 2, 1, 3, 4, 0, 6, 0, 6, 3, 0, 0, 0, 0, 0, 1, 2, 0, 3, 17, 4, 18, 33, 19, 49, 65, 81, 5, 20, 34, 50, 97, 129, 35, 51, 66, 82, 113, 161, 145, 177, 241, 255, 196, 0, 25, 1, 0, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 3, 0, 5, 255, 196, 0, 32, 17, 0, 3, 1, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 17, 3, 33, 49, 4, 18, 65, 50, 81, 97, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 64, 23, 230, 43, 64, 123, 146, 48, 33, 151, 109, 78, 89, 185, 43, 244, 129, 234, 99, 171, 80, 90, 157, 152, 25, 7, 32, 226, 93, 89, 152, 41, 83, 245, 99, 13, 159, 18, 55, 254, 138, 57, 125, 101, 122, 132, 167, 40, 168, 16, 99, 129, 222, 69, 189, 107, 185, 171, 80, 25, 118, 131, 144, 115, 156, 192, 81, 131, 65, 177, 215, 42, 190, 120, 154, 180, 250, 122, 41, 2, 203, 47, 80, 196, 239, 36, 140, 136, 97, 82, 237, 5, 231, 140, 211, 165, 109, 129, 54, 18, 135, 183, 7, 17, 169, 212, 181, 40, 173, 96, 37, 79, 5, 189, 69, 86, 87, 180, 51, 171, 6, 243, 149, 237, 147, 54, 86, 83, 229, 16, 171, 51, 59, 113, 98, 191, 111, 212, 51, 78, 31, 125, 29, 245, 251, 46, 134, 212, 178, 218, 172, 235, 219, 196, 191, 76, 17, 145, 21, 211, 99, 212, 249, 175, 236, 30, 12, 105, 70, 173, 44, 93, 165, 72, 97, 46, 226, 249, 42, 186, 126, 146, 242, 112, 53, 218, 6, 213, 64, 189, 115, 122, 128, 227, 142, 227, 188, 27, 215, 42, 86, 77, 80, 208, 177, 235, 129, 233, 254, 35, 23, 174, 7, 100, 209, 80, 152, 112, 122, 95, 133, 88, 250, 94, 169, 179, 146, 50, 20, 119, 48, 26, 230, 21, 88, 136, 203, 176, 168, 236, 59, 159, 243, 49, 117, 110, 166, 210, 155, 217, 27, 177, 195, 120, 158, 239, 107, 92, 189, 132, 187, 118, 201, 158, 59, 149, 233, 234, 173, 28, 252, 57, 27, 90, 198, 189, 202, 170, 6, 78, 79, 113, 25, 184, 85, 78, 141, 149, 45, 71, 111, 117, 60, 17, 57, 154, 153, 171, 96, 245, 185, 87, 95, 34, 48, 249, 195, 171, 176, 29, 77, 228, 17, 129, 192, 238, 34, 185, 77, 96, 87, 79, 78, 155, 73, 69, 107, 163, 168, 217, 118, 75, 14, 63, 38, 21, 43, 61, 82, 21, 51, 248, 204, 207, 162, 27, 40, 110, 144, 123, 213, 120, 33, 187, 16, 61, 67, 13, 90, 223, 96, 122, 198, 8, 238, 177, 111, 142, 91, 218, 120, 141, 38, 158, 98, 44, 247, 109, 109, 140, 155, 88, 113, 159, 115, 222, 166, 208, 48, 199, 62, 79, 169, 109, 37, 226, 250, 109, 107, 16, 99, 171, 129, 159, 56, 255, 0, 201, 235, 168, 119, 42, 7, 62, 132, 202, 184, 190, 189, 203, 208, 167, 189, 52, 30, 141, 83, 161, 250, 142, 76, 217, 166, 212, 245, 9, 15, 199, 168, 156, 0, 167, 151, 24, 252, 201, 212, 193, 36, 28, 48, 154, 71, 45, 194, 79, 240, 202, 162, 95, 76, 126, 193, 76, 15, 76, 123, 139, 244, 218, 227, 140, 62, 61, 77, 95, 48, 159, 221, 44, 143, 149, 45, 118, 77, 92, 31, 209, 242, 64, 25, 156, 177, 28, 137, 161, 114, 216, 68, 4, 177, 30, 60, 192, 86, 72, 76, 159, 50, 201, 97, 22, 6, 4, 140, 30, 49, 49, 101, 33, 129, 193, 43, 130, 8, 134, 210, 186, 163, 146, 232, 31, 140, 115, 50, 245, 9, 39, 39, 36, 249, 133, 76, 240, 124, 67, 157, 4, 102, 150, 220, 41, 10, 150, 176, 172, 182, 72, 7, 180, 53, 90, 183, 210, 53, 157, 59, 3, 2, 177, 117, 119, 109, 98, 15, 57, 148, 123, 13, 231, 10, 112, 1, 201, 49, 90, 213, 140, 100, 240, 125, 240, 91, 152, 216, 149, 217, 119, 76, 31, 63, 153, 208, 116, 159, 78, 253, 71, 110, 165, 64, 99, 32, 114, 63, 83, 153, 248, 78, 152, 234, 237, 8, 70, 43, 79, 184, 231, 152, 254, 212, 170, 141, 37, 148, 37, 238, 89, 148, 133, 70, 96, 73, 63, 129, 30, 97, 53, 184, 35, 166, 186, 60, 179, 54, 218, 203, 75, 86, 219, 70, 67, 19, 156, 254, 160, 116, 203, 103, 68, 245, 173, 47, 110, 123, 145, 142, 34, 202, 236, 178, 187, 48, 114, 174, 135, 4, 71, 58, 166, 83, 66, 217, 253, 88, 236, 61, 153, 55, 29, 43, 151, 45, 102, 15, 200, 156, 180, 244, 206, 199, 248, 187, 43, 5, 136, 245, 39, 92, 249, 99, 42, 157, 55, 168, 216, 44, 41, 98, 248, 50, 163, 86, 216, 28, 87, 251, 19, 47, 4, 211, 131, 15, 244, 224, 152, 90, 233, 118, 165, 173, 82, 187, 83, 190, 76, 207, 129, 220, 96, 201, 146, 20, 228, 247, 241, 46, 195, 131, 212, 195, 60, 241, 152, 69, 99, 187, 129, 218, 100, 76, 130, 8, 239, 53, 212, 113, 201, 61, 160, 163, 130, 237, 12, 64, 108, 224, 247, 34, 71, 57, 33, 107, 224, 123, 131, 107, 25, 92, 21, 238, 124, 120, 158, 179, 251, 63, 84, 84, 16, 160, 90, 78, 90, 246, 3, 200, 94, 39, 77, 240, 199, 210, 232, 168, 172, 82, 203, 110, 170, 252, 2, 65, 201, 231, 223, 224, 78, 91, 113, 11, 140, 126, 230, 173, 14, 165, 180, 215, 173, 136, 161, 136, 247, 26, 107, 5, 164, 118, 90, 141, 62, 158, 203, 1, 101, 27, 148, 100, 145, 231, 159, 48, 26, 183, 85, 96, 120, 13, 216, 204, 21, 124, 75, 115, 90, 109, 82, 166, 204, 5, 2, 94, 219, 69, 141, 147, 140, 227, 7, 62, 102, 124, 183, 56, 243, 214, 116, 39, 189, 148, 102, 57, 35, 141, 179, 211, 91, 103, 238, 31, 238, 2, 195, 140, 159, 18, 163, 81, 102, 62, 227, 255, 0, 50, 83, 70, 142, 67, 204, 186, 114, 192, 30, 210, 73, 61, 1, 2, 40, 195, 15, 243, 46, 126, 252, 120, 245, 36, 145, 25, 199, 172, 126, 185, 226, 242, 78, 121, 146, 73, 223, 135, 4, 63, 202, 111, 212, 215, 167, 39, 253, 73, 36, 74, 240, 43, 211, 161, 214, 86, 155, 40, 59, 70, 70, 63, 234, 101, 14, 193, 73, 7, 28, 201, 36, 199, 155, 249, 6, 60, 51, 218, 236, 119, 101, 143, 136, 61, 199, 220, 146, 64, 135, 71, 255, 217]
Each MIME type has a signature(magic number). By first bytes its a JPEG img.
# your array
arr = [255, 216, 255, 224, 0, ...]
>>> bytearray(arr[:4])
bytearray(b'\xff\xd8\xff\xe0')
FF D8 FF E0 - its a jpeg signature image.
I tried:
f = open('/tmp/myimage.jpeg', 'wb')
f.write(bytearray(arr))
f.close()
and got a next image: