Shrinking spells and other ImageMagick

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€47.39
List Price:  €66.35
You save:  €18.96
£39.48
List Price:  £55.27
You save:  £15.79
CA$69.93
List Price:  CA$97.91
You save:  CA$27.98
A$76.86
List Price:  A$107.61
You save:  A$30.75
S$67.15
List Price:  S$94.02
You save:  S$26.86
HK$389.09
List Price:  HK$544.76
You save:  HK$155.66
CHF 44.28
List Price:  CHF 61.99
You save:  CHF 17.71
NOK kr552.48
List Price:  NOK kr773.52
You save:  NOK kr221.03
DKK kr353.46
List Price:  DKK kr494.87
You save:  DKK kr141.41
NZ$85.02
List Price:  NZ$119.04
You save:  NZ$34.01
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳5,952.52
List Price:  ৳8,334.01
You save:  ৳2,381.48
₹4,217.70
List Price:  ₹5,905.12
You save:  ₹1,687.41
RM223.53
List Price:  RM312.96
You save:  RM89.43
₦83,723.75
List Price:  ₦117,219.95
You save:  ₦33,496.20
₨13,842.48
List Price:  ₨19,380.58
You save:  ₨5,538.10
฿1,735
List Price:  ฿2,429.14
You save:  ฿694.14
₺1,722.82
List Price:  ₺2,412.08
You save:  ₺689.26
B$289.31
List Price:  B$405.06
You save:  B$115.74
R905.17
List Price:  R1,267.31
You save:  R362.14
Лв92.66
List Price:  Лв129.73
You save:  Лв37.07
₩69,838.72
List Price:  ₩97,779.80
You save:  ₩27,941.07
₪186.81
List Price:  ₪261.55
You save:  ₪74.74
₱2,950.38
List Price:  ₱4,130.77
You save:  ₱1,180.39
¥7,786.65
List Price:  ¥10,901.93
You save:  ¥3,115.28
MX$1,010.13
List Price:  MX$1,414.26
You save:  MX$404.13
QR181.66
List Price:  QR254.33
You save:  QR72.67
P677.69
List Price:  P948.83
You save:  P271.13
KSh6,473.70
List Price:  KSh9,063.70
You save:  KSh2,590
E£2,484.50
List Price:  E£3,478.50
You save:  E£994
ብር6,130.69
List Price:  ብር8,583.46
You save:  ብር2,452.76
Kz45,623.90
List Price:  Kz63,877.12
You save:  Kz18,253.21
CLP$48,554.78
List Price:  CLP$67,980.58
You save:  CLP$19,425.80
CN¥362.25
List Price:  CN¥507.18
You save:  CN¥144.93
RD$2,999.87
List Price:  RD$4,200.06
You save:  RD$1,200.18
DA6,670.19
List Price:  DA9,338.81
You save:  DA2,668.61
FJ$113.51
List Price:  FJ$158.92
You save:  FJ$45.41
Q384.54
List Price:  Q538.38
You save:  Q153.84
GY$10,416.21
List Price:  GY$14,583.53
You save:  GY$4,167.31
ISK kr6,885.62
List Price:  ISK kr9,640.42
You save:  ISK kr2,754.80
DH497.67
List Price:  DH696.78
You save:  DH199.10
L909.82
List Price:  L1,273.82
You save:  L364
ден2,912.40
List Price:  ден4,077.60
You save:  ден1,165.19
MOP$399.33
List Price:  MOP$559.09
You save:  MOP$159.76
N$900.58
List Price:  N$1,260.89
You save:  N$360.30
C$1,833.12
List Price:  C$2,566.52
You save:  C$733.39
रु6,730.44
List Price:  रु9,423.16
You save:  रु2,692.71
S/189.03
List Price:  S/264.65
You save:  S/75.62
K200.43
List Price:  K280.62
You save:  K80.18
SAR187.67
List Price:  SAR262.75
You save:  SAR75.08
ZK1,376.04
List Price:  ZK1,926.57
You save:  ZK550.52
L235.82
List Price:  L330.17
You save:  L94.34
Kč1,197.77
List Price:  Kč1,676.98
You save:  Kč479.20
Ft19,418.42
List Price:  Ft27,187.34
You save:  Ft7,768.92
SEK kr549.81
List Price:  SEK kr769.78
You save:  SEK kr219.97
ARS$50,077.77
List Price:  ARS$70,112.88
You save:  ARS$20,035.11
Bs345.44
List Price:  Bs483.65
You save:  Bs138.20
COP$220,323.37
List Price:  COP$308,470.35
You save:  COP$88,146.97
₡25,320.40
List Price:  ₡35,450.59
You save:  ₡10,130.18
L1,258.58
List Price:  L1,762.11
You save:  L503.53
₲387,944.27
List Price:  ₲543,153.02
You save:  ₲155,208.75
$U2,137.07
List Price:  $U2,992.07
You save:  $U855
zł205.58
List Price:  zł287.83
You save:  zł82.25
Already have an account? Log In

