Ich habe eine Reihe von Farben, zum Beispiel:
A=[['#ff00ff', '#00ff00', '#ffff00', '#00ff00', '#0000ff', '#00ffff', '#00ffff'],
['#0000ff', '#0000ff', '#00ffff', '#00ff00', '#ffff00', '#00ff00', '#00ff00'],
['#ffff00', '#0000ff', '#0000ff', '#ffff00', '#ffff00', '#ffff00', '#ffff00'],
['#ffff00', '#ffff00', '#ffff00', '#ffff00', '#00ff00', '#00ff00', '#00ff00'],
['#00ff00', '#ffff00', '#ffff00', '#00ff00', '#00ff00', '#00ff00', '#00ff00'],
['#00ff00', '#00ff00', '#00ff00', '#00ff00', '#00ff00', '#0000ff', '#0000ff'],
['#0000ff', '#00ff00', '#00ff00', '#00ff00', '#0000ff', '#0000ff', '#0000ff']]
Ich möchte dies so in ein Bild umwandeln, dass das Pixel der Koordinate (i,j)
die Farbe hat A[i][j]
.
Frage: Wie mache ich das in Gimp (oder direkt in HTML oder irgendetwas Relevanterem)?
Anmerkung: Das reale Array, das ich konvertieren möchte, ist 5000 mal 5000 (dh 25000000 Pixel), also kann ich das nicht von Hand machen und ich brauche eine automatische Prozedur.
Das folgende Bild ist eine Komprimierung des Ergebnisses:
Was das bedeutet, siehe hier .
Es sind ein paar Zeilen in Python:
from PIL import Image
import numpy
A = […]
convert = lambda string: [int(string[i:i+2],base=16) for i in (1,3,5)]
B = numpy.array([map(convert,line) for line in A], dtype=numpy.uint8)
image = Image.fromarray(B, mode="RGB")
image.save("image.png")
Eine kurze Erklärung:
convert
ist eine Funktion, die einen HTML-Farbstring (wie '#00ff00'
) in eine Liste von Farbwerten (zB [0,256,0]
) umwandelt.B
ist ein Array, in dem jedes Pixel mit konvertiert wird convert
.Image.fromarray
dient dazu, genau solche Arrays in Bilder umzuwandeln.Image
aus der ersten Zeile entferne und hinzufüge from PIL import Image
.<canvas>
Sie können dies einfach mit dem Element und etwas JavaScript tun ... A
ist bereits im JS-Format!
var canvas = document.querySelector("canvas"), // Select our canvas element
ctx = canvas.getContext("2d"), // Save the context we're going to use
width = A[0].length, // Get the width of the array
height = A.length, // Get the height of the array
scale = 10; // Scales the whole image by this amount, set to 1 for default size
// Make sure the canvas is no larger than the size we need
canvas.width = width * scale;
canvas.height = height * scale;
// Loop through each color and draw that section
for(var row = 0; row < height; row++) {
for(var col = 0; col < width; col++) { // Since there are nested arrays we need two for loops
ctx.fillStyle = A[row][col]; // Set the color to the one specified
ctx.fillRect(col * scale, row * scale, scale, scale); // Actually draw the rectangle
}
}
Hinweis : Diese Lösung färbt die Pixel von links nach rechts und dann von oben nach unten, da ich das Format Ihres Arrays so interpretiert habe. Wenn Sie möchten, dass es von oben nach unten und dann von links nach rechts geht, wechseln Sie einfach row
und col
in der fillRect
Zeile wie folgt :
ctx.fillRect(col * scale, row * scale, scale, scale);
Sehen Sie sich hier die Demo an !
var A = read(/home/palcoux/array.txt);
Kennen Sie den guten Code dafür?
Zach Saucier
Sebastian Palcoux