2017-04-06 5 views
1

Hilfe, ich muss einen Rahmen auf das Bild aus dem folgenden Code angezeigt werden. Wie mache ich das? Der Code unten zieht das Bild hoch, und jetzt brauche ich einen Rahmen. Hilfe .. All dieses Codes ist PythonHinzufügen eines Rahmens zu und Bild in meinem Code

# -*- coding: utf-8 -*- 
''' 
LoganCaroline_1_4_7: Merge images 
''' 
import matplotlib.pyplot as plt 
import os.path 
import numpy as np  # “as” lets us use standard abbreviations 

'''Read the image data''' 
# Get the directory of this python script 
directory = os.path.dirname(os.path.abspath(__file__)) 
# Build an absolute filename from directory + filename 
filename = os.path.join(directory, 'family1.jpg') 
# Read the image data into an array 
img = plt.imread(filename) 

'''Show the image data''' 
# Create figure with 1 subplot 
fig, ax = plt.subplots(1, 1) 
# Show the image data in a subplot 
ax.imshow(img, interpolation='none') 
# Show the figure on the screen 
fig.show() 

Antwort

1

nur eine etwas größere Array erstellen, das die zentralen Pixel mit Ihrem Bild schwarz und füllen ist.

import numpy as np 
import matplotlib.pyplot as plt 

def frame_image(img, frame_width): 
    b = frame_width # border size in pixel 
    ny, nx = img.shape[0], img.shape[1] # resolution/number of pixels in x and y 
    if img.ndim == 3: # rgb or rgba array 
     framed_img = np.zeros((b+ny+b, b+nx+b, img.shape[2])) 
    elif img.ndim == 2: # grayscale image 
     framed_img = np.zeros((b+ny+b, b+nx+b)) 
    framed_img[b:-b, b:-b] = img 
    return framed_img 

img = np.random.rand(456, 333, 3) 
fig, (ax1, ax2) = plt.subplots(1,2) 
ax1.imshow(img, interpolation='none') 
ax2.imshow(frame_image(img, 20), interpolation='none') 
plt.show() 

enter image description here

+0

Ich bin in einer Klasse und nur all dies zu lernen. Ich habe versucht, etwas Ähnliches zu tun, und es sagte mir, dass "Bild" nicht definiert wurde. Bekomme ich diese Nachricht mit diesem Code zurück oder funktioniert das? Und wenn ich diese Nachricht zurückbekomme, wie kann ich ein Bild definieren? Ist das eine Sache? – Anonymous

+0

In dem Code-Snippet, das Sie gepostet haben, gibt es keine Variable image, nur eine Variable img. Niemand kann Ihnen helfen, wenn Sie nicht posten: 1) der genaue Code + alle verwendeten Daten (wie die Bilddatei) und 2) die genaue Fehlermeldung. Der von mir gepostete Code wird mit dem von Ihnen geposteten Code kombiniert. Füge meinen Code einfach an dein Snippet an. – Paul

+0

Der Code, den ich hatte, als ich diese Frage einreichte, war der Code, den ich im Programm hatte. Ich habe hinzugefügt, was Sie hatten und jetzt zeigt es mein Bild und Ihr statisches Bild sowie Ihr statisches Bild mit einem Rand. Alles, was ich jetzt brauche, ist, die statische in Ihrem Bild durch meine zu ersetzen. Wie mache ich das? – Anonymous

Verwandte Themen