Using ImageMagick for image scaling, merging, and cropping (js + python) _ javascript skills

Source: Internet
Author: User
Tags imagemagick
Because it needs to be processed on the server side, we will use imagemagick for research. A Node is encapsulated. the js and Python methods mainly focus on the use of imagemagick to scale, synthesize, and crop the images. The cover of the book needs to be processed in the latest project, it is to add a shadow line to form a sense of the spine, and then cut the spine into two parts, so that the client can enable the animation. Because it needs to be processed on the server side, we will use imagemagick for research. At the same time, I want to encapsulate a Node. js and Python method. I 'd like to talk about it and then use imagemagick to scale, synthesize, and crop the image.

First, the material file is as follows (the cover is not processed on the left and the shadow to be merged on the right ):

Install ImageMagick process will not talk about, you can refer to the official site of the Installation Method: http://www.imagemagick.org/script/install-source.php

First, scale the envelope image file.png to a height of, and generate newfile.png to facilitate the synthesis of shadow images. The command is as follows:

The Code is as follows:

Convert-resize x1024 file.png newfile.png



The convert scaling method is as follows:

Convert-resize 1024 file.jpg newfile.jpg
The image width is 1024. The height is calculated based on the proportion of the original image.

Convert-resize x768 file.jpg newfile.jpg
The obtained image is 768 high and the width is calculated based on the proportion of the original image.

Convert-resize 1024 × 768! File.jpg newfile.jpg
Fixed width and height scaling. Scale the image to a specified size without considering the original ratio of the image width.

Convert-resize "1024 × 768>" file.jpg newfile.jpg
Dimensions have the same size.

Convert-resize "1024 × 768 <" file.jpg newfile.jpg
Dimensions have the same size.

Download to synthesize the shadow file to the envelope (convert yy.pngfrom the upper left corner to file.pngto generate newfile.png ):

The Code is as follows:

Composite-gravity northwest yy.png file.png newfile.png



Here we will mainly explain the-gravity parameter:

-Gravity northwest indicates the upper right corner.
If the request is in the center, the parameter is
If it is required to be in the lower right corner, the parameter is southeast
Other operations are performed by orientation.

After merging, the effect is as follows:

The final cut of an image is to divide the image into two parts, and the shadow left.png and other images right.png:

The Code is as follows:


Left: convert file.png-gravity southwest-crop 31x1024 + 0 + 0 left.png
Right: convert file.png-gravity southeast-crop 737x1024 + 0 + 0 right.png



The cropping method is adjusted as follows:

Convert file.png-crop widthxheight + x + y newfile
Widthxheight is the size of the target image, and + x + y is the coordinate point of the original image. The two groups must have at least one group, or both. In addition, gravity can be used to redefine the coordinate system.

The final result is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.