how to convert list to RGB value in python - python

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

Related

How to apply slicing to a pandas DataFrame? [duplicate]

This question already has answers here:
How to slice a pandas DataFrame by position?
(5 answers)
Closed 24 days ago.
I am trying to replace the following code:
DfInt['Closest Service'] = DfInt[
[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]
].idxmin(axis=1)
by something like
DfInt['Closest Service'] = DfInt[[0:224]].idxmin(axis=1)
But this is not working... Anyone an idea?
If need select by labels use DataFrame.loc, : is for select all rows:
DfInt['Closest Service'] = DfInt.loc[:, :223].idxmin(axis=1)
If select by positions - first 223 columns use DataFrame.iloc by 224:
DfInt['Closest Service'] = DfInt.iloc[:, :224].idxmin(axis=1)

convert image from [0.0, 1.0] to [0, 255]

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]})

Removing letters in string at positions that don't correspond to numbers in range in list Python

I have a list of ranges of numbers i.e. [[1, 3, 'C'], [4, 48, 'S'], [49, 54, 'C'], [55, 79, 'S'], [80, 83, 'C'], [84, 115, 'S'], [151, 152, 'C'], [153, 304, 'S'], [305, 305, 'D']].
I have a sequence of characters:
"MSEVTRSLLQRWGASLRRGADFDSWGQLVEAIDEYQILARHLQKEAQAQHNNSEFTEEQKKTIGKIATCLELRSAALQSTQSQEEFKLEDLKKLEPILKNILTYNKEFPFDVQPIPLRRILAPGEEENLEFEEDEEGGAGAGPPDSFSARVPGTLLPRLPSEPGMTLLTIRIEKIGLKDAGQCIDPYITVSVKDLNGIDLTPVQDTPVASRKEDTYVHFNVDIELQKHVERLTKGAAIFFEFKHYKPKKRFTSTKCFAFMEMDEIKPGPIVIELYKKPTDFKRKKLQLLTKKPLYLHLHQSLHKE"
and the numbers in the list correspond to the positions of the characters.
i.e. [1,3,'C'] means characters MSE (characters at position 1-3) are C.
Some of the characters in the string aren't being referenced by numbers in the list i.e. 116-150 aren't being referenced, and as a result I would like to remove these letters from the string.
Another example is when I have the range of numbers:[[45, 45, 'D'], [46, 49, 'C'], [50, 66, 'S'], [67, 101, 'C'], [102, 103, 'S'], [104, 106, 'C'], [107, 108, 'S'], [109, 120, 'C'], [121, 121, 'S'], [122, 123, 'C'], [124, 140, 'S'], [141, 149, 'C'], [150, 176, 'S'], [177, 178, 'C'], [179, 181, 'S'], [182, 194, 'C'], [195, 213, 'S'], [214, 217, 'C']]
This example is corresponding to a string of length 310, and we can see that the characters at position 0-45 are not being referred to, neither are the characters from 218-310 so I would like to remove these too.
I am having trouble creating a function that fixes these three cases of removal, as sometimes only one of the cases is true, or neither of them are true.
I have extracted the digits from the sequence into a list and changed the index as string index starts at 0:
[44, 44, 45, 48, 49, 65, 66, 100, 101, 102, 103, 105, 106, 107, 108, 119, 120, 120, 121, 122, 123, 139, 140, 148, 149, 175, 176, 177, 178, 180, 181, 193, 194, 212, 213, 216]
and my current thought process is:
1) If first number in list != 0, remove from corresponding character from 0 to element at first position - i.e. remove 0 to 45 in example 2.
2) If the difference between every 2nd and 3rd element != 1, i.e. if there is a gap in the middle of the numbers, then remove the letters corresponding to said elements. i.e. remove 116-150 mentioned above in example 1.
I am doing this by splitting the string into 0 to 116 and 150 to end of list and then concatenating the strings.
3) If the last number in the list is not the last character in the string, then remove from last character referenced until the end of the string.
I have tried implementing the above logic, and it works for some cases but not for others.
I have also tried adding the complete list of numbers into a list (i.e) the range of each pair of numbers, and removing the characters at the positions not referenced in list.
x is the list of numbers, strSeq is the string
copyx = x
x[0]=0
veryFirstElementOfX = int(x[0])
veryLastElementOfX = int(x[-1])
removed = 0
reeString = ''
if x[0] != 0:
for i in range(0, x[0]):
reeString = strSeq[x[0]:]
xMinusFirst = x
xMinusFirst.pop(0)
firstElement = int(x[0])
secondElement = int(x[1])
while secondElement != x[-1]:
x.pop(0)
x.pop(0)
firstElement = int(x[0])
secondElement = int(x[1])
difference = secondElement - firstElement
print("FE: " + str(firstElement))
print("SE: " + str(secondElement))
print(difference)
print("\n")
if difference > 1:
firstString = strSeq[0:firstElement]
secondString = strSeq[secondElement:]
reeString = reeString + firstString + secondString
removed = removed + (difference-1)
indexuno = veryFirstElementOfX
print(indexuno)
indexdos = veryLastElementOfX - removed
string1 = reeString[indexuno:indexdos]
I expect the output of a reduced string, with only the letters corresponding to the ranges of numbers in the list.
(i.e.) the characters whose position is not being referenced removed.
The above code works with the first example (i.e.) when characters at position 116-150 need to be removed.
But when I run the code for the second sequence, I get the following error:
7 x.pop(0)
8 firstElement = int(x[0])
----> 9 secondElement = int(x[1])
10 difference = secondElement - firstElement
11 print("FE: " + str(firstElement))
IndexError: list index out of range
Instead of finding what to delete, you could create a new string with what you want to keep.
s = "MSEVTRSLLQRWGASLRRGADFDSWGQLVEAIDEYQILARHLQKEAQAQHNNSEFTEEQKKTIGKIATCLELRSAALQSTQSQEEFKLEDLKKLEPILKNILTYNKEFPFDVQPIPLRRILAPGEEENLEFEEDEEGGAGAGPPDSFSARVPGTLLPRLPSEPGMTLLTIRIEKIGLKDAGQCIDPYITVSVKDLNGIDLTPVQDTPVASRKEDTYVHFNVDIELQKHVERLTKGAAIFFEFKHYKPKKRFTSTKCFAFMEMDEIKPGPIVIELYKKPTDFKRKKLQLLTKKPLYLHLHQSLHKE"
L = [[1, 3, 'C'], [4, 48, 'S'], [49, 54, 'C'], [55, 79, 'S'], [80, 83, 'C'],
[84, 115, 'S'], [151, 152, 'C'], [153, 304, 'S'], [305, 305, 'D']]
new_s = ''
for item in L:
new_s += s[item[0]-1:item[1]]
print(len(new_s), new_s)
Your problem statement is written a little bit confusing, but hopefully, the code below will give you some assistance or at least some ideas on how you can use sets in your logic to make your life much easier.
list_numbers = [
[1, 3, "C"],
[4, 48, "S"],
[49, 54, "C"],
[55, 79, "S"],
[80, 83, "C"],
[84, 115, "S"],
[151, 152, "C"],
[153, 304, "S"],
[305, 305, "D"],
]
# Generate a list of all the numbers given.
numbers = set()
for obj in list_numbers:
num1 = obj[0]
num2 = obj[1]
numbers.update(range(num1, num2 + 1))
# > {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, 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, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305}
print(numbers)
# Retrieve the biggest number.
max_number = max(numbers)
# Generate a set from 0 to max_number.
all_numbers = set(i for i in range(max_number))
# > {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, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304}
print(all_numbers)
# Subtract our current list minus the set.
missing_numbers = all_numbers - set(numbers)
# > {0, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}
print(missing_numbers)
If I understood your prompt right, with the list above you should be able to remove all the indexes in the string that is not mentioned.

