A method, apparatus, electronic device, and storage medium for image compression.

By determining the target quality compression value and subsequent size compression, the problem of image clarity and size not meeting requirements after compression in existing technologies is solved. This allows images to be compressed to a specified size while maintaining clarity, thus improving the compression effect.

CN115842892BActive Publication Date: 2025-12-02BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211559396.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-06
Publication Date
2025-12-02
Estimated Expiration
2042-12-06

AI Technical Summary

Technical Problem

Existing technologies struggle to simultaneously guarantee that the compressed image has the required clarity and size, resulting in poor image quality.

Method used

First, determine the target quality compression value for the image to be compressed, ensuring it is not less than the quality threshold and the corresponding image size is within a preset range. Then, perform quality compression while keeping the image size unchanged. Next, while keeping the quality of the compressed image unchanged, perform size compression until the difference is within the target range, ensuring that the clarity and size meet the requirements.

Benefits of technology

It intelligently compresses images to a specified size while maintaining image clarity, improving the quality and size accuracy of the compressed image.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115842892B_ABST
    Figure CN115842892B_ABST
Patent Text Reader

Abstract

This disclosure provides an image compression method, apparatus, electronic device, and storage medium, relating to the field of image processing technology, specifically image compression and cloud computing. The specific implementation scheme is as follows: An image to be compressed is acquired; a target quality compression value for the image to be compressed is determined, wherein the target quality compression value is not less than a quality threshold, and the size of the compressed image is within a preset range of the target image size; keeping the size of the image to be compressed unchanged, the image to be compressed is performed using the target quality compression value to obtain a compressed image; keeping the image quality value of the compressed image unchanged, the compressed image is further compressed in size until the difference between the compressed image size and the target image size is within a target difference range, thus obtaining a compressed image, achieving intelligent image compression.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of image processing technology, and further to the fields of image compression, cloud computing and other technologies, and particularly to an image compression method, apparatus, electronic device and storage medium. Background Technology

[0002] With the increase in online business, users need to upload images in various application scenarios (such as website registration, account opening, etc.), and there are certain restrictions on the resolution and size of the uploaded images. At this time, it is necessary to compress the uploaded images so that the compressed images can meet the requirements for uploading. Summary of the Invention

[0003] This disclosure provides an image compression method, apparatus, electronic device, and storage medium.

[0004] According to one aspect of this disclosure, an image compression method is provided, comprising:

[0005] Get the image to be compressed;

[0006] Determine the target quality compression value for the image to be compressed, wherein the target quality compression value is not less than a quality threshold and the size of the image after quality compression is within a preset range of the target image size;

[0007] Keeping the size of the image to be compressed unchanged, the image to be compressed is compressed using the target quality compression value to obtain a quality-compressed image.

[0008] Keeping the image quality value of the quality-compressed image unchanged, the image is then resized until the difference between the size of the resized image and the target image size is within the target difference range, thus obtaining the compressed image.

[0009] According to another aspect of this disclosure, an image compression apparatus is provided, comprising:

[0010] The image acquisition module is used to acquire images to be compressed.

[0011] The compression value determination module is used to determine the target quality compression value of the image to be compressed, wherein the target quality compression value is not less than a quality threshold and the size of the image after compression is within a preset range of the target image size;

[0012] The first image compression module is used to keep the size of the image to be compressed unchanged, and to perform quality compression on the image to be compressed using the target quality compression value to obtain a quality-compressed image.

[0013] The second image compression module is used to keep the image quality value of the quality-compressed image unchanged, and to compress the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thereby obtaining the compressed image.

[0014] According to another aspect of this disclosure, an electronic device is provided, comprising:

[0015] At least one processor; and

[0016] A memory communicatively connected to the at least one processor; wherein,

[0017] The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform any of the methods described in this disclosure.

[0018] According to another aspect of this disclosure, a non-transitory computer-readable storage medium is provided storing computer instructions, wherein the computer instructions are used to cause the computer to perform any of the methods described in this disclosure.

[0019] According to another aspect of this disclosure, a computer program product is provided, comprising a computer program that, when executed by a processor, implements the method described in any one of this disclosure.

[0020] This embodiment of the disclosure implements intelligent image compression.

[0021] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of this disclosure, nor is it intended to limit the scope of this disclosure. Other features of this disclosure will become readily apparent from the following description. Attached Figure Description

[0022] The accompanying drawings are provided to better understand this solution and do not constitute a limitation of this disclosure. Wherein:

[0023] Figure 1 This is a schematic diagram of an image compression method according to the present disclosure;

[0024] Figure 2 This is another schematic diagram based on the image compression method disclosed herein;

[0025] Figure 3 This is a timing implementation diagram of the image compression method disclosed herein;

[0026] Figure 4 This is a schematic diagram illustrating the image compression method disclosed herein;

[0027] Figure 5This is a schematic diagram of an image compression device according to the present disclosure;

[0028] Figure 6 This is a block diagram of an electronic device used to implement the image compression method of the embodiments of this disclosure. Detailed Implementation

[0029] The exemplary embodiments of this disclosure are described below with reference to the accompanying drawings, including various details of the embodiments to aid understanding, and should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of this disclosure. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0030] Image compression is used in various scenarios, such as when websites restrict the resolution and size of uploaded images for registration, requiring image compression, and when uploading images to cloud storage devices, etc.

