Skip to content

Instantly share code, notes, and snippets.

@dahlia
Last active December 17, 2015 10:09
Show Gist options
  • Save dahlia/5592282 to your computer and use it in GitHub Desktop.
Save dahlia/5592282 to your computer and use it in GitHub Desktop.
Response to Nicolas de Bari Embriz Garcia Rojas <[email protected]>
from wand.color import Color
from wand.image import Image
with Image(background=Color('black'), width=1280, height=720) as result:
with Image(filename='source.jpg') as image:
result.composite(
image,
(result.width - image.width) / 2,
(result.height- image.height) / 2
)
result.save(filename='result.jpg')
MIME-Version: 1.0
Date: Thu, 16 May 2013 14:48:35 +0100
From: Nicolas de Bari Embriz Garcia Rojas <[email protected]>
List-Archive: <http://librelist.com/archives/wand/>
List-Id: [email protected]
List-Post: <mailto:[email protected]>
Reply-To: [email protected]
Sender: [email protected]
Subject: [wand] how to crop defining -gravity and -extent etc
To: [email protected]
Hi, I need to crop an image multiple times changing the offset.
currently I use the following command line:
convert image.jpg -crop 1280x720+0+0 -gravity center -background black
-sharpen 0x.5 -quality 75 -extent 1280x720 ppm:-
convert image.jpg -crop 1280x720+1+2 -gravity center -background black
-sharpen 0x.5 -quality 75 -extent 1280x720 ppm:-
convert image.jpg -crop 1280x720+2+4 -gravity center -background black
-sharpen 0x.5 -quality 75 -extent 1280x720 ppm:-
...
...
...
Any idea of how to do it using wand ?
currently I am using python subprocess but would like to use wand since
I notice is faster.
regards.
@nbari
Copy link

nbari commented May 16, 2013

# ken burns effect
from wand.image import Image
from wand.color import Color
from subprocess import Popen, PIPE, STDOUT
import time

start_time = time.time()

gw = 1280
gh = 720

X = 0
Y = 0

cmd = [
    'ffmpeg1', '-loglevel', 'info', '-y', '-f', 'image2pipe', '-vcodec', 'ppm', '-s', '1280x720', '-r', '25', '-threads',
    '0', '-i', '-', '-vcodec', 'libx264', '-b:v', '2048k', '-maxrate', '2048k', '-bufsize', '4096k', '-an', '-sn', 'out.mp4']
p = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=STDOUT)

with Image(filename=img_file) as image:
    with Image(background=Color('black'), width=gw, height=gh) as bg_image:
        for i in range(0, 80):
            with bg_image.clone() as result:
                with image.clone() as img:
                    img.crop(X, Y, width=gw, height=gh)
                    result.composite(
                        img,
                        (result.width - img.width) / 2,
                        (result.height - img.height) / 2)
                    if i:
                        p.stdin.write(result.make_blob('ppm'))
                    print i, result.size
            X += 1
            Y += 2

out, err = p.communicate()
print out, err
print p.returncode
print 'finished in: %f seconds' % (time.time() - start_time)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment