Related
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
I am in a real need of a tool that does the following:
You draw with your mouse a curve from a starting point to a finish point and then it exports this to an object, which can then be interpolated as a numpy array to a given number of points.
Is anybody aware of such tool, or a way to achieve something similar?
Thanks
This might get you started. Draw your curve with Photoshop, GIMP or your favourite painting/drawing program and save it as a white line on a black background in PNG/GIF/JPEG/TIFF format, or any other format that PIL/Pillow understands:
Load the image with PIL/Pillow, blur it a little to allow for discontinuities in your hand-drawn-curve, then find the brightest pixel in each column. Save as a ".npy" file that you can load and interpolate to any dimension of array you want:
#!/usr/bin/env python3
import sys
import numpy as np
from PIL import Image, ImageFilter
# Check an image file was supplied
if len(sys.argv) != 2:
print("Usage: curve2array IMAGE", file=sys.stderr)
exit(1)
# Assign filename and open image as greyscale
filename = sys.argv[1]
im = Image.open(filename).convert('L')
# OPTIONAL = Blur the image a little to allow discontinuities in hand-sketched curves
im = im.filter(ImageFilter.GaussianBlur(1))
# Convert image to Numpy array and get row number of brightest pixel in each column
na = np.array(im)
yvals = np.argmax(im, axis=0)
yvals = na.shape[0] - yvals # Make origin at bottom-left instead of top-left
# Write result as ".npy" file
np.save('result.npy', yvals)
My input image is 600px wide by 400 pixels tall, so there are 600 y-values and the last is the largest one at 394 because it's near the top of a 400 px high image. The array looks like this:
array([ 1, 1, 3, 6, 10, 13, 16, 20, 23, 26, 29, 32, 36,
39, 42, 45, 48, 51, 54, 57, 59, 62, 65, 68, 71, 74,
76, 79, 82, 84, 87, 90, 92, 95, 97, 100, 102, 104, 107,
109, 111, 114, 116, 118, 120, 123, 125, 127, 129, 131, 133, 135,
137, 139, 140, 142, 144, 146, 148, 149, 151, 153, 154, 156, 157,
159, 160, 162, 163, 164, 166, 167, 168, 169, 171, 172, 173, 174,
175, 176, 177, 178, 179, 180, 180, 181, 182, 182, 183, 184, 184,
185, 185, 186, 186, 187, 187, 187, 188, 188, 188, 188, 188, 188,
188, 188, 188, 188, 188, 187, 187, 187, 186, 186, 185, 185, 184,
184, 183, 182, 181, 180, 180, 179, 178, 177, 176, 175, 173, 172,
171, 170, 169, 167, 166, 165, 163, 162, 160, 159, 157, 156, 155,
153, 152, 150, 149, 147, 145, 144, 142, 141, 139, 138, 136, 135,
133, 132, 130, 129, 127, 126, 124, 123, 121, 120, 119, 117, 116,
114, 113, 112, 110, 109, 108, 106, 105, 104, 103, 101, 100, 99,
98, 97, 96, 95, 94, 92, 91, 90, 89, 88, 87, 86, 85,
85, 84, 83, 82, 81, 80, 79, 79, 78, 77, 76, 75, 75,
74, 73, 72, 72, 71, 70, 70, 69, 68, 68, 67, 66, 66,
65, 64, 64, 63, 63, 62, 61, 61, 60, 60, 59, 58, 58,
57, 57, 56, 56, 55, 55, 54, 54, 53, 53, 52, 52, 51,
51, 50, 50, 49, 49, 48, 48, 47, 47, 47, 46, 46, 45,
45, 45, 44, 44, 43, 43, 43, 42, 42, 42, 41, 41, 41,
40, 40, 40, 39, 39, 39, 38, 38, 38, 38, 37, 37, 37,
37, 36, 36, 36, 36, 36, 35, 35, 35, 35, 35, 34, 34,
34, 34, 34, 34, 33, 33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35,
36, 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 39,
39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43,
43, 44, 44, 45, 45, 46, 46, 46, 47, 47, 48, 48, 49,
49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 55, 55, 56,
56, 57, 57, 58, 59, 59, 60, 60, 61, 62, 62, 63, 64,
64, 65, 66, 66, 67, 68, 69, 69, 70, 71, 72, 72, 73,
74, 75, 75, 76, 77, 78, 79, 79, 80, 81, 82, 83, 84,
85, 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, 115, 116, 117, 118, 119, 120, 121, 123, 124,
125, 126, 128, 129, 130, 131, 133, 134, 135, 136, 138, 139, 140,
142, 143, 145, 146, 147, 149, 150, 152, 153, 155, 156, 157, 159,
160, 162, 163, 165, 167, 168, 170, 171, 173, 175, 176, 178, 179,
181, 183, 185, 186, 188, 190, 192, 193, 195, 197, 199, 201, 203,
204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 229,
231, 233, 235, 237, 239, 242, 244, 246, 248, 251, 253, 256, 258,
261, 263, 266, 268, 271, 273, 276, 279, 281, 284, 287, 290, 292,
295, 298, 301, 304, 307, 310, 314, 317, 320, 323, 327, 330, 334,
337, 341, 344, 348, 352, 356, 360, 364, 368, 372, 376, 381, 385,
390, 394])
I saved the script above as curve2array, so I run it with:
./curve2array image.png
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:
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ïó>'