You are here

Cutting Circles Out Of Square Images

Hi there,
I'm curious if anyone has ever seen a script for this... I'm not very good at the scripting stuff, so hopefully it's already been done.

I have a lot of square/rectangle images. I need the images to be circle, and cut the remaining parts out and replace with an alpha channel. What I do manually, is create a perfect circle starting in the center and make it's height/width = the smallest dimension of the rectangle. I cut that circle, and paste into a new image with dimensions the next largest factor of 2 (the software for displaying them handles image with factors of 2 more gracefully) and an alpha channel.

I figure, since I could write the pseudo code for it, I should be able to script it, but I'm really not familiar with the gimp script language.

Pseudocode as follows:

if (original image height) >= (original width)
circleDimension = (original width);
if (original width) > (original height)
circleDimension = (original height);

if circleDimension < 512
newImageHeight = 512;
newImageWidth = 512;

elif circleDimension < 1024
newImageHeight = 1024;
newImageWidth = 1024;

elif circleDimension < 2048
newImageHeight = 2048;
newImageWidth = 2048;

elif circleDimension < 4096 && newImageHeight == 0
newImageHeight = 4096;
newImageWidth = 4096;
print("error, image too large")

circleSelect = select_circle //centered in middle of picture, with a heigh & width == circleDimension

cut circleSelect;

paste_to_newImage; //in the exact center

//Also somewhere in here the new image has an alpha channel... =)

; GIMP - The GNU Image Manipulation Program
; Copyright (C) 1995 Spencer Kimball and Peter Mattis
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; GNU General Public License for more details.

(define (script-fu-lordvaderize image layer)
  (let* (
      (orig-height (car (gimp-image-height image)))
      (orig-width (car (gimp-image-width image)))
      (circle-dimension (min orig-height orig-width))
      (new-image 0)
      (new-height (expt 2 (ceiling (/ (log circle-dimension) (log 2)))))
      (new-width new-height)
      (buffer-name "buffer")
      (orig-selection 0)
    (gimp-image-undo-freeze image)
    (set! orig-selection (car (gimp-selection-save image)))
    (if (> new-height 4096)
      (gimp-message "Error, image too large!")
        (gimp-ellipse-select image 
                             (/ (- orig-width circle-dimension) 2) (/ (- orig-height circle-dimension) 2) 
                             circle-dimension circle-dimension 
                             CHANNEL-OP-REPLACE TRUE FALSE 0)
        (set! buffer-name (car (gimp-edit-named-copy-visible image buffer-name)))
        (set! new-image (car (gimp-edit-named-paste-as-new buffer-name)))
        (gimp-image-undo-disable new-image)
        (gimp-image-resize new-image 
                           (/ (- new-width circle-dimension) 2) 
                           (/ (- new-height circle-dimension) 2))
        (gimp-display-new new-image)
        (gimp-layer-resize-to-image-size (car (gimp-image-get-active-layer new-image)))
        (gimp-image-undo-enable new-image)
        (gimp-buffer-delete buffer-name)
    (gimp-selection-load orig-selection)
    (gimp-image-remove-channel image orig-selection)
    (gimp-image-undo-thaw image)
(script-fu-register "script-fu-lordvaderize"
        "Copy circle per Lord Vader"
        "Copy a circular version of image to new image"
        "Saul Goode"
        "Saul Goode"
        SF-IMAGE    "Image"    0
        SF-DRAWABLE "Drawable" 0
(script-fu-menu-register "script-fu-lordvaderize"
Subscribe to Comments for "Cutting Circles Out Of Square Images"