[0031] One approach involves compressing the image to a specified resolution or quality. This can be achieved by using the `UIImageJPEG Representation()` function, which requires two parameters: an image identifier (or reference) and a compression factor. The compression factor typically ranges from 0 to 1; a smaller value indicates lower quality and a smaller file size after compression. The file size refers to the amount of image data.

[0032] Another approach involves compressing the image to be compressed to a specified size. This can be achieved by using the function `UIGraphicsGetImageFromCurrentImageContext()` to compress the image, and by passing the desired output (compressed) image size to `drawInRect(draw rectangle)`.

[0033] However, while using the `UIImageJPEGRepresentation()` function to compress the image quality can control the compression factor and thus preserve the image's sharpness, it cannot guarantee that the compressed image size will meet requirements. Conversely, using the `UIGraphicsGetImageFromCurrentImageContext()` function to compress the image size can maintain the compressed image's dimensions, but this may result in lower image sharpness.

[0034] To achieve intelligent image compression, this disclosure provides an image compression method. The method involves acquiring an image to be compressed, determining a target quality compression value for the image, where the target quality compression value is not less than a quality threshold and the size of the compressed image is within a preset range of the target image size; then, while keeping the size of the image to be compressed unchanged, performing quality compression on the image using the target quality compression value to obtain a quality-compressed image; finally, while keeping the image quality value of the quality-compressed image unchanged, compressing the size of the quality-compressed image until the difference between the size of the compressed image and the target image size is within a target difference range to obtain the compressed image.

[0035] In this embodiment, a target quality compression value for the image to be compressed is first determined. This target quality compression value is not less than a quality threshold and corresponds to a value whose size after quality compression is within a preset range of the target image size. This ensures that, while maintaining the size of the image to be compressed, the target quality compression value is used to compress the image, resulting in a compressed image that maintains image clarity and whose size is within the preset range of the target image size. Furthermore, while maintaining the image quality value of the compressed image, the size of the compressed image is compressed until the difference between the compressed image size and the target image size is within a target difference range. This ensures that the compressed image maintains clarity while its size is close to the target image size, thus achieving intelligent image compression to a specified image size while maintaining image clarity.

[0036] The image compression method provided in the embodiments of this disclosure will be described in detail below.

[0037] The image compression method provided in this disclosure can be applied to any scenario requiring image compression. The image compression method provided in this disclosure can be applied to electronic devices, such as server devices, smart terminal devices, etc. In one example, it can be specifically applied to image compression applications (or tools) in electronic devices. For instance, the image compression tool can be a high-efficiency intelligent image compression tool based on OpenCV, where OpenCV is a cross-platform computer vision and machine learning software library capable of implementing many algorithms in image processing and computer vision.

[0038] See Figure 1 , Figure 1 This is a flowchart illustrating an image compression method provided in an embodiment of the present disclosure, including the following steps:

[0039] S101, Obtain the image to be compressed.

[0040] The image to be compressed refers to the image that needs to be compressed, and there can be one or more images.

[0041] S102, Determine the target quality compression value for the image to be compressed.

[0042] The target quality compression value is not less than the quality threshold, and the size of the image after quality compression is within a preset range of the target image size.

[0043] In one example, the target quality compression value represents the quality compression factor used to compress the image. The quality threshold can be a user-specified quality value or the default quality value of the electronic device. This quality threshold is used to ensure the clarity of the compressed image and can be represented as 0%-100% or a value between 0.0 and 1.0. The quality threshold value corresponds to the image clarity as blurry, standard, and high definition, respectively. The target image size represents the user-specified size of the compressed image. The preset range can be set according to the actual size of the image to be compressed or empirical values. For example, the preset range can be a range corresponding to a target image size plus or minus 500 bytes, 100 kilobytes, 1 MB, or 100 MB, etc.

[0044] In one example, when compressing an image, the image size is kept constant before quality compression is applied to ensure the image's clarity. The target quality compression value is determined while maintaining the image size. Preferably, while keeping the image size constant, an optimization method is used to calculate a quality value that is not less than a quality threshold, and the resulting compressed image size is within a preset range of the target image size. This value is then determined as the target quality compression value. The optimization method could be, for example, a binary search, gradient descent, or Newton's method.

[0045] S103, Keep the size of the image to be compressed unchanged, and use the target quality compression value to compress the image to obtain the quality compressed image.

[0046] In one example, when performing quality compression on an image to be compressed, the size compression value of the image to be compressed is set to 1 to keep the size of the image to be compressed unchanged. The target quality compression value is then used to perform quality compression on the image to be compressed, resulting in a quality-compressed image.

[0047] For example, the size parameter of the image to be compressed is set to 1 * original size, and the quality parameter is the target quality compression value * original quality value. The image to be compressed is then subjected to quality compression to obtain the quality-compressed image.

[0048] S104, keeping the image quality value of the quality-compressed image unchanged, compress the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thus obtaining the compressed image.