How to convert byte array to picture [closed]

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:

python into to char to string coversion

I am taking a cryptography course and I managed to convert a string of hex values to their int equivalents. I know want to convert these to their char equivalent using the chr(x) function that takes a number from 0-255 and returns the corresponding ASCII value. I want to then take all these and show them as one string.
Here is some simple code:
x = [49, 21, 92, 196, 78, 238, 234, 170, 168, 139, 181, 95, 248, 138, 170, 175, 249, 145, 23, 116, 65, 20, 69, 91, 191, 244, 67, 62, 225, 23, 120, 132, 75, 184, 143, 250, 160, 0, 13, 220, 199, 113, 29, 216, 136, 133, 90, 168, 128, 4, 78, 229, 94, 238, 233, 159, 250, 164, 64, 11, 177, 22, 99, 52, 73, 156, 193, 20, 70, 111, 251, 183, 119, 120, 140, 205, 223, 242, 45, 211, 58, 175, 255, 240, 2, 33, 29, 223, 255, 255, 245, 91, 180, 64, 3, 59, 181, 81, 16, 13, 208, 13, 208, 4, 69, 85, 84, 70, 104, 138, 174, 235, 185, 152, 134, 98, 34, 43, 177, 19, 55, 125, 218, 174, 232, 133, 87, 117, 85, 83, 60, 204, 205, 216, 136, 136, 131, 58, 167, 123, 188, 195, 55, 117, 82, 32, 14, 224, 6, 110, 229, 81, 21, 93, 210, 34, 44, 201, 149, 84, 78, 235, 186, 165, 80, 2, 37, 91, 184, 140, 204, 197, 87, 126, 238, 229, 89, 148, 65, 24, 140, 206, 231, 125, 220, 198, 107, 188, 196, 65, 21, 85, 86, 107, 189, 219, 179, 54, 107, 187, 188, 202, 163, 62, 232, 135, 119, 116, 67, 48, 1, 31, 251, 188, 202, 170, 163, 59, 184, 131, 59, 178, 34, 32, 8, 128, 9, 149, 86, 96, 9, 152, 135, 120, 129, 21, 95, 246, 101, 82, 40, 134, 103, 118, 100, 71, 112, 3, 61, 222, 224, 15, 243, 61, 213, 82, 36, 68, 64, 0, 10, 161, 25, 155, 177, 21, 89, 150, 97, 16, 11, 177, 17, 30, 239, 243, 62]
result = ""
for i in x:
print chr(i)
result = result + chr(i)
print result
What puzzles me is that when I do "print chr(i)" I get the character '1' which is ASCII for 42. However as I append this to the "result" string it seems to change. When I then print the "result" in the final line the first character of my string is not a '1'.
Any ideas? Busting my head for 2 days now until i managed to trace the issue with my crypto code to these few lines.
Cheers
Your code is correct. The output just gets mangled because, in the encoding that’s being used, some of the subsequent characters (i.e. after the 1) are control characters, which cause previously written output to be overwritten, when printed to the terminal.
You can easily verify this by looking that there are control characters in your string:
import curses.ascii
num_ctrl = len(filter(curses.ascii.iscntrl, result))
# num_ctrl = 41
Furthermore, you can examine which control characters you’re dealing with, by using the following snippet:
control_chars = [curses.ascii.controlnames[ord(x)] for x in result if curses.ascii.isctrl(x)]
# control_chars = ['NAK', 'ETB', …]
You can look up the meaning of those in the documentation. In particular, note that your string contains the BS control sequence, which corresponds to a backspace.
And lastly, you can filter out control characters and just print the remainder:
print ''.join([x for x in result if not curses.ascii.isctrl(x)])
# 1\�N�ꪨ��_��tAE[��C>�x�K�����q؈�Z��N�^�…
You can use a generator expression, then join to create a single string
>>> ''.join(chr(i) for i in x)
'1\x15\\ÄNîꪨ\x8bµ_ø\x8aª¯ù\x91\x17tA\x14E[¿ôC>á\x17x\x84K¸\x8fú\xa0\x00\rÜÇq\x1dØ\x88\x85Z¨\x80\x04Nå^îé\x9fú¤#\x0b±\x16c4I\x9cÁ\x14Foû·wx\x8cÍßò-Ó:¯ÿð\x02!\x1dßÿÿõ[´#\x03;µQ\x10\rÐ\rÐ\x04EUTFh\x8a®ë¹\x98\x86b"+±\x137}Ú®è\x85WuUS<ÌÍØ\x88\x88\x83:§{¼Ã7uR \x0eà\x06nåQ\x15]Ò",É\x95TN뺥P\x02%[¸\x8cÌÅW~îåY\x94A\x18\x8cÎç}ÜÆk¼ÄA\x15UVk½Û³6k»¼Ê£>è\x87wtC0\x01\x1fû¼Êª£;¸\x83;²" \x08\x80\t\x95V`\t\x98\x87x\x81\x15_öeR(\x86gvdGp\x03=Þà\x0fó=ÕR$D#\x00\n¡\x19\x9b±\x15Y\x96a\x10\x0b±\x11\x1eïó>'

Categories

Resources