Automatische Batch-Größenänderung von Bildern durch Hinzufügen von Leerraum (d. h. Originalproportionen werden nicht verzerrt)

Ich habe eine Reihe von Fotos, von denen jedes eine andere Größe und Auflösung hat. Ich möchte sie alle quadratisch machen - die Auflösung ist nicht kritisch und kann je nach Foto unterschiedlich sein, aber jedes Ausgabebild muss im Verhältnis quadratisch sein.

Ich muss auch sicherstellen, dass das Originalfoto in keiner Weise verzerrt oder schief ist - ich nehme an, dass dies durch Hinzufügen von Leerraum am unteren Rand eines horizontalen rechteckigen Fotos (zum Beispiel) erreicht werden kann, aber es könnte andere Möglichkeiten geben, dies zu erreichen ...

Jede Hilfe wäre toll!

Danke

Antworten (3)

Sie können ImageMagick verwenden, um die Größe des Bildes zu ändern und es auf einer größeren Leinwand mit weißem Hintergrund zu platzieren. Wenn Sie Mac oder Linux verwenden, können Sie Dateien in einer for-Schleife verarbeiten:

mkdir output
for i in *.jpg ; do
   convert "$i" -resize 4096x4096 -gravity center -background white -extent 4096x4096  "output/${i%.jpg}-processed.jpg"
done

Sie können verwenden parallel, um mehrere Dateien gleichzeitig zu verarbeiten:

mkdir output
for i in *.jpg ; do
   echo convert \"$i\" -resize 4096x4096 -gravity center -background white -extent 4096x4096  \"output/${i%.jpg}-processed.jpg\"
done | parallel

Ich habe etwas Ähnliches getan, um Bilder mit genau denselben Abmessungen für eine HTML-Diashow zu erstellen, selbst wenn die Originaldatei beschnitten oder im Hoch- oder Querformat ausgerichtet war. Ich habe Gimp und ein Schemaskript verwendet, um den Prozess zu vereinfachen, da diese Variation der Eingabegröße und des Verhältnisses den Prozess mühsam machte. Ich habe es jedoch nicht vollständig automatisiert, da ich nur fünf Bilder gleichzeitig gemacht habe und ich ein autodidaktischer Anfänger im Programmieren bin. Trotzdem umfasste dies etwas mehr als hundert Codezeilen.
Der Ablauf war ungefähr so:

  1. Wenn die Breite des Eingabebilds größer als die Höhe ist, skalieren Sie das Bild um die ursprüngliche Höhe x (Originalbreite / Zielbreite) auf die Zielbreite. Alternativ Sollhöhe durch Originalbreite x (Originalhöhe / Sollhöhe).
  2. Ändern Sie die Größe der Leinwand auf die Zielbreite mal die Zielhöhe mit zentrierten Versätzen.
  3. Datei glätten und speichern.

Code-Auflistung

;; -*-scheme-*-
;; dmkonlinux 2014
;; resizes files larger than 1024 by 1024 to 1024 pixels on the longest side then applies a black canvas to create a 1024 by 1024 pixel square image for "1024by1024 x.jpg"
;; add ability to resize 1080 by 1435, 75% quality sRGB optomised, progressive, baseline, strip exif for "1080by1435 x.jpg"
;; tested on Ubuntu 17.10 and Gimp 2.8.22