[0049] After quality compression of the image to be compressed, further size compression is performed on the compressed image. During size compression, the quality parameter of the compressed image is set to the target quality compression value multiplied by the original quality value to maintain the image quality value unchanged. Size compression is then performed until the difference between the size of the compressed image and the target image size is within a target difference range. This results in a compressed image where the difference between the compressed image size and the target image size is within the target difference range. Preferably, the target difference range is less than the aforementioned preset range. Specifically, it can be set according to the actual size of the image to be compressed or empirical values. For example, the target difference range could be a range corresponding to plus or minus 100 bytes, 10 kilobytes, or 1 megabytes of the target image size, etc.

[0050] In this embodiment, a target quality compression value for the image to be compressed is first determined. This target quality compression value is not less than a quality threshold and corresponds to a value whose size after quality compression is within a preset range of the target image size. This ensures that, while maintaining the size of the image to be compressed, the target quality compression value is used to compress the image, resulting in a compressed image that maintains image clarity and whose size is within the preset range of the target image size. Furthermore, while maintaining the image quality value of the compressed image, the size of the compressed image is compressed until the difference between the compressed image size and the target image size is within a target difference range. This ensures that the compressed image maintains clarity while its size is close to the target image size, thus achieving intelligent image compression to a specified image size while maintaining image clarity.

[0051] In one possible implementation, the process of determining the target quality compression value of the image to be compressed in step S102 includes: keeping the size of the image to be compressed unchanged, and calculating the quality compression value that makes the image size of the image after quality compression closest to the target image size, while ensuring that the image quality value of the image after quality compression is not less than the quality threshold, as the target quality compression value.

[0052] In this embodiment of the disclosure, when compressing the image to be compressed, the size of the image to be compressed is kept unchanged first, and quality compression is performed on the image to be compressed, ensuring that the clarity of the image after quality compression is appropriate. Furthermore, when performing quality compression on the image to be compressed, while keeping the size of the image to be compressed unchanged and ensuring that the image quality value of the image after quality compression is not less than a quality threshold, a target quality compression value is calculated that makes the image size of the image after quality compression closest to the target image size, and then the image to be compressed is compressed using the target quality compression value.

[0053] For example, with a quality threshold of 60%, and keeping the size of the image to be compressed unchanged (1 * original size), quality compression is performed on the image to be compressed if the image quality value after quality compression is not less than the original quality value * 60%. The target quality compression value is then calculated to find the image size of the image after quality compression that is closest to the target image size. This "image size closest to the target image size" can be achieved by adjusting the image quality compression value so that the difference between the image size of the image after quality compression and the target image size (with limited image size precision, such as integer, two decimal places, or three decimal places) remains constant or within a set range, provided the image quality value after quality compression is not less than the original quality value * 60%. Alternatively, it can be achieved by adjusting the image quality value to 60% of the original quality value; if the image size of the image after quality compression is still larger than the target image size, then the image size of the image after quality compression in this case is determined to be closest to the target image size. This set range can be the same as or different from the preset range mentioned above.

[0054] In this embodiment of the disclosure, the size of the image to be compressed is kept unchanged. If the image quality value of the image after quality compression is not less than the quality threshold, a target quality compression value is calculated that makes the image size of the image after quality compression closest to the target image size. Then, the image to be compressed is compressed using the target quality compression value. This ensures that the image size of the image after quality compression is closest to or reaches the target image size while maintaining image quality. Furthermore, it facilitates the rapid reduction of the difference between the size of the image after quality compression and the target image size to the target difference range when compressing the size of the image after quality compression.

[0055] In one possible implementation, while keeping the size of the image to be compressed unchanged, and ensuring that the image quality value of the compressed image is not less than a quality threshold, the target quality compression value is calculated as the image size that makes the compressed image size closest to the target image size. This includes:

[0056] Keeping the size of the image to be compressed unchanged, the image to be compressed is compressed using a binary search method within the range of the maximum and minimum image quality values. If the image quality value of the compressed image is not less than the quality threshold, the quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated and used as the target quality compression value.

[0057] Alternatively, keep the size of the image to be compressed unchanged, and gradually reduce the maximum image quality value of the image to be compressed by a preset step size. Compress the image quality value of the image to be compressed one by one. If the image quality value of the compressed image is not less than the quality threshold, calculate the quality compression value corresponding to the image size of the compressed image that is closest to the target image size, and use it as the target quality compression value.

[0058] For example, with a quality threshold of 60%, the size of the image to be compressed remains unchanged (1 * original size). The maximum image quality value of the image to be compressed is 100%, and the minimum image quality value is 0%. The bisection point is determined as (0% + 100%) / 2 = 50%. The image to be compressed is then compressed using 50% * the original quality value. Since 50% is less than 60% (the quality threshold), the range 0% - 50% is discarded. The bisection point for the range 50% - 100% is determined as (50% + 100%) / 2 = 75%. The image to be compressed is then compressed again using 75% * the original quality value. Since 75% is greater than 60%, the size relationship between the compressed image size and the target image size is compared. If the compressed image size is greater than the target image size, then... Discard the 75%-100% range, then determine the 50%-75% bisection point: (50%+75%) / 2 = 62.5%. Use 62.5% * the original quality value to compress the image. If it is less than this, discard the 50%-75% range, then determine the 75%-100% bisection point: (75%+100%) / 2 = 87.5%. Use 87.5% * the original quality value to compress the image. Continue this process until the difference between the compressed image size and the target image size no longer changes or is within the set range. At this point, the compressed image size is closest to the target image size, and the corresponding compression value is taken as the target compression value.

