Cropping multiple images the same way
The tools we’ll be using are =GIMP= and =mogrify= (from the ImageMagick suite),
so make sure that you have them installed.
We’ll use GIMP to graphically select the area to be cropped
and the mogrify tool to automate the cropping, saving us a lot of work. Let’s start with the selecting:
Getting the right cropping values using GIMP
In 5 steps:
Where to find the cropping values
1. Open up GIMP.
2. Open one of the images in GIMP.
3. Using the Rectangle Select Tool (hotkey “R”), select the area you want to be cropped.
4. Note the X, Y, Width and Height values GIMP gives you
(have a look at the picture, you can find them in the GIMP main window).
5. Close GIMP (or leave it open if you plan to use it again soon).
We now have the values we need to tell the mogrify utility what to crop.
Let’s go on and write a line that’ll execute mogrify in such a way that it’ll crop all our images!
Cropping the images
Now we’ll start working in the terminal.
Open up your favourite one and change directory to the directory
where the images are located.
Note that I strongly recommend having only the images that are to be cropped in the directory, nothing more.
It saves you a lot of trouble. Well then, let’s start with the mogrify command.
The syntax for cropping is as follows:
>> mogrify -crop {Width}x{Height}+{X}+{Y} image.png
Now don’t be scared, the {Width}, {Height} and so on simply are the places where you should put the values you got from GIMP! Note that I use a png file as an example, while mogrify is able to handle over 100 image file types. You don’t have to use png files with it. Anyway, if I’d fill in the values from the screenshot, the mogrify command would look like this:
>> mogrify -crop 643×393+7+83 image.png
The logic behind this system is the following:
crop an area of 643 by 393 pixels, starting at 7 pixels from the left and 83 pixels from the top of the image. Got it?
Ok then.
The above command would overwrite image.png with a cropped version.
But this still manipulates just a single image.
The easiest way to make mogrify modify all images is just this:
>> mogrify -crop 643×393+7+83 *.png
The asterisk makes bash fill in all png files in the current directory, and mogrify will handle them all happily. After a (hopefully short) wait, all the images will have been cropped. If you want to crop images of an other format, just change “*.png” to, for example, “*.jpg” or “*.gif”.
You might want to give the cropped images other names, so that the original images will not be overwritten and it will be clear which images have been cropped and which haven’t. This is more complex, but hey, we’re working on Linux! Everything is possible if you take the time to write it.
Renaming the cropped images
In order to give the cropped images other names, we’ll use a bash loop. This time, we’ll use the convert utility. It is from the same family as mogrify, but it makes it easier for us to output to an other filename. I won’t explain the whole loop, as most of it is bash knowledge, but I will tell you which things you can/should alter to get the right results. There are loops for two cases, just pick the one of which you like the file naming the most.
Case 1: You want the output files to be named like this: originalfile.png => cropped_originalfile.png (again, you can insert any of the over 100 supported image formats here, I just like png). The loop should be like this:
$ for file in *.png; do convert -crop {Width}x{Height}+{X}+{Y} $file cropped_$file; done
You should replace “png” with the extension you want (think of jpg, gif, png (of course) and so on), and the “{Width}”, “{Height}” etc with the values you got from GIMP. You may also replace “cropped_” with any prefix you like.
Case 2: You want the output files to be named like this: originalfile.png => originalfile_cropped.png (or originalfile.jpg => originalfile_cropped.jpg, you name it). In that case, you should use the following loop:
$ for file in *.png; do convert -crop {Width}x{Height}+{X}+{Y} $file ${file%.png}_cropped.png; done
Again, replace “png” with the extension you want (watch it, there are 3 instances of it) and the “{Width}”, “{Height}” etc with the values you got from GIMP. You can also replace “_cropped” with any suffix you like.
Note that you can, in this case, easily modify the output format: if you want to output the cropped images in jpg format, you can just replace the third instance of “png” with “jpg”, no matter what format your input files are. The convert utility will detect it and change the image format automatically.
Cropping multiple images the same way的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- scala - multiple overloaded alternatives of method bar define default arguments
同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
随机推荐
- A joke about regular expression
As the old computer science joke goes: “Let’s say you have a problem, andyou decide to solve it with ...
- 关于浮动与清浮动 float
浮动常见的几种属性值 float {left; right; none; } 主要是定义元素朝哪个方向浮动: 元素浮动后的特性 在一行显示,父级的宽度放不下,自己折行: 支持宽高等样式: 不设置 ...
- UART、I2C、SPI三种协议对比
学嵌入式需要打好基础 下面我们来学习下计算机原理里的3种常见总线协议及原理 协议:对等实体之间交换数据或通信所必须遵守规则或标准的集合 1.UART(Universal Asynchronous Re ...
- python删除列表中元素的方法
删除列表中元素的三种方法-remove.pop.del 1 1.remove: 删除单个元素,删除首个符合条件的元素,按值删除 2 举例说明: 3 >>> str=[1,2,3,4, ...
- [POI2009]救火站Gas
Description 给你一棵树,现在要建立一些消防站,有以下要求: 1. 消防站要建立在节点上,每个节点可能建立不只一个消防站. 2. 每个节点应该被一个消防站管理,这个消防站不一定建立在该节点上 ...
- Bryce1010的linux课程设计
1.设计目的 2.软件环境 3.要求 4.需求分析 5.总体设计 6.详细设计 7.调试与测试 8.总结 思路整理: 1.如果要开始编译着手的准备 SQLite数据库的安装 gtk+的安装 (.... ...
- 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...
- Looper、MessageQueue、Message、Handler的关系
1.快速复习 1.1 基本装置 类 装置名 作用 线程中数量 Looper 消息分发装置 从消息队列中取出一个消息,交给对应的Handler处理消息. 1 MessageQueue 消息队列 保存所有 ...
- AJPFX编写cmd界面下一键编译、执行java代码的bat脚本
此脚本适合刚接触java的同学,在cmd界面下用jc取代 复杂的 javac *.java + java main使用说明:把脚本内容复制到txt文本中,修改后缀名为.bat,运行一次即可完成配置, ...
- testlink 从1.8.5 升级到 1.9.8
step1:备份原 1.8.5 的数据库. step2:分别下载 1.9.0 / 1.9.3 / 1.9.8 的安装包. step3:分别解压 1.9.0 / 1.9.3 / 1.9.8 成3 ...