Transcript

In this chapter, we will learn how to process images from the command line. We will start with the most complex and widely used image command line interface processing toolkit called image magic. In order to install it run sudo apt install image magic. As you can see, I already have it installed. Now let's find some images to process let's use the default Ubuntu backgrounds that can be found in user share background. Let's copy the background so another authentication so that we don't alter our default ones.

Let's take a look at the first image in our list. We can see from ls that it is a JPEG image of 1.6 megabytes to open it and see how it looks. Let's use the elk I have known Image Viewer. The first and most important part in knowing how to process an image is knowing What that image actually is. To find this out image magic comes with a tool called identify. In its simplest form, you have to feed it an image name, and it will output information like the following.

We can see that the file is a JPEG image of 1.6 megabytes and most importantly, its size is this B. Yes, it's so huge. If we look at the quality final Ubuntu, we see that the output format is pretty similar. We see that the size and resolution are higher, and that the image format is PNG. Let's see how it looks. PNG images usually take more space than JPEG ones.

If you don't have transparency, it is recommended to use JPEG. In order to convert from one type to the other. We use the image magic convert command with two parameter Input filename and output file name. The format of the output image will be deduced by convert from the filename extension. As you can see, the output is a JPEG image with a similar solution, but with a much, much smaller size than the PNG version 180 kilobytes instead of 2.6 megabytes. And if we open the image, we can see any noticeable differences.

This is a big thing when it comes to web development, because if we were to use this picture on a web page, it would load even 15 times faster than the PNG version. If you want to crop a region of the image, we can do that with convert. For example, if you want to cut a 500 by 500 piece of the image starting at coordinates 100 to 100, you would use the command on the screen As you can see, the output image is added the resolution requested, but it has a much lower size of only 2.5 kilobytes. Usually analyzing the two images, we can see that a cropped one is a region of the big picture. Normally, you wouldn't want to guess pixels in the command line, but would use an image processing software like GIMP or GIMP actually, to do the work for you so that you can visually select and cropped portions of the images.

But when developing software applications, it is often the case that you have to programmatically crop images, in which case this comes in handy. Convert is also good at creating images. If you want to create an image from a text string, we could use the following command. This will create a JPEG image of size pixels containing the text specified. In this case, the string 123. We can see the outward it is a 3.4 kilobytes image.

And if you look at it visually, we see that text 123. This can also come in handy in different scenarios where you need to programmatically generate readable images like doing capture software or generating default profile images with the users initials. Now let's look at some image shrinking tools outside of image match. The first one is a PNG shrinking tool called PNG Quint, we will install it by typing sudo apt install PNG client. Now let's try to shrink the large PNG image that we were looking at earlier if supposedly the image contains transparency and it needed to stay in the PNG format, we could just call PNG point with the image name. By default, it outputs a file with the same name and an added extension of a site.

We can see the difference in size is also noticeable. It's smaller by one megabyte, which is almost half the original size. And if we visually compare the images again, we won't be able to spot any differences. All right, now let's try and do the same thing for JPEG images. For this, we install the equivalent of PNG croissant with cheese jpg optim. So Doc, the stylish Bugatti.

We will call it the same way and we're just giving you a command line argument, which is the file to shrink. Let's pick some random images to try and see if we can reduce their size. As you can see from the output, it is saying skipped. That means the image was already shrunk and probably the guys at Ubuntu are using the same tool before submitting the image. Also, if we try it on the JPG produced by image magic, you can see that it also it is also skipped. Image magic already uses the minimum necessary for it.

The image processing tools come in handy especially when it comes to web development, where lots of images need to be used and their size needs to be as small as possible. command line tools are especially useful because they can be used to automate tasks. Image shrinking is usually added to build tasks where Production versions of websites are prepared. The image magic toolkit comes with a lot more tools than the ones we have seen today. So feel free to explore other handy commands from the toolkit. Also, when it comes to graphically processing the images, there are some great open source tools like GIMP and Inkscape that can really help you get your job done and also save a lot of money.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.