[0059] The preset step size can be set according to requirements, such as 10%, 5%, or 1%. For example, with a quality threshold of 60%, a preset step size of 5%, and a maximum image quality value of 100% for the image to be compressed, keeping the size of the image to be compressed unchanged (1 * original size), the image to be compressed is compressed using 100% * original quality value. If 100% is greater than 60% (quality threshold), the size relationship between the compressed image size and the target image size is determined. If it is greater, the quality compression value is determined as: 100% - preset step size (5%) = 95%. The image to be compressed is then compressed using 95% * original quality value. If 95% is greater than 60%, this process continues until the difference between the compressed image size and the target image size no longer changes or is within the set range. At this point, the compressed image size is determined to be closest to the target image size, and the corresponding quality compression value is taken as the target quality compression value.

[0060] In one example, the preset step size can be variable. For instance, if the difference between the size of the compressed image and the target image is within a first range, a first step size is set; if the difference is within a second range, a second step size is set; and if the difference is within a third range, a third step size is set. For example, if the difference is within the first range of 100M-1000M, a first step size of 10% is set; if the difference is within the second range of 1M-100M, a second step size of 5% is set; if the difference is within the third range of 10KB-1M, a third step size of 1%, and so on.

[0061] In this embodiment of the disclosure, the image to be compressed is performed using a binary search method or by decreasing the image quality value. If the image quality value of the compressed image is not less than the quality threshold, the target quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated accurately and efficiently.

[0062] In one possible implementation, in a scenario where the user specifies the size of the image to be compressed, if the size of the image to be compressed remains unchanged, and the image to be compressed is performed using the target quality compression value, and the image quality value of the compressed image is not less than the quality threshold, and the difference between the image size and the target image size meets the user's requirements or reaches the target image size, then there is no need to further compress the size of the compressed image.

[0063] Since the determined target quality compression value is not less than the quality threshold, the size of the image to be compressed remains unchanged. The image to be compressed is then compressed using the target quality compression value. The resulting image quality value is the target quality compression value, which is not less than the quality threshold.

[0064] In one possible implementation, step S104, which maintains the image quality value of the quality-compressed image unchanged and performs size compression on the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, to obtain a compressed image, includes: if the size of the quality-compressed image is larger than the target image size, performing the step: maintaining the image quality value of the quality-compressed image unchanged and performing size compression on the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, to obtain a compressed image.

[0065] In one example, while keeping the size of the image to be compressed unchanged, quality compression is performed using the target quality compression value. If the size of the quality-compressed image is within a positive preset range of the target image size (i.e., the size of the quality-compressed image is larger than the target image size), then size compression continues. Alternatively, if the target difference range is less than a preset range, quality compression can also be performed while keeping the size of the image to be compressed unchanged. If the difference between the size of the quality-compressed image and the target image size is outside the target difference range, then size compression continues.

[0066] In this embodiment of the disclosure, if the size of the quality-compressed image is larger than the size of the target image, the quality-compressed image is further subjected to size compression so that the size-compressed image can more accurately approximate the size of the target image.

[0067] In one possible implementation, step S104 above maintains the image quality value of the quality-compressed image unchanged, and performs size compression on the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thus obtaining the compressed image. This process includes:

[0068] Step 1: Keep the image quality value of the compressed image unchanged, and calculate the compression value that makes the size of the compressed image closest to the size of the target image.

[0069] Step 2: Keep the image quality value of the quality-compressed image unchanged, and then use the size compression value to compress the size of the quality-compressed image to obtain the compressed image.

[0070] In one example, while maintaining the image quality value of the compressed image, a binary search method is used to compress the image size within the range of its maximum and minimum dimensions. This calculates the compression value that most closely approximates the target image size, or the compression value where the difference between the compressed and target image sizes falls within a target range. Alternatively, while maintaining the image quality value, the maximum image size of the compressed image is gradually reduced with a set step size. This process is repeated to calculate the compression value that most closely approximates the target image size, or the compression value where the difference between the compressed and target image sizes falls within a target range. The set step size can be fixed or variable; refer to the preset step size settings described above for details.

[0071] For example, the implementation method for calculating the size compression value of the image after size compression that is closest to the target image size can refer to the implementation method described above for calculating the quality compression value of the image after quality compression that is closest to the target image size.

[0072] After calculating the size compression value, the image quality value of the quality-compressed image remains unchanged. The size compression value is then used to directly compress the size of the quality-compressed image to obtain the compressed image.

[0073] In this embodiment of the disclosure, the image quality value of the quality-compressed image remains unchanged, a size compression value is calculated that makes the image size of the size-compressed image closest to the target image size, and then the size compression value is used to compress the quality-compressed image to accurately compress the image to the target image size.

[0074] In one possible implementation, there are multiple images to be compressed, and the image compression method may further include: allocating a thread for each image to be compressed.

[0075] When there are multiple images to be compressed, a thread is allocated for each image to be compressed, and the allocated thread is used to compress the image.