(define (script-fu-dmkonlinux-web-image2    image                       ;define function and parameters in order of SF statements at end ie SF-IMAGE=image SF-DRAWABLE=drawable
                drawable
                option_size
                adjustment_number)                          
(let*   (                                                           ;define variables
    (width (car (gimp-image-width image)))                      ;use start of gimps image width variable
    (height (car (gimp-image-height image)))                    ;use start of gimps image height variable
    (targetwidth)                                                   ;a variable for the target width dependant on option_size
    (targetheight)                                              ;a variable for the target height dependant on option_size
    )
    (gimp-image-undo-group-start image)                     ;start an undo group for the image
    (gimp-context-set-interpolation 2)                          ;sets the interpolation method to (2) cubic
    (gimp-context-set-default-colors)                           ;sets the foreground / backgroud colours to default (black / white)
    (gimp-context-swap-colors)                                  ;swops the foreground / background colours

    (cond   ((= option_size 0) (set! targetwidth 1024)          ;if option_size is 0 (1024x1024) set targetwidth to 1024
                   (set! targetheight 1024))                        ;if option_size is 0 (1024x1024) set targetheight to 1024
        ((= option_size 1) (set! targetwidth 1435)              ;if option_size is 1 (1080x1435) set targetwidth to 1435
                   (set! targetheight 1080))                        ;if option_size is 1 (1080x1435) set targetheight to 1435
    )

    (if (> width height)
        (gimp-image-scale image targetwidth (/ height (/ width targetwidth)))       ;then scale image to width targetwidth by new height (divide height by ratio of width divided by targetwidth)
        (gimp-image-scale image (/ width (/ height targetheight)) targetheight)     ;else scale to...
    )
    (set! width (car (gimp-image-width image)))                 ;reset width and height to new dimensions
    (set! height (car (gimp-image-height image)))
    (gimp-image-resize image targetwidth targetheight (/ (- targetwidth width) 2) (/ (- targetheight height) 2))    ;resize canvas to targetwidth by targetheight with offsets centered
    (gimp-image-flatten image)                          ;flatten image alpha to background colour
    (gimp-image-undo-group-end image)                       ;end an undo group
)

(let*   (                                       ;define some local variables
    (drawable (car (gimp-image-get-active-drawable image)))             ;drawable has changed so this finds the drawable layer that is needed for file-web-export
    (dir_name)                                  ;define variable dir_name for save proceedure
    (filename)                                  ;define variable filename for save proceedure
    (filenumber adjustment_number)                          ;define variable filenumber for save proceedure abd set to adjustment_number
    (comment "")                                    ;define variable comment for save proceedure
    )

    (cond   ((= option_size 0) (set! filename "1024by1024 ")                                ;if option_size is 0 (1024x1024) then filename = 1024by1024
                   (set! dir_name "Desktop/")                                   ;and dir_name is users desktop
                   (set! comment "resized to 1024x1024"))                           ;and comment is resized to 1024x1024
        ((= option_size 1) (set! filename "1080by1435 ")                                ;if option_size is 1 (1080x1435) then filename = 1080by1435
                   (set! dir_name "Desktop/")                                   ;and dir_name is users desktop
                   (set! comment "resized to 1080x1435"))                           ;and comment is resized to 1080x1435
    )

    (set! filename (string-append dir_name filename (number->string filenumber) ".jpg"))    ;set filename to dir_name + filename + filenumber (number converted to string) + ".jpg"

    (gimp-image-detach-parasite image "gimp-metadata")                  ;lifted from gimp-save-for-web on github these are intended to remove the exif info and file save settings before saving
    (gimp-image-detach-parasite image "exif-data")
    (gimp-image-detach-parasite image "jpeg-settings")


    (gimp-image-set-filename image filename)                        ;set gimps filename to reflect the file as saved
    (file-jpeg-save 1 image drawable filename filename 0.75 0 1 1 comment 0 1 0 0)      ;this proceedure is save as jpeg.  params only used if used NON-INTERACRIVE (0)
)                                               ;   1 : run NON-INTERACTIVE  
                                                ;   image : variable containing image id
                                                ;   drawable : variable containing drawable id
                                                ;   filename : variable containing image filename
                                                ;   raw_filename : variable containing image filename
                                                ;   0.75 : value for quality setting
                                                ;   0 : value for smoothing (off)
                                                ;   1 : value for optimisation (on)
                                                ;   1 : value for progressive (on)
                                                ;   comment : variable containing comment
                                                ;   0 : value for subsampling (chroma quatered)
                                                ;   1 : value for baseline jpeg (on)
                                                ;   0 : value for restart markers (off)
                                                ;   0 : value for dct method (integer)

(gimp-displays-flush)                           ;flush all pending updates of image manipulations to the user interface
(gimp-image-clean-all image)                ;resets dirty count to 0 and allows closing without save dialogue.
)

