crop an image or fit it to to size you want. It's based on imag center.
1 def fit_crop(file_path, max_width=None, max_height=None, save_as=None):
2 # Open file
3 img = Image.open(file_path)
4
5 # Store original image width and height
6 w, h = float(img.size[0]), float(img.size[1])
7
8 # Use the original size if no size given
9 max_width = float(max_width or w)
10 max_height = float(max_height or h)
11
12 # Find the closest bigger proportion to the maximum size
13 scale = max(max_width / w, max_height / h)
14
15 # Image bigger than maximum size?
16 if (scale < 1):
17 # Calculate proportions and resize
18 w = int(w * scale)
19 h = int(h * scale)
20 img = img.resize((w, h), Image.ANTIALIAS)
21 #
22
23 # Avoid enlarging the image
24 max_width = min(max_width, w)
25 max_height = min(max_height, h)
26
27 # Define the cropping box
28 left = int((w - max_width) / 2)
29 top = int((h - max_height) / 2)
30 right = int(left + max_width)
31 bottom = int(top + max_height)
32
33 # Crop to fit the desired size
34 img = img.crop( (left, top, right, bottom) )
35
36 # Save in (optional) 'save_as' or in the original path
37 img.save(save_as or file_path)
38
39 return True
Juste un script qui se charge de faire un screenshot, le place dans un répertoire donné, et affiche une miniature de ce qui à été capturé. La capture se fait en utilisant scrot (par défaut) ou import (si scrot n'est pas présent) Testé on Linux and Solaris
1 #!/usr/bin/env bash
2 #
3 #printscreen.sh written by : nali <phobos@goupilfr.org>
4 # enhanced by : slubman <slubman@ifrance.com>
5 # this script is free software according to the
6 # GNU General Public License (see http://www.gnu.org/licenses/gpl.html)
7 #
8 #
9 # This script make a screenshot and save it to a default location
10 # You can customize some component by editing variables:
11 # * REP_SAVE
12 # * IMG_PREF
13 # * IMG_UNIQ
14 # * IMG_VIEW
15 # * IMG_SIZE
16 #
17 # The script first try to use the scrot program, if not found it default on import
18 #
19 ###########################################################
20
21
22 # Modify the following variables to fetch what you want:
23 #repertoire des sauvegardes
24 REP_SAVE=~/Documents/Captures
25
26 # Prefixe du nom des images
27 IMG_PREF=capture
28
29 # Estampille (unique) des images
30 IMG_UNIQ=`date +%Y-%m-%d-%H%M%S`
31
32 # View Image (0=no , 1=yes : this feature depend on the display program) ?
33 IMG_VIEW=1
34
35 # Display image resize
36 IMG_SIZE="840x625"
37
38
39
40 ###########################################################
41 # You really don't need to edit anything bellow this line #
42 ###########################################################
43
44 #Delai en sec. avant la capture . Peut etre interessant a mettre par exemple a
45 #5 ou 10 secondes pour capturer des screen savers.
46
47 [ "$1" ] && DELAY=$1
48
49 ###########################################################
50
51 #repertoire des captures non datees
52 REP_TEMP=$REP_SAVE/tmp
53
54 #creation du repertoire des sauvegardes si il n existe pas
55 [ ! -d $REP_SAVE ] && mkdir $REP_SAVE
56
57 #creation du repertoire des sauvegardes avant "datage", si il n existe pas
58 [ ! -d $REP_TEMP ] && mkdir $REP_TEMP
59
60 ###########################################################
61
62 # Verification de l'exitence de import
63 CAPTURE_CMDS=(scrot import)
64 for CAPTURE_CMD in ${CAPTURE_CMDS[@]};
65 do
66 which $CAPTURE_CMD
67 if [ $? -eq 0 ]; then
68 echo "Using $CAPTURE_CMD for the capture !!"
69 break
70 fi
71 done
72
73 # Verification de l'exitence de display
74 if [ IMG_VIEW == "1" -a ! $(which display) ]; then
75 echo "display program not found, please install ImageMagick (http://www.imagemagick.org)"
76 exit
77 fi
78
79 #nom de l'image avant datage
80 NOM_TEMP=$IMG_PREF.png
81
82 #nettoyage du temp
83 cd $REP_TEMP
84 rm -f $NOM_TEMP
85
86 #capture de l'image
87 [ $DELAY ] && sleep $DELAY
88 case $CAPTURE_CMD in
89 scrot)
90 scrot -m $NOM_TEMP
91 ;;
92 import)
93 import -window root $NOM_TEMP
94 ;;
95 *)
96 echo "Unknown capture command"
97 exit
98 ;;
99 esac
100
101
102 #ajout de la date et heure au nom de la capture
103 #c'est pas beau mais ca marche :)
104 SEPARATEUR=-
105 NOM_DATE=`basename $NOM_TEMP .png`$SEPARATEUR$IMG_UNIQ.png
106
107 cp $NOM_TEMP $NOM_DATE
108
109 mv $NOM_DATE $REP_SAVE
110
111 #affichage en mode reduit
112 [ $IMG_VIEW == "1" ] && display -resize $IMG_SIZE "$REP_SAVE/$NOM_DATE" &
113
114 #nettoyage du temp , pour laisser propre en partant ...
115 cd $REP_TEMP
116 rm -f $NOM_TEMP
Resize a lot of images with shell and imagemagick
1 for i in *.jpg
2 do convert $i -scale 1024x760 resized-$i;
3 done
Pages : 1