[0076] Accordingly, step S102 above, which determines the target quality compression value of the image to be compressed, includes: using the thread corresponding to the image to be compressed to execute: determining the target quality compression value of the image to be compressed;

[0077] The above step S103 keeps the size of the image to be compressed unchanged, and uses the target quality compression value to compress the image to obtain a quality compressed image, including: using the thread corresponding to the image to be compressed to execute: keep the size of the image to be compressed unchanged, use the target quality compression value to compress the image to obtain a quality compressed image;

[0078] The above step S104 maintains the image quality value of the quality-compressed image unchanged, and performs size compression on the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thereby obtaining the compressed image. This includes: using the thread corresponding to the image to be compressed to execute: maintaining the image quality value of the quality-compressed image unchanged, performing size compression on the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thereby obtaining the compressed image.

[0079] In this embodiment of the disclosure, when there are multiple images to be compressed, a thread is allocated to each image to be compressed, and the allocated thread is used to compress the image, thereby realizing batch intelligent compression of images.

[0080] In one possible implementation, the above image compression method may further include: outputting at least one of the image size, dimensions, and image quality value of the compressed image.

[0081] In this embodiment of the disclosure, at least one of the image size, dimensions, and image quality value of the compressed image is output so that the user can know the actual image size, dimensions, and image quality value of the compressed image.

[0082] For example, such as Figure 2 As shown, the image compression method provided in this embodiment may include the following steps:

[0083] S201, Obtain the image to be compressed;

[0084] S202, keeping the size of the image to be compressed unchanged, and under the condition that the image quality value of the image after quality compression is not less than the quality threshold, calculate the quality compression value that makes the image size of the image after quality compression closest to the target image size, and use it as the target quality compression value;

[0085] S203, Keep the size of the image to be compressed unchanged, and use the target quality compression value to compress the image to obtain the quality compressed image;

[0086] S204, If the image size of the compressed image is larger than the target image size, keep the image quality value of the compressed image unchanged, and calculate the size compression value that makes the image size of the compressed image closest to the target image size.

[0087] S205, Keep the image quality value of the quality-compressed image unchanged, and use the size compression value to compress the size of the quality-compressed image to obtain the compressed image;

[0088] S206, output at least one of the following: image size, dimensions, and image quality value of the compressed image.

[0089] In this embodiment, while keeping the size of the image to be compressed unchanged, and provided that the image quality value of the compressed image is not less than a quality threshold, a target quality compression value is calculated that makes the size of the compressed image closest to the target image size. The calculated target quality compression value is not less than the quality threshold, and the corresponding compressed image size is closest to the target image size. This ensures that, while keeping the size of the image to be compressed unchanged, quality compression using the target quality compression value guarantees image clarity, and the compressed image size is closest to the target image size. If the compressed image size is larger than the target image size, the image quality value is further kept constant, and a size compression value is calculated that makes the compressed image size closest to the target image size. This size compression value is then used to compress the compressed image, ensuring clarity while making the compressed image size closer to the target image size. This achieves intelligent image compression to a specified size while maintaining image clarity, and allows outputting at least one of the compressed image size, size, and image quality value.