(script-fu-register "script-fu-dmkonlinux-web-image2"
_"dmkonlinux web image resize2"
_"scale image to 1024 by 1024 or 1080 by 1435 and add background colour canvas"
"dmkonlinux"
"dmkonlinux, 2014"
"Sept 2014"
"*"
SF-IMAGE    "Input Image" 0                                                 ;the current image id
SF-DRAWABLE "Input Drawable" 0                                      ;the current drawable id
SF-OPTION   "Size"          '("1024 by 1024" "1080 by 1435")                ;display option box widget with list option 0, option 1
SF-ADJUSTMENT   "File number"       '(1 1 20 1 10 0 SF-SPINNER)         ;display adjustment box widget with lstart value, lower / upper values, step_inc, page_inc, digits after decimal point, type slider or spinner
)

(script-fu-menu-register "script-fu-dmkonlinux-web-image2"
         "<Image>/dmkonlinux's")

;(gimp-image-get-filename (gimp-item-get-image item)
;(gimp-image-list) returns number of open images and array of image id's ie (2#(2 1))
;(cadr(gimp-image-list) returns the the first item of the tail of gimp-image-list ie #(2 1)
;(aref(cadr(gimp-image-list)) 0) returns the value at array ref 0 ie 2
;(gimp-get-parasite-list) returns global parasite list ie (1 ("jpeg-save-defaults"))
;(gimp-image-get-parasite-list 1) returns number of parasites and string array of currently attached parasites for image id ie (5 ("jpeg-save-options" "jpeg-settings" "exif-data" "gimp-comment" "gimp-metadata"))
;(gimp-file-save RUN-INTERACTIVE image drawable filename raw_filename) this proceedure calls the save by filetype proceedure

; saved to home/gimp-2.8/scripts
; changed location to dmkonlinux's
; 17/01/2015 added set default colours and swap colours and launch web export window
; 27/08/2017 changed to file-jpeg-save from downloaded c plugin gimp-save-for-web
; 27/08/2017 added 2nd file size for alternative image
; 22/10/2018 little tidy up for photography se answer

Dieses Skript ist sehr grob und fertig, es wird zu meinem eigenen Vorteil stark kommentiert, da es möglicherweise erst das zweite Schema-Skript ist, das ich geschrieben habe (oder jemals schreiben werde). Es sollte jedoch jedem ermöglichen, es relativ einfach anzupassen. Es wurde unter Linux Ubuntu geschrieben, daher muss die Referenz für das Dateispeicherverzeichnis von anderen Benutzern beachtet werden, und offensichtlich muss die Auflösung der Ausgabedatei an Ihre Bedürfnisse angepasst werden.

Danke dmkonlinux. Das klingt nach der Art von Sache, nach der ich suche - wären Sie bereit, Ihren Code zu teilen?
@NatAes Ich habe den Code hinzugefügt (auf die Gefahr hin, die Off-Topic-Polizei zu verärgern), hoffe, er hilft.

Es gibt mehrere verschiedene Möglichkeiten, dies zu tun. Am einfachsten wäre es, die Bilder so zuzuschneiden, dass sie quadratisch werden (in Photoshop lässt sich dies leicht bewerkstelligen, indem dem Zuschneidewerkzeug ein bestimmtes Seitenverhältnis gegeben wird). Aber das ist die offensichtliche Methode und ich glaube nicht, dass Sie etwas von Ihren Bildern verlieren wollen.

Die von Ihnen beschriebene Art und Weise kann in Photoshop erreicht werden, indem Sie ein neues Bild in der richtigen Größe, mit dem richtigen Seitenverhältnis und der richtigen Hintergrundfarbe (z. B. Weiß) erstellen und dann einfach das Foto hinzufügen, das Sie in diesem Bild verwenden möchten, und die Größe ändern passen in diesen neuen Rahmen. Um sicherzustellen, dass das Original nicht verzerrt ist, können Sie das Seitenverhältnis mit dem Schloss-Symbol in der Symbolleiste sperren.

Wenn Sie dies für viele Bilder tun müssen und weil Sie immer die gleichen Schritte ausführen, können Sie dies auch mit Photoshop mit Aktionen automatisieren.