2017-12-04 2 views
0

Ich versuche ein Skript zu schreiben, das bei meinem Industrie-Porjekt helfen soll, aber ich habe Probleme, diesen Code zum Laufen zu bringen. Sie sollten alle Bilder in einem Verzeichnis abschneiden, sie zuschneiden, wobei die Zuschneidung für jedes Bild gleich ist, und dann das zugeschnittene Bild exportieren.Automatisches Zuschneiden von Bildern mit Python

import sys 
import os 
from PIL import Image 

filepath = "C:\Users\Ellis\Desktop\bunny_test" 
os.listdir = filepath 

# Loop through all provided arguments 
for i in range(1, len(filepath)): 
try: 
    # Attempt to open an image file 
    #filepath = sys.argv[i] 
    image = Image.open(filepath) 
except IOError, e: 
    # Report error, and then skip to the next argument 
    print "Problem opening", filepath, ":", e 
    continue 

# Perform operations on the image here 
image = image.crop(261, 435, 153, 343) 

# Split our origional filename into name and extension 
(name, extension) = os.path.splittext(filepath) 

# Save the image as "(origional_name)_thumb.jpg 
image.save("C:\Users\Ellis\Desktop\cropped", name + '_cropped.jpg') 
+0

Warum sind die unteren drei Zeilen Betrieb nicht innerhalb eingerückt for-Schleife? Dies würde nur das allerletzte Bild auf der Liste beschneiden:/ – Caladbolgll

+1

In was war es nicht funktioniert? Bitte korrigieren Sie auch Ihre Einrückung .. Wenn das ist, wie Ihr Code eingerückt ist, haben Sie eine leere "For" -Schleife – kdopen

Antwort

0

Es gab einige Fehler in Ihrem Quellcode.

  • falsche Vertiefung (obwohl ich glaube, das passiert, wenn Sie Ihren Code kopieren kleistert, da das Programm über nicht einmal die Parse-Schritt passieren würde)
  • fehlt Schleife über Dateinamen, tun dies mit os.listdir()
  • image.crop ein Tupel nimmt, statt, sofern Sie 4 Argumente
  • Join-Pfad und Dateiname mit os.path.join (image.save braucht nicht zwei Argumente)
  • es ist splitext, nicht splittext

ist hier etwas, das funktionieren sollte:

import sys 
import os 
from PIL import Image 

filepath = "C:\Users\Ellis\Desktop\bunny_test" 

# Loop through all provided arguments 
for filename in os.listdir(filepath): 
    if "." not in filename: 
     continue 
    ending = filename.split(".")[1] 
    if ending not in ["jpg", "gif", "png"]: 
     continue 

    try: 
     # Attempt to open an image file 
     image = Image.open(os.path.join(filepath, filename)) 
    except IOError, e: 
     # Report error, and then skip to the next argument 
     print "Problem opening", filepath, ":", e 
     continue 

    # Perform operations on the image here 
    image = image.crop((261, 435, 153, 343)) 

    # Split our origional filename into name and extension 
    name, extension = os.path.splitext(filename) 

    # Save the image as "(origional_name)_thumb.jpg 
    print(name + '_cropped.jpg') 
    image.save(os.path.join("C:\Users\Ellis\Desktop\cropped", name + '_cropped.jpg')) 
+0

Cheers hansapast das hat super funktioniert! Entschuldigung für den schlechten Quellcode, immer noch ein Anfänger, wenn es um Codierung geht. – EllisC

+0

@EllisC froh, dass geholfen hat, und froh, dass ich eine Ihrer ersten Projekte steigern konnte. Jeder hat einmal angefangen. Können Sie bitte die Antwort aktualisieren/akzeptieren, wenn Sie damit zufrieden sind? – hansaplast

Verwandte Themen