[0090] For example, such as Figure 3 As shown, the electronic device acquires image data to be compressed. This image data may contain data for at least one image to be compressed. Correspondingly, at least one thread is started to call the ImageResizeManager, which initiates the ImageResizeOperation. The ImageResizeHandle is used to keep the size of the image to be compressed constant. While ensuring the image quality value after quality compression is not less than a quality threshold, a target quality compression value is calculated that makes the image size after quality compression closest to the target image size. Specifically, an image compression tool (such as OpenCV) is called to keep the size of the image to be compressed constant and adjust the quality compression value to perform quality compression on the image to be compressed (i.e., ...). Figure 3 The image size is adjusted (located in the arc arrow indicating the target quality compression value) to obtain the quality-compressed image (i.e., the image after compression). Figure 3The image is located in the arc arrow indicating the target quality compression value (the compressed image and its size are shown in the image). The calculation continues until the target quality compression value is obtained, which makes the size of the compressed image closest to the target image size. Further adjustments to the image size handle are made to maintain the image quality value of the compressed image. Figure 3 Keeping the target quality compression value unchanged, calculate the size compression value that makes the size of the compressed image closest to the target image size. Specifically, use an image compression tool to keep the image quality value of the compressed image unchanged, and adjust the size compression value to compress the size of the compressed image (i.e., ...). Figure 3 The image is resized (in the arc-shaped arrow indicating the compression size value) to obtain the compressed image (i.e., Figure 3 The process involves calculating the compressed image size (indicated by the arc arrow in the image compression value calculation) until the compressed image size is closest to the target image size. The image size manager then outputs at least one of the compressed image size, dimensions, and image quality values.

[0091] For example, such as Figure 4 As shown, images to be compressed can be selected individually or in batches via electronic devices, and the size of the compressed image can be specified (in...). Figure 4 The input fields include the target size, compression resolution (blurry, standard, and high definition, which can be represented by corresponding compression quality values), and compression size. When a compression resolution is specified, the electronic device directly compresses the image to be compressed to the specified resolution. When a compression size is specified, the electronic device directly compresses the image to be compressed to the specified size. When the size of the compressed image is specified, or when both the size and compression resolution are specified, the electronic device uses the image compression method provided in this embodiment to compress the image to be compressed and can output at least one of the following: image size, dimensions, and image quality value of the compressed image.

[0092] The image compression method provided in this embodiment compresses the image to a specified size by keeping the size of the image to be compressed unchanged, compressing the quality first using a calculated target quality compression value, then keeping the quality value of the compressed image unchanged, and finally compressing the size using a calculated size compression value. Compared to keeping the size unchanged and only compressing the quality, this method can improve the quality of the compressed image by 20% to 30%, and compared to keeping the quality unchanged and only compressing the size, it can improve the size of the compressed image by about 25%. In other words, while compressing the image to be compressed to a specified size, it can ensure the clarity of the compressed image and also ensure that the compressed size is not too small.

[0093] This disclosure also provides an image compression device, see [link to relevant documentation]. Figure 5The device includes:

[0094] Image acquisition module 501 is used to acquire images to be compressed;

[0095] Compression value determination module 502 is used to determine the target quality compression value of the image to be compressed, wherein the target quality compression value is not less than the quality threshold and the size of the image after compression is within a preset range of the target image size;

[0096] The first image compression module 503 is used to keep the size of the image to be compressed unchanged, and to perform quality compression on the image to be compressed using the target quality compression value to obtain a quality compressed image.

[0097] The second image compression module 504 is used to keep the image quality value of the quality-compressed image unchanged, and to compress the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thus obtaining the compressed image.

[0098] In this embodiment, a target quality compression value for the image to be compressed is first determined. This target quality compression value is not less than a quality threshold and corresponds to a value whose size after quality compression is within a preset range of the target image size. This ensures that, while maintaining the size of the image to be compressed, the target quality compression value is used to compress the image, resulting in a compressed image that maintains image clarity and whose size is within the preset range of the target image size. Furthermore, while maintaining the image quality value of the compressed image, the size of the compressed image is compressed until the difference between the compressed image size and the target image size is within a target difference range. This ensures that the compressed image maintains clarity while its size is close to the target image size, thus achieving intelligent image compression to a specified image size while maintaining image clarity.

[0099] In one possible implementation, the compression value determination module 502 is specifically used to: keep the size of the image to be compressed unchanged, and, provided that the image quality value of the image after quality compression is not less than the quality threshold, calculate the quality compression value that makes the image size of the image after quality compression closest to the target image size, and use it as the target quality compression value.

[0100] In one possible implementation, the compression value determination module 502 described above is specifically used for:

[0101] Keeping the size of the image to be compressed unchanged, the image to be compressed is compressed using a binary search method within the range of the maximum and minimum image quality values. If the image quality value of the compressed image is not less than the quality threshold, the quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated and used as the target quality compression value.

[0102] Alternatively, keep the size of the image to be compressed unchanged, and gradually reduce the maximum image quality value of the image to be compressed by a preset step size. Compress the image quality value of the image to be compressed one by one. If the image quality value of the compressed image is not less than the quality threshold, calculate the quality compression value corresponding to the image size of the compressed image that is closest to the target image size, and use it as the target quality compression value.

[0103] In one possible implementation, the second image compression module 504 is specifically used to: when the image size of the quality-compressed image is larger than the target image size, trigger the second image compression module 504 to perform: keep the image quality value of the quality-compressed image unchanged, compress the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, and obtain the compressed image.

[0104] In one possible implementation, the second image compression module 504 described above is specifically used for:

[0105] While maintaining the image quality value of the compressed image, calculate the size compression value that makes the size of the compressed image closest to the target image size;

[0106] To obtain a compressed image, the quality value of the compressed image is kept unchanged, and the size of the compressed image is compressed using the size compression value.

[0107] In one possible implementation, there are multiple images to be compressed, and the apparatus further includes:

[0108] The thread allocation module is used to allocate one thread for each image to be compressed.

[0109] The compression value determination module 502 mentioned above is specifically used to execute the following using the thread corresponding to the image to be compressed: determine the target quality compression value of the image to be compressed;

[0110] The first image compression module 503 mentioned above is specifically used to execute the following using the thread corresponding to the image to be compressed: keeping the size of the image to be compressed unchanged, compressing the image to be compressed using the target quality compression value, and obtaining the image after quality compression.

[0111] The second image compression module 504 mentioned above is specifically used to execute the following using the thread corresponding to the image to be compressed: keeping the image quality value of the image after quality compression unchanged, compressing the size of the image after quality compression until the difference between the size of the compressed image and the target image size is within the target difference range, thereby obtaining the compressed image.

[0112] In one possible implementation, the above-described apparatus further includes:

[0113] The output module is used to output at least one of the following: image size, dimensions, and image quality value of the compressed image.

[0114] According to embodiments of this disclosure, this disclosure also provides an electronic device, a readable storage medium, and a computer program product.

[0115] This disclosure provides an electronic device, comprising:

[0116] At least one processor; and

[0117] A memory that is communicatively connected to at least one processor; wherein,

[0118] The memory stores instructions that can be executed by at least one processor to enable the at least one processor to perform any of the methods of this disclosure.

[0119] This disclosure provides a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause a computer to perform any of the methods described in this disclosure.

[0120] This disclosure provides a computer program product, including a computer program that, when executed by a processor, implements any of the methods described in this disclosure.

[0121] The collection, storage, use, processing, transmission, provision, and disclosure of user personal information in this technical solution comply with relevant laws and regulations and do not violate public order and good morals. It should be noted that the head model in this embodiment is not a head model specific to any particular user and does not reflect the personal information of any particular user. It should also be noted that the two-dimensional face images in this embodiment are from publicly available datasets.

[0122] Figure 6A schematic block diagram of an example electronic device 600 that can be used to implement embodiments of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the present disclosure described and / or claimed herein.

[0123] like Figure 6 As shown, device 600 includes a computing unit 601, which can perform various appropriate actions and processes based on a computer program stored in read-only memory (ROM) 602 or a computer program loaded from storage unit 608 into random access memory (RAM) 603. RAM 603 may also store various programs and data required for the operation of device 600. The computing unit 601, ROM 602, and RAM 603 are interconnected via bus 604. Input / output (I / O) interface 605 is also connected to bus 604.

[0124] Multiple components in device 600 are connected to I / O interface 605, including: input unit 606, such as keyboard, mouse, etc.; output unit 607, such as various types of monitors, speakers, etc.; storage unit 608, such as disk, optical disk, etc.; and communication unit 609, such as network card, modem, wireless transceiver, etc. Communication unit 609 allows device 600 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0125] The computing unit 601 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 601 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 601 performs the various methods and processes described above, such as the image compression method. For example, in some embodiments, the image compression method may be implemented as a computer software program tangibly contained in a machine-readable medium, such as storage unit 608. In some embodiments, part or all of the computer program may be loaded and / or installed on device 600 via ROM 602 and / or communication unit 609. When the computer program is loaded into RAM 603 and executed by the computing unit 601, one or more steps of the image compression method described above may be performed. Alternatively, in other embodiments, the computing unit 601 may be configured to perform the image compression method by any other suitable means (e.g., by means of firmware).

