Use gdal to produce a JPG Quick View (thumb chart)

Source: Internet
Author: User

There are two main steps to generate a quick view using gdal: the first step is to produce a small image, and the second step is to generate a small image createcopy to generate a jpg or PNG format. Since gdal does not support directly creating jpg or PNG compressed images, a transfer process is required in the middle.

The following is a brief description of the process and the code will not be pasted. First, calculate the size of the output image by obtaining the sampling ratio. You may need to define the size as needed. I have encountered two common situations: first, scale down the image to one of the original 10 points (or another ratio). For example, the source image is 20000 × 20000, the quick view is 2000X2000. Second, the output image range is within a specified range. For example, the output view size cannot exceed 1024 × 1024, here, we need to calculate the sampling ratio by the width and height of the original image and the 1024 ratio. These two cases are all handled in one case, that is, the re-sampling ratio is calculated, and then the re-sampling ratio is used for sampling.

For more information about how to use gdal re-sample, see the previous blog "How to Use gdal re-sample images" at http://blog.csdn.net/liminlu0314/article/details/6130064.

This is a temporary file that can be directly created using gdal, such as tiff or IMG. Next, use gdalopen to open the image, use the createcopy function to create a jpg image (PNG is similar to this), and then delete the temporary TIFF file. At this point, this function is completed, but it is always a bit uncomfortable. What is it? It's this temporary file. It's really uncomfortable to come up with a temporary file with such a small function. Here we will tell you how to replace this temporary file.

If someone has read my blog, they should remember that there was a blog post about gdal memory file, which is in MEM format. This blog is called "Saving temporary files with memory files of gdal". The address is http://blog.csdn.net/liminlu0314/article/details/7166230. Well, it's him. We can use mem to replace this temporary file.

The usage of MEM memory files is the same as that of common images, except that when the driver is created, Mem is selected. When the image is created, the file path does not need to be specified and an empty string is used directly.


Through the above analysis, we will give a general summary here, modify the gdal re-sampling code, and create the output image in it as a mem gdaldataset, in this way, the result of the re-sampling is stored in the gdaldataset of the mem. Then, create a JPG driver and use the createcopy function of the jpg driver to generate the jpg image.

PS: the size of the mem file depends on the memory of your computer. Therefore, when creating the mem file, make sure that the memory of your computer can store the file. If it is too large, it will fail, given that the fast view here is generally relatively small, it is quite appropriate to use mem here.

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.