[0126] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0127] The program code used to implement the methods of this disclosure may be written in any combination of one or more programming languages. This program code may be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing apparatus, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0128] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0129] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0130] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as a data server), or computing systems that include middleware components (e.g., an application server), or computing systems that include frontend components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with embodiments of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., a communication network). Examples of communication networks include local area networks (LANs), wide area networks (WANs), and the Internet.

[0131] Computer systems can include clients and servers. Clients and servers are generally located far apart and typically interact via communication networks. Client-server relationships are created by computer programs running on the respective computers and having a client-server relationship with each other. Servers can be cloud servers, servers in distributed systems, or servers incorporating blockchain technology.

[0132] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this disclosure can be achieved, and this is not limited herein.

[0133] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure should be included within the scope of protection of this disclosure.

Claims

1. An image compression method, comprising: Get the image to be compressed; A target quality compression value is determined for the image to be compressed, wherein the target quality compression value is: a value that is not less than a quality threshold and whose size after quality compression is within a preset range of the target image size, obtained by using an optimization method to compress the image while keeping its size unchanged; the quality threshold is used to ensure the clarity of the compressed image. Keeping the size of the image to be compressed unchanged, the image to be compressed is compressed using the target quality compression value to obtain a quality-compressed image. Keeping the image quality value of the quality-compressed image unchanged, the image is then resized until the difference between the size of the resized image and the target image size is within the target difference range, thus obtaining the compressed image.

2. The method according to claim 1, wherein, Determining the target quality compression value of the image to be compressed includes: Keeping the size of the image to be compressed unchanged, and provided that the image quality value of the image after quality compression is not less than the quality threshold, calculate the quality compression value that makes the image size of the image after quality compression closest to the target image size, and use it as the target quality compression value.

3. The method according to claim 2, wherein, While keeping the size of the image to be compressed unchanged, and ensuring that the image quality value of the compressed image is not less than the quality threshold, the process of calculating a quality compression value that makes the image size of the compressed image closest to the target image size, and using this value as the target quality compression value, includes: Keeping the size of the image to be compressed unchanged, the image to be compressed is performed using a binary search method within the range of the maximum and minimum image quality values ​​of the image to be compressed. If the image quality value of the compressed image is not less than the quality threshold, the quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated and used as the target quality compression value. Alternatively, keeping the size of the image to be compressed unchanged, the maximum image quality value of the image to be compressed is gradually reduced by a preset step size, and the image to be compressed is compressed in turn. If the image quality value of the compressed image is not less than the quality threshold, the quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated and used as the target quality compression value.

4. The method according to claim 1, wherein, The process of maintaining the image quality value of the quality-compressed image unchanged, and then compressing the size of the quality-compressed image until the difference between the size of the compressed image and the target image size is within a target difference range, to obtain the compressed image, includes: If the size of the compressed image is larger than the target image size, the following steps are performed: keeping the image quality value of the compressed image unchanged, compressing the size of the compressed image until the difference between the size of the compressed image and the target image size is within the target difference range, thus obtaining the compressed image.

5. The method according to claim 1 or 4, wherein, The process of maintaining the image quality value of the quality-compressed image unchanged, and then compressing the size of the quality-compressed image until the difference between the size of the compressed image and the target image size is within a target difference range, to obtain the compressed image, includes: While keeping the image quality value of the compressed image unchanged, calculate the size compression value that makes the image size of the compressed image closest to the target image size; Keeping the image quality value of the quality-compressed image unchanged, the size of the quality-compressed image is compressed using the size compression value to obtain the compressed image.

6. The method according to any one of claims 1-5, wherein the images to be compressed are multiple, and the method further includes: Allocate one thread for each image to be compressed; Determining the target quality compression value of the image to be compressed includes: using the thread corresponding to the image to be compressed to execute: determining the target quality compression value of the image to be compressed; The step of keeping the size of the image to be compressed unchanged and compressing the image using the target quality compression value to obtain a quality-compressed image includes: using the thread corresponding to the image to be compressed to execute: keeping the size of the image to be compressed unchanged and compressing the image using the target quality compression value to obtain a quality-compressed image; The step of keeping the image quality value of the quality-compressed image unchanged and compressing the size of the quality-compressed image until the difference between the size of the compressed image and the target image size is within a target difference range to obtain a compressed image includes: using the thread corresponding to the image to be compressed to execute: keeping the image quality value of the quality-compressed image unchanged and compressing the size of the quality-compressed image until the difference between the size of the compressed image and the target image size is within a target difference range to obtain a compressed image.

7. The method according to claim 6, further comprising: Output at least one of the following: image size, dimensions, and image quality value of the compressed image.

8. An image compression device, comprising: The image acquisition module is used to acquire images to be compressed. The compression value determination module is used to determine the target quality compression value of the image to be compressed, wherein the target quality compression value is: a value not less than a quality threshold determined by using an optimization method to compress the image to be compressed while keeping the size of the image to be compressed unchanged, and the size of the image after quality compression is within a preset range of the target image size; the quality threshold is used to ensure the clarity of the image after compression. The first image compression module is used to keep the size of the image to be compressed unchanged, and to perform quality compression on the image to be compressed using the target quality compression value to obtain a quality-compressed image. The second image compression module is used to keep the image quality value of the quality-compressed image unchanged, and to compress the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thereby obtaining the compressed image.

9. The apparatus according to claim 8, wherein, The compression value determination module is specifically used to: keep the size of the image to be compressed unchanged, and, provided that the image quality value of the image after quality compression is not less than the quality threshold, calculate the quality compression value that makes the image size of the image after quality compression closest to the target image size, and use this value as the target quality compression value.

10. The apparatus according to claim 9, wherein, The compression value determination module is specifically used for: Keeping the size of the image to be compressed unchanged, the image to be compressed is performed using a binary search method within the range of the maximum and minimum image quality values ​​of the image to be compressed. If the image quality value of the compressed image is not less than the quality threshold, the quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated and used as the target quality compression value. Alternatively, keeping the size of the image to be compressed unchanged, the maximum image quality value of the image to be compressed is gradually reduced by a preset step size, and the image to be compressed is compressed in turn. If the image quality value of the compressed image is not less than the quality threshold, the quality compression value corresponding to the image size of the compressed image that is closest to the target image size is calculated and used as the target quality compression value.

11. The apparatus according to claim 8, wherein, The second image compression module is specifically used to: when the image size of the quality-compressed image is larger than the target image size, trigger the second image compression module to perform: keeping the image quality value of the quality-compressed image unchanged, compressing the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thereby obtaining the compressed image.

12. The apparatus according to claim 8 or 11, wherein, The second image compression module is specifically used for: While keeping the image quality value of the compressed image unchanged, calculate the size compression value that makes the image size of the compressed image closest to the target image size; Keeping the image quality value of the quality-compressed image unchanged, the size of the quality-compressed image is compressed using the size compression value to obtain the compressed image.

13. The apparatus according to any one of claims 8-12, wherein the images to be compressed are multiple, and the apparatus further comprises: The thread allocation module is used to allocate one thread for each image to be compressed. The compression value determination module is specifically used to execute, using the thread corresponding to the image to be compressed, to determine the target quality compression value of the image to be compressed. The first image compression module is specifically used to execute the following using the thread corresponding to the image to be compressed: keeping the size of the image to be compressed unchanged, and using the target quality compression value to compress the image to be compressed to obtain a quality-compressed image; The second image compression module is specifically used to execute the following using the thread corresponding to the image to be compressed: keeping the image quality value of the quality-compressed image unchanged, compressing the size of the quality-compressed image until the difference between the size of the size-compressed image and the target image size is within the target difference range, thereby obtaining the compressed image.

14. The apparatus of claim 13, further comprising: The output module is used to output at least one of the following: image size, dimensions, and image quality value of the compressed image.

15. An electronic device comprising: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-7.

16. A non-transitory computer-readable storage medium storing computer instructions, wherein, The computer instructions are used to cause the computer to perform the method according to any one of claims 1-7.

17. A computer program product comprising a computer program that, when executed by a processor, implements the method according to any one of claims 1-7.

Citation Information

Patent Citations

  • Picture compression method and device and storage medium

    CN113724345A

  • Methods, apparatuses, systems, and non-transitory computer readable media for improving and / or optimizing image compression quality

    US20170195674A1