Picture compression method and compression device

By selecting an appropriate compression strategy based on the image format and performing similarity verification, the problem of poor visual quality after image compression in existing technologies is solved, achieving efficient compression and improved user experience.

CN116055728BActive Publication Date: 2026-05-29杭州普联系统技术有限公司

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
杭州普联系统技术有限公司
Filing Date
2023-01-10
Publication Date
2026-05-29

AI Technical Summary

Technical Problem

In existing technologies, image compression methods only control the volume dimension, resulting in poor visual quality of compressed images and affecting the user's visual experience.

Method used

Based on the image format, a matching compression strategy is selected, and a similarity verification is performed. The target image is saved only when the similarity between the images before and after compression is greater than a threshold, thus ensuring visual quality.

Benefits of technology

While maintaining visual quality, we have improved image compression efficiency, reduced the installation package size, and enhanced the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116055728B_ABST
    Figure CN116055728B_ABST
Patent Text Reader

Abstract

The application provides a picture compression method and a compression device, and belongs to the technical field of picture processing. The compression method comprises the following steps: determining a first compression strategy from a plurality of preset picture compression strategies according to the picture format of a first picture; performing compression processing on the first picture through the first compression strategy to obtain a target picture; and saving the target picture if the similarity value of the target picture and the first picture is greater than or equal to a preset first threshold. The application can guarantee the visual quality of the picture and improve the visual experience of the user while efficiently compressing the picture.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of image processing technology, and in particular to an image compression method and compression apparatus. Background Technology

[0002] With the development of internet technology, the functions of various applications (Apps) in the Android system are becoming increasingly diverse, and the size of Android application packages (APKs) is also growing larger. These packages typically contain a large number of images, significantly impacting their size. To reduce the size of the installation package, various formats of raw images can be compressed during the app development phase, thereby reducing the APK size and ultimately improving the app's performance in terms of installation time, runtime memory usage, and image loading speed.

[0003] In related technologies, the images to be compressed are first obtained from the installation package. Then, various methods, such as Huffman compression, are used to compress the images until their size is less than or equal to a preset threshold. Finally, the compressed images are saved to the target folder. This image compression method only controls compression from the single dimension of image size. It primarily uses a set size threshold to determine whether an image needs further compression. It does not consider the impact of multiple compressions on the image's visual quality, which may result in poor visual quality of the compressed image and a poor user experience. Summary of the Invention

[0004] This application provides a method and apparatus for compressing images, which can efficiently compress images while ensuring their visual quality and improving the user's visual experience.

[0005] Firstly, a method for compressing images is provided, including:

[0006] The first compression strategy is determined from multiple preset image compression strategies based on the image format of the first image.

[0007] The first image is compressed using the first compression strategy to obtain the target image;

[0008] If the similarity value between the target image and the first image is greater than or equal to a preset first threshold, then the target image is saved.

[0009] In one possible design, compressing the first image using the first compression strategy includes:

[0010] The first image is compressed using a preset first sub-strategy to obtain an intermediate image;

[0011] If the similarity value between the intermediate image and the first image is greater than or equal to the first threshold, the intermediate image is compressed using a preset second sub-strategy to obtain the target image.

[0012] In one possible design, the compression method further includes:

[0013] If the similarity value between the target image and the first image is less than the first threshold, then the intermediate image is saved.

[0014] In one possible design, the compression method further includes:

[0015] If the similarity value between the target image and the first image is less than the first threshold, then the first image is saved.

[0016] In one possible design, before determining the first compression strategy from multiple preset image compression strategies based on the image format of the first image, the compression method further includes:

[0017] It is determined that the metadata of the first image does not contain an indication field, which is used to indicate that the image has been compressed.

[0018] Before saving the target image, the compression method further includes:

[0019] Write the indicated field into the metadata of the target image.

[0020] In one possible design, before determining the first compression strategy from multiple preset image compression strategies based on the image format of the first image, the compression method further includes:

[0021] Traverse the contents of all files to be submitted to obtain the path of at least one image to be processed, wherein the at least one image to be processed includes the first image.

[0022] In one possible design, determining the first compression strategy from multiple preset image compression strategies based on the image format of the first image includes:

[0023] The first compression strategy is determined based on the image format of the first image and the information of the first image's alpha channel.

[0024] In one possible design, the first compression strategy includes at least one of performing image bit compression, removing image metadata, and converting the image format.

[0025] Secondly, an image compression device is provided, comprising:

[0026] The determining unit is used to determine a first compression strategy from multiple preset image compression strategies based on the image format of the first image.

[0027] A compression unit is used to compress the first image using the first compression strategy to obtain a target image;

[0028] A storage unit is configured to store the target image when the similarity value between the target image and the first image is greater than or equal to a preset first threshold.

[0029] In one possible design, the compression unit is specifically used for:

[0030] The first image is compressed using a preset first sub-strategy to obtain an intermediate image;

[0031] If the similarity value between the intermediate image and the first image is greater than or equal to the first threshold, the intermediate image is compressed using a preset second sub-strategy to obtain the target image.

[0032] In one possible design, the storage unit is also used for:

[0033] If the similarity value between the target image and the first image is less than the first threshold, then the intermediate image is saved.

[0034] In one possible design, the storage unit is also used for:

[0035] If the similarity value between the target image and the first image is less than the first threshold, then the first image is saved.

[0036] In one possible design, the determining unit is further used for:

[0037] It is determined that the metadata of the first image does not contain an indication field, which is used to indicate that the image has been compressed.

[0038] The compression device further includes:

[0039] A writing unit is used to write the indication field into the metadata of the target image.

[0040] In one possible design, the compression device further includes:

[0041] The acquisition unit is used to traverse the contents of all files to be submitted in order to obtain the path of at least one image to be processed, wherein the at least one image to be processed includes the first image.

[0042] In one possible design, the determining unit is specifically used for:

[0043] The first compression strategy is determined based on the image format of the first image and the information of the first image's alpha channel.

[0044] In one possible design, the first compression strategy includes at least one of performing image bit compression, removing image metadata, and converting the image format.

[0045] Thirdly, an image compression apparatus is provided, comprising: a processor; a memory; and a computer program, wherein the computer program is stored in the memory, and when the computer program is executed by the processor, causes the compression apparatus to perform the compression method provided by any of the possible designs in the first aspect described above.

[0046] Fourthly, a computer-readable storage medium is provided, the computer-readable storage medium storing program code that, when the computer program code is run on a computer, causes the computer to perform the compression method provided by any of the possible designs in the first aspect above.

[0047] Fifthly, a chip system is provided, including a processor for calling and running a computer program from a memory, causing an electronic device equipped with the chip system to perform the compression method provided by any possible design in the first aspect above.

[0048] In a sixth aspect, a computer program product is provided, the computer program product comprising: computer program code, which, when run on a computer, causes the computer to perform the compression method provided by any possible design in the first aspect above.

[0049] It should be noted that the above-mentioned computer program code can be stored in whole or in part on the first storage medium, wherein the first storage medium can be packaged together with the processor or packaged separately from the processor, and this application does not make specific limitations in this regard.

[0050] Since a single compression strategy cannot guarantee high compression efficiency for images of different formats, the compression method provided in this application, upon obtaining an image to be compressed (i.e., the first image), firstly selects a matching compression strategy (i.e., the first compression strategy) from a set of preset compression strategies based on the format of the first image. Different compression strategies can be used to compress images of different formats, thereby ensuring high compression efficiency for images of various formats. Thus, when batch compressing multiple images of different formats contained in a project folder during the APP development phase, it helps improve the overall image compression efficiency, thereby effectively reducing the size of the folder (installation package).

[0051] Furthermore, according to the compression method provided in this application embodiment, the image obtained after compression (i.e., the target image) is not necessarily saved. Instead, a similarity value verification is required. Only when the similarity value between the two images before and after compression is greater than or equal to a preset first threshold can the image be saved. In this case, the target image can be used to replace the first image to reduce the size of the installation package. Conversely, when the similarity value between the two images before and after compression is less than the preset first threshold, the first image can be saved instead of the target image. Therefore, while efficiently compressing the image, the visual quality of the image can be guaranteed, which is beneficial to improving the user's visual experience.

[0052] The compression method provided in this application, while ensuring a certain level of image visual quality, can achieve a compression rate of 65%-75% for PNG format images, 50%-70% for JPG format images, and 10%-20% for GIF format images. Compared to other Android platform solutions that perform compression optimization during the actual image loading process, this solution addresses the image compression problem at the source of the project, offering better versatility. Attached Figure Description

[0053] Figure 1 This is a flowchart of an example of an image compression method provided in the embodiments of this application.

[0054] Figure 2 This is a flowchart of another example of the image compression method provided in the embodiments of this application.

[0055] Figure 3 This is a flowchart of yet another example of the image compression method provided in the embodiments of this application.

[0056] Figure 4 This is a flowchart of yet another example of the image compression method provided in the embodiments of this application.

[0057] Figure 5This is a flowchart of yet another example of the image compression method provided in the embodiments of this application.

[0058] Figure 6 This is a flowchart of yet another example of the image compression method provided in the embodiments of this application.

[0059] Figure 7 This is a flowchart of yet another example of the image compression method provided in the embodiments of this application.

[0060] Figure 8 This is a schematic block diagram of an image compression device provided in the embodiments of this application.

[0061] Figure 9 This is a structural block diagram of the image compression device provided in the embodiments of this application. Detailed Implementation

[0062] The technical solutions of this application will now be described with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them.

[0063] In the following description, specific details such as particular system architectures and techniques are set forth for illustrative purposes and not for limitation, in order to provide a thorough understanding of the embodiments of this application. However, those skilled in the art will understand that this application may also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known systems, apparatuses, circuits, and methods have been omitted so as not to obscure the description of this application with unnecessary detail.

[0064] The term "comprising" in this document indicates the presence of the described feature, whole, step, operation, element, and / or component, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or collections thereof. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.

[0065] Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of embodiments of this application, unless otherwise stated, "a plurality of" means two or more.

[0066] In this article, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. Additionally, the character " / " in this article generally indicates that the preceding and following related objects have an "or" relationship.

[0067] This application first provides an image compression method that can efficiently compress images while maintaining their visual quality and improving the user's visual experience.

[0068] Figure 1 This is a flowchart of an example of the image compression method 100 provided in this application embodiment. For example... Figure 1 As shown, the compression method 100 provided in this application embodiment can be applied to any electronic device such as a desktop computer, server, laptop computer, tablet computer, and smartphone, but is not limited thereto. The execution entity of the compression method 100 can be an electronic device or a processor or chip inside the electronic device. The compression method 100 includes steps 110-150.

[0069] Step 110: The electronic device determines a first compression strategy from multiple preset image compression strategies based on the image format of the first image. The first image can be one of at least one image to be processed, for example, any one of them.

[0070] Step 120: The electronic device compresses the first image using the first compression strategy to obtain the target image;

[0071] Step 130: The electronic device determines whether the similarity value between the target image and the first image is greater than or equal to a preset first threshold. If yes, proceed to step 140; otherwise, proceed to step 150.

[0072] Step 140: The electronic device saves the target image.

[0073] Step 150: The electronic device saves the first image.

[0074] To reduce the size of the installation package, during the project development phase, the electronic device can compress raw images of various formats within the project folder. Here, the images to be processed can include one or more, including a first image. The electronic device can compress these at least one image in a specific order. When processing the first image, the electronic device first determines a first compression strategy from multiple preset image compression strategies based on the image format of the first image, and then uses the first compression strategy to compress the first image to obtain the target image. This first compression strategy matches the image format of the first image, and compressing the first image using the first compression strategy has better compression efficiency, meaning the target image has a smaller file size.

[0075] The electronic device further determines whether the similarity value between the target image and the first image is greater than or equal to a preset first threshold. This first threshold can be preset according to actual needs; for example, it can be 70%–90%, specifically 75%, 80%, or 85%, but is not limited to these. If the similarity value between the target image and the first image is greater than or equal to the preset first threshold, it means that the visual quality (visual experience) of the target image is relatively close to that of the first image (i.e., the compressed image relative to the original image), and the visual quality of the image has not been significantly affected. In this case, the electronic device can save the target image to a pre-set target folder (output target image), replacing the first image with the target image, thereby reducing the size of the installation package. If the similarity value between the target image and the first image is less than the preset first threshold, it means that the visual quality of the target image has been significantly affected (loss) compared to the first image. In this case, to ensure the visual quality of the image, the first image can be saved to a pre-set target folder (output first image), instead of saving the target image.

[0076] Since a single compression strategy cannot guarantee high compression efficiency for images of different formats, the compression method 100 provided in this application, upon obtaining an image to be compressed (i.e., the first image), firstly selects a matching compression strategy (i.e., the first compression strategy) from a set of preset compression strategies based on the format of the first image. Different compression strategies can be used to compress images of different formats, thereby ensuring high compression efficiency for images of various formats. In this way, when batch compressing multiple images of different formats contained in a project folder during the APP development phase, it helps improve the overall image compression efficiency, thereby effectively reducing the size of the project folder (installation package).

[0077] Furthermore, according to the compression method 100 provided in this application embodiment, the image obtained after compression (i.e., the target image) is not necessarily saved. Instead, a similarity value verification is required. Only when the similarity value between the two images before and after compression is greater than or equal to a preset first threshold can the image be saved. In this case, the target image can be used to replace the first image to reduce the size of the installation package. Conversely, when the similarity value between the two images before and after compression is less than the preset first threshold, the first image can be saved instead of the target image. Therefore, while efficiently compressing the image, the visual quality of the image can be guaranteed, which is beneficial to improving the user's visual experience.

[0078] The compression method 100 provided in this application, while ensuring a certain level of image visual quality, can achieve a general compression rate of 65%-75% for PNG format images, 50%-70% for JPG format images, and 10%-20% for GIF format images. Compared to other Android platform solutions that perform compression optimization during the actual image loading process, this solution addresses the image compression problem from the source of the project, offering better universality.

[0079] Optionally, the first image can be in any of the following formats: JPG, PNG, or GIF, but is not limited to these. For example, the first image can also be in the following formats: PSD, TIF, BMP, PCX, TGA, EXIF, FPX, SVG, CDR, PCD, DXF, UFO, EPS, AI, RAW, WMF, WebP, AVIF, or APNG.

[0080] In step 110, the electronic device can determine a first compression strategy from multiple preset image compression strategies based on the image format of the first image. This application does not specifically limit how the electronic device determines the first compression strategy; for example, it can be determined based on a preset algorithm or logic. In this embodiment, the electronic device can determine the first compression strategy based on a pre-set mapping relationship between image formats and compression strategies.

[0081] For example, multiple image formats can be matched one-to-one with multiple preset image compression strategies. The electronic device can first determine the image format of the first image, and then determine the first compression strategy from the multiple preset image compression strategies according to the above correspondence.

[0082] For example, there could be multiple image formats corresponding to one image compression strategy, in which case the image compression strategy is applicable to multiple different image formats.

[0083] For example, one image format could correspond to multiple image compression strategies, in which case multiple compression strategies apply to the same image format. Furthermore, the electronic device can select any one of these strategies, or further combine other image information (such as attribute information) to determine which specific compression strategy to choose.

[0084] Table 1 shows the mapping relationship between image formats and compression strategies. As a specific example, if the electronic device determines that the first image's format is format #1, then according to Table 1, the first compression strategy can be determined to be compression strategy #1. If the electronic device determines that the first image's format is format #2, then according to Table 1, the first compression strategy can be determined to be compression strategy #2. If the electronic device determines that the first image's format is format #3 or format #4, then according to Table 1, the first compression strategy can be determined to be compression strategy #3.

[0085] Table 1:

[0086]

[0087] If the electronic device determines that the image format of the first image is format #5, then according to the contents of Table 1, the first compression strategy can be determined to be either compression strategy #4 or compression strategy #5. The electronic device can choose either of the two as the first compression strategy, or further combine other information of the first image to determine which one to choose as the specific first compression strategy.

[0088] As one possible implementation, a first compression strategy can be determined based on the image format of the first image and the information of its alpha channel. Here, the alpha channel information of the first image can include whether an alpha channel exists and whether unnecessary alpha channels exist.

[0089] Specifically, for images in formats like PNG, they can be broadly categorized into two types: those with an alpha channel and those without. PNG images with an alpha channel will have a transparent ring around the image during preview; in these cases, the alpha channel is necessary. However, for some PNG images, even if they contain an alpha channel, there's no need for them to be displayed transparently. In these cases, the alpha channel is unnecessary.

[0090] Image files are stored in units of pixels. The number of bits refers to how many bytes are used to store one pixel. A typical PNG image with an alpha channel requires 32 bits of data per pixel to store RGBA, where A represents the space required for the alpha channel. In other words, a PNG image with an alpha channel is 32-bit. If the alpha channel data for each pixel is FF (i.e., opaque), then the image contains unnecessary alpha channels. Electronic devices can use this information, combined with the format of the first image, to determine a first compression strategy from multiple compression strategies.

[0091] The image compression strategy here may include any method, algorithm (such as the aforementioned Huffman compression algorithm), approach or steps that can compress images, and this application does not impose any special limitations on it.

[0092] Optionally, the image compression strategy may include one or more of the following: bit compression, removal of image metadata, and conversion of image format.

[0093] For example, a preset algorithm can be used to compress a 32-bit or 24-bit image into an 8-bit or 16-bit image.

[0094] For example, unnecessary metadata can be removed from images.

[0095] Here, image metadata refers to data describing the image's characteristics, including information such as the image's author and date. Image compression can be achieved by removing some metadata. This "unnecessary metadata" can be verified by a pre-defined algorithm through certain logic. For example, the contents of metadata blocks such as XMP and EXIF ​​can be removed.

[0096] For example, image compression can be achieved by converting the image format. For instance, PNG images can be converted to JPG or lossless WebP formats. Testing has shown that converting PNG images to WebP format can further reduce their file size.

[0097] Optionally, the electronic device can compress the first image using locally installed software or tools, or it can use an online image compression tool. In other words, the preset image compression strategy can include the electronic device calling a local program or an online program (tool) to compress the first image.

[0098] For example, the compression of the first image can be achieved using tools such as tinyPng, PNGCrush, packJPG, pngquant, or gifsicle.

[0099] As a concrete example, the online image compression tool tinyPng is one of the better compression tools among these processing tools (including compression ratio and post-compression image quality). Therefore, tinyPng can be used to compress the first image. The electronic device can then send an HTTP request to tinyPng, carrying the first image in the request message. tinyPng compresses the first image to obtain the target image and returns it to the electronic device.

[0100] In steps 120-140, the electronic device compresses the first image using a determined first compression strategy to obtain a target image. If the similarity value between the target image and the first image is greater than or equal to a preset first threshold, the target image is saved.

[0101] Here, the similarity value represents the similarity between the target image and the first image. The higher the similarity value, the more similar the two images are before and after processing, and the closer their visual quality (visual experience) is. This similarity value can be calculated using any image similarity comparison algorithm such as histogram or perceptual hashing.

[0102] In this embodiment, the similarity value can be calculated using structural similarity (SSIM). In other words, the similarity value in this application can be an SSIM value. SSIM is an indicator used to measure image similarity and can also be used to judge the quality of compressed images. Compared to other algorithms, the SSIM algorithm is based on human visual perception and can better measure how the image appears to the human eye after processing.

[0103] Figure 2 This is a flowchart illustrating another example of the image compression method provided in the embodiments of this application. The following is a description of the method in conjunction with the attached diagram. Figure 2 The compression method provided in the embodiments of this application will be further described. Figure 2 and Figure 1 The same steps can be found in the aforementioned section. Figure 1 The relevant information will not be repeated in this embodiment.

[0104] Step 101: The electronic device traverses all files to be submitted to obtain the path of at least one image to be processed.

[0105] Specifically, the code version management remote repository stores the final version of the Android project code. When synchronizing code with the remote repository using version management tools, it must be ensured that all images in this commit are compressed, and unprocessed images are not allowed to be synchronized to the remote repository. At this time, the electronic device can traverse the contents of all files to be committed to obtain the path (e.g., a relative path) of at least one image to be processed, thereby determining the number of images that need to be compressed and their storage path. The electronic device can then process multiple images to be processed sequentially in a certain order.

[0106] Optionally, the content of all files to be submitted here can be the entire Android project file, that is, all images in the entire Android project are not compressed. In this case, all images in the project need to be processed once (full processing).

[0107] Specifically, first, the initial directory of the project is traversed recursively to obtain the paths of folders in the project that begin with "drawable" (folders under the "build" folder that meet the criteria are excluded during the recursion). Second, the contents of each file in the target folder are traversed sequentially, and the file extension is used to determine whether the file is an image. The paths of image resources in formats such as jpg, png, and gif are the paths of the images to be processed.

[0108] Optionally, the images in the Android project may have already undergone a full processing, and the content of all files to be submitted here can be the content of added or replaced files in subsequent submissions.

[0109] At this point, you can use a third-party code version control tool to obtain the paths of all files involved in this commit. Then, you can iterate through these file paths and determine whether a file is an image by its file extension. The paths of image resources in formats such as jpg, png, and gif are the paths of the images to be processed.

[0110] Step 102: The electronic device determines whether the metadata of the first image contains an indication field, which indicates that the image has been compressed (i.e., the image has been compressed). If the metadata of the first image contains the indication field, it means that the first image has already been processed once and does not need to be processed again, and the process proceeds to step 103. If the metadata of the first image does not contain the indication field, it means that the first image has not yet been compressed, and the process proceeds to step 110.

[0111] Optionally, the indicator field here can be the isModifiedByTPImageHandler field, or other fields. The indicator field here can include one or more bytes or bits.

[0112] Step 103: Process the next image. This involves using the same compression method to compress the remaining images until all images have been compressed.

[0113] Step 110: The electronic device determines a first compression strategy from multiple preset image compression strategies based on the image format of the first image, wherein the first image is any one of at least one image to be processed.

[0114] Step 120: The electronic device compresses the first image using the first compression strategy to obtain the target image;

[0115] Step 130: The electronic device determines whether the similarity value between the target image and the first image is greater than or equal to a preset first threshold. If yes, proceed to step 131; otherwise, proceed to step 150.

[0116] Step 131: The electronic device writes the indication field into the metadata of the target image. By verifying the indication field as described above, the step of writing the indication field after processing ensures the "uniqueness" of the image compression process, preventing the image quality from continuously deteriorating after multiple compression processes.

[0117] Step 140: The electronic device saves the target image after writing the instruction field.

[0118] Step 150: The electronic device saves the first image.

[0119] Figure 3 This is a flowchart illustrating another example of the image compression method provided in the embodiments of this application. The following is a description of the method in conjunction with the attached diagram. Figure 3 The compression method provided in the embodiments of this application will be further described. Figure 3 and Figure 1 The same steps can be found in the aforementioned section. Figure 1 The relevant information will not be repeated in this embodiment.

[0120] Step 110: The electronic device determines a first compression strategy from multiple preset image compression strategies based on the image format of the first image, wherein the first image is any one of at least one image to be processed.

[0121] Step 120 specifically includes steps 121, 122 and 123.

[0122] Step 121: The electronic device compresses the first image using a preset first sub-strategy to obtain an intermediate image;

[0123] Step 122: The electronic device determines whether the similarity value between the intermediate image and the first image is greater than or equal to a first threshold. If yes, proceed to step 123. If no, proceed to step 150: The electronic device saves the first image.

[0124] Step 123: The electronic device compresses the intermediate image using a preset second sub-strategy to obtain the target image.

[0125] In other words, the first compression strategy can include multiple sub-strategies, including the first sub-strategy and the second sub-strategy. Under the premise of ensuring that the compressed image has sufficient image quality, the image can be compressed multiple times (e.g., twice or more), thereby further improving compression efficiency and reducing the size of the target image.

[0126] Optionally, the first sub-policy and the second sub-policy can be the same or different.

[0127] Optionally, the first sub-strategy may include one or more of the following: performing image bit compression, removing image metadata, and converting the image format.

[0128] Optionally, the second sub-strategy may include one or more of the following: performing image bit compression, removing image metadata, and converting the image format.

[0129] For example, the first sub-strategy could be to perform bit compression and remove metadata from the first image using local or online tools to obtain an intermediate image. The second sub-strategy could be to convert the format of the intermediate image to obtain the target image.

[0130] For example, the first sub-strategy could be to convert the format of the first image to obtain an intermediate image, and the second sub-strategy could be to use local or online tools to compress the bit depth of the intermediate image and remove the image's metadata to obtain the target image.

[0131] Step 130: The electronic device determines whether the similarity value between the target image and the first image is greater than or equal to a preset first threshold. If yes, proceed to step 140; otherwise, proceed to step 160.

[0132] Step 140: The electronic device saves the target image.

[0133] Step 160: The electronic device saves the intermediate image. The intermediate image is the image obtained by compressing the first image using the first sub-strategy. It meets the visual quality requirements of the image. If the target image does not meet the visual quality requirements, the intermediate image can be saved to a preset target folder to replace the first image.

[0134] The compression method provided in this application will be further described below with reference to specific examples. The compression methods 200, 300, 400 and 500 provided in the following embodiments can be regarded as further explanations of the aforementioned compression method 100, and are more specific and subordinate embodiments of compression method 100.

[0135] Figure 4 This is a flowchart of the image compression method 200 provided in an embodiment of this application. For example... Figure 4 As shown, compression method 200 includes steps 201-225.

[0136] Step 201, image processing begins;

[0137] Step 202: Obtain the list of paths for the images to be processed;

[0138] Step 203: Process a certain image (i.e., the first image) according to a preset order;

[0139] Step 204: Determine if the image is in PNG format. If so, proceed to step 205.

[0140] Step 205: Determine whether the PNG image contains an alpha channel. If so, proceed to step 206.

[0141] Step 206: Determine whether there is an unnecessary alpha channel (i.e., continue to determine the necessity of the alpha channel, that is, determine whether the PNG image is 32-bit and whether the values ​​of the alpha channels are all FF). If so, proceed to step 207.

[0142] Step 207: Convert the PNG image to JPG format (that is, compress the first image into an intermediate image).

[0143] Step 208: Calculate the similarity value between the images before and after processing (i.e., calculate the similarity value between the intermediate image and the first image, such as the SSIM value).

[0144] Step 209: Determine whether the similarity value is greater than the similarity threshold (i.e., the aforementioned first threshold). If yes, proceed to step 210; otherwise, proceed to step 224 and save the original image (i.e., save the first image).

[0145] Step 210: Send an HTTP request to tinyPng to establish an HTTP connection and transmit the image to tinyPng. Then, use its internal algorithm to compress the image and remove metadata (i.e., compress the intermediate image into the target image).

[0146] Step 211: Calculate the similarity value between the images before and after processing (i.e., calculate the similarity value between the target image and the first image).

[0147] Step 212: Determine whether the similarity value is greater than the similarity threshold (i.e., the aforementioned first threshold). If yes, proceed to step 213; otherwise, proceed to step 225 and save the image obtained after processing in step 207 (i.e., save the intermediate image).

[0148] Step 213: Save the processed image (i.e., save the target image).

[0149] Step 214: If it is determined in step 205 that the PNG format image does not contain an alpha channel, or in step 206 that it does not contain an unnecessary alpha channel (i.e., the value of the alpha channel is necessary), then an HTTP request is sent to tinyPng, that is, an HTTP connection is established, the image is transmitted to tinyPng, and its internal algorithm performs bit compression and metadata removal (i.e., compressing the first image into an intermediate image).

[0150] Step 215: Calculate the similarity value between the images before and after processing (i.e., calculate the similarity value between the intermediate image and the first image, such as the SSIM value).

[0151] Step 216: Determine whether the similarity value is greater than the similarity threshold (i.e., the aforementioned first threshold). If yes, proceed to step 217; otherwise, proceed to step 224: save the original image (i.e., save the first image).

[0152] Step 217: Convert the PNG image to a lossless WebP image (i.e., compress the intermediate image into the target image). Then proceed to step 211: Calculate the similarity value between the images before and after processing (i.e., calculate the similarity value between the target image and the first image). Then proceed to step 212: Determine if the similarity value is greater than the similarity threshold (i.e., the aforementioned first threshold). If yes, proceed to step 213: Save the processed image (i.e., save the target image); otherwise, proceed to step 225: Save the image obtained after processing in step 214 (i.e., save the intermediate image).

[0153] Step 218: If it was determined in step 204 that the image is not in PNG format, then determine whether the image is in JPG format. If so, proceed to step 210, send an HTTP request to tinyPng, establish an HTTP connection, and transmit the image to tinyPng. Its internal algorithm performs bit compression and metadata removal (compressing the first image into the target image). Then proceed to step 211, calculate the similarity value between the images before and after processing (i.e., calculate the similarity value between the target image and the first image); then proceed to step 212, determine whether the similarity value is greater than the similarity threshold (i.e., the aforementioned first threshold). If so, proceed to step 213, save the processed image (i.e., save the target image). If not, proceed to step 225, save the original image (i.e., save the first image).

[0154] Step 219: If it was determined in step 218 that the image format is not JPG, then further determine whether the image format is GIF. If yes, proceed to step 220; otherwise, return to step 203.

[0155] Step 220: Compress using gifsicle (that is, compress the first image into the target image).

[0156] Step 221: Use GIFsicle to extract the first frame of the image before and after compression (GIF format images usually have multiple frames; here, any single frame can be extracted). Then proceed to step 211 to calculate the similarity value between the processed and unprocessed images (i.e., calculate the similarity value between the target image and the first image). Next, proceed to step 212 to determine if the similarity value is greater than the similarity threshold (i.e., the aforementioned first threshold). If yes, proceed to step 213 to save the processed image (i.e., save the target image). If not, proceed to step 225 to save the original image (i.e., save the first image).

[0157] Step 222: Determine if all images have been processed. If yes, proceed to step 223; otherwise, return to step 203.

[0158] Step 223, image processing complete.

[0159] Figure 5 This is a flowchart of the image compression method 300 provided in an embodiment of this application. For example... Figure 5 As shown, compression method 300 includes steps 301-306.

[0160] Step 301: Begin processing the image.

[0161] Step 302: Obtain the relative path of the image to be processed.

[0162] Step 303: Determine whether the image's metadata contains the isModifiedByTPImageHandler field (i.e., the aforementioned indicator field). If yes, return to step 302; otherwise, proceed to step 304.

[0163] Step 304: Perform image compression processing according to the preset image compression strategy (that is, compress the first image into the target image; for details, please refer to the aforementioned compression method 100).

[0164] Step 305: Write the isModifiedByTPImageHandler field into the metadata block of the processed image (i.e., write the indicator field into the metadata of the target image).

[0165] Step 306, image processing complete.

[0166] Specifically, compression method 300 mainly describes the image processing uniqueness strategy provided in the embodiments of this application. Extensive image testing has verified that if the above image processing strategy processes the same image multiple times, it will cause a continuous decline in the image's visual quality, ultimately leading to the image failing the UI department's review. Therefore, secondary processing of already processed images should be avoided during image processing. To achieve the above functionality, the image processing flow involves processing image metadata (i.e., data stored in the image describing its characteristics). Before processing the image, the data in the image metadata block is obtained, its contents are traversed, and it is determined whether it contains the isModifiedByTPImageHandler field. If it does, it means the image has already been processed and does not need further processing; the next image is then processed. If it does not contain the isModifiedByTPImageHandler field, it means the image has not been processed and is processed according to the above compression strategy. After processing, the isModifiedByTPImageHandler field needs to be written into the image's metadata block.

[0167] Figure 6 This is a flowchart of the image compression method 400 provided in an embodiment of this application. For example... Figure 6 As shown, compression method 400 includes steps 401-407.

[0168] Step 401: Begin obtaining the image path (e.g., a relative path);

[0169] Step 402: Determine whether the startup method is full or incremental. If it is full, proceed to step 403.

[0170] Step 403: Search for folders starting with "drawable" in the initial project directory (filtering the contents of the "build" folder).

[0171] Step 404: Traverse the files in the folder and obtain the paths of JPG, PNG, and GIF format files.

[0172] Step 405: If the startup method is determined to be incremental, obtain the paths of all files in this commit through a third-party library version control tool.

[0173] Step 406: Traverse all file paths and obtain the paths of JPG, PNG, and GIF format files.

[0174] Specifically, compression method 400 mainly describes the strategy for quickly querying the path of an image to be processed provided in the embodiments of this application. According to the requirements of image processing, the strategy for quickly querying the path of an image to be processed can be divided into two types: full-processing and incremental-processing. Full-processing is mainly for cases where all images in the target Android project have not undergone any processing, requiring processing of all images contained in the project at once; incremental-processing is mainly for cases where the images in the target Android project have already undergone full processing once, requiring processing of subsequently added or replaced images.

[0175] The full processing flow is as follows: First, recursively traverse the initial directory of the project to obtain the paths of folders in the project that begin with "drawable" (excluding folders under the "build" folder that meet the criteria during the recursion). Second, sequentially traverse the contents of each file in the target folder, determining whether a file is an image by its file extension; the paths of the obtained jpg, png, and gif format image resources are the paths of the images to be processed.

[0176] The incremental processing flow is as follows: Obtain the paths of all files involved in this code commit through a third-party code version control tool, then traverse these file paths, determine whether the file is an image by the file extension, and the paths of the obtained jpg, png, and gif format image resources are the paths of the images to be processed.

[0177] Figure 7 This is a flowchart of the image compression method 500 provided in an embodiment of this application. For example... Figure 7 As shown, compression method 500 includes steps 501-514.

[0178] Step 501: Begin submitting code to the remote repository.

[0179] Step 502, Git pre-commit is triggered, the contents of the resultcode file are cleared and a Gradle task named tpimagehandler is run.

[0180] Step 503, TPImageHandler starts.

[0181] Step 504: Obtain a list of image files to be processed.

[0182] Step 505: Are there any images that need to be processed? If so, proceed to step 506.

[0183] Step 506: Begin compressing the image.

[0184] Step 507: Determine if an error occurred during image processing; otherwise, proceed to step 508.

[0185] Step 508: Write 0 to the Resultcode.txt file.

[0186] Step 509: Write the relative path of the image into globalProcessed.

[0187] Step 510: If an error is determined in step 507 during image processing, write 1 to the Resultcode.txt file.

[0188] Step 511: Determine if all images have been processed. If yes, proceed to step 512; otherwise, return to step 504.

[0189] Step 512: After adding Git, recommit, which returns you to step 502.

[0190] Step 513: If it is determined in step 505 that there are no images that need to be processed, then determine whether the content of the resultcode file is empty. If it is (i.e., it does not contain "1"), then proceed to step 514; otherwise, return to step 512.

[0191] Step 514: Incremental image processing successful. Commute allowed.

[0192] Specifically, compression method 500 mainly illustrates the absolute image processing strategy provided in the embodiments of this application. Since the remote repository for code version management stores the final version of the project code, when synchronizing code to the remote repository through version management tools, it should be ensured that all images in this commit have been processed, and unprocessed images are not allowed to be synchronized to the remote repository. Therefore, the absoluteness of image processing should be guaranteed.

[0193] The absolute processing of images is achieved through hook functions in the code version control tool. By writing relevant hook scripts to intercept code submission requests, the image processing task is triggered after the Resultcode.txt file is cleared. After execution, the task first checks if any images requiring processing exist in the current submission using the image path retrieval strategy described above. If not, the Resultcode.txt file is empty, and the submission is allowed, indicating success. If images exist, they are processed according to the image compression strategy described above. During processing, if no errors occur, a 0 is written to the ResultCode.txt file after each image processing step, indicating successful processing. If an error occurs, a 1 is written to the ResultCode.txt file, indicating failure. The script then displays different prompts based on the ResultCode, including "Image processing successful, please add and resubmit" and "Image processing failed, please reprocess." Finally, regardless of whether an image is successfully processed, it must be added and resubmitted, and the above process is repeated until all images have been successfully processed.

[0194] The above text combined Figures 1 to 7 The image compression method provided in the embodiments of this application is described in detail below, in conjunction with... Figure 8 , Figure 9 This application describes in detail the image compression apparatus according to embodiments of the present application. It should be understood that... Figure 8 , Figure 9 The compression device shown can achieve Figures 1-7 The steps shown in the method flow are one or more. To avoid repetition, they will not be described in detail here.

[0195] Figure 8 This is a schematic block diagram of the image compression device 800 provided in the embodiments of this application. Figure 8 As shown, the compression device 800 includes a determining unit 810, a compression unit 820, a storage unit 830, a writing unit 840, and an acquisition unit 850.

[0196] The determining unit 810 is used to determine a first compression strategy from multiple preset image compression strategies based on the image format of the first image.

[0197] Compression unit 820 is used to compress the first image using the first compression strategy to obtain the target image;

[0198] The storage unit 830 is used to save the target image when the similarity value between the target image and the first image is greater than or equal to a preset first threshold.

[0199] In one possible design, the compression unit 820 is specifically used for:

[0200] The first image is compressed using a preset first sub-strategy to obtain an intermediate image;

[0201] If the similarity value between the intermediate image and the first image is greater than or equal to the first threshold, the intermediate image is compressed using a preset second sub-strategy to obtain the target image.

[0202] In one possible design, the storage unit 830 is further used for:

[0203] If the similarity value between the target image and the first image is less than the first threshold, then the intermediate image is saved.

[0204] In one possible design, the storage unit 830 is further used for:

[0205] If the similarity value between the target image and the first image is less than the first threshold, then the first image is saved.

[0206] In one possible design, the determining unit 810 is further configured to:

[0207] It is determined that the metadata of the first image does not contain an indication field, which is used to indicate that the image has been compressed.

[0208] The compression device further includes:

[0209] The writing unit 840 is used to write the indication field into the metadata of the target image.

[0210] In one possible design, the compression device further includes:

[0211] The acquisition unit 850 is used to traverse the contents of all files to be submitted in order to obtain the path of at least one image to be processed, wherein the at least one image to be processed includes the first image.

[0212] In one possible design, the determining unit 810 is specifically used for:

[0213] The first compression strategy is determined based on the image format of the first image and the information of the first image's alpha channel.

[0214] In one possible design, the first compression strategy includes at least one of performing image bit compression, removing image metadata, and converting the image format.

[0215] Specifically, the compression device 800 may correspond to the electronic device in the compression method 100-compression method 500 provided according to the embodiments of this application, or a chip disposed in the electronic device. The compression device 800 may include a chip for performing compression. Figures 1-7 Each unit of the method executed by the electronic device. Furthermore, each unit in the compression device 800 and the other operations and / or functions described above are respectively for implementing the corresponding processes of compression method 100-compression method 500. The specific process of each unit executing the above-mentioned corresponding steps has been described in detail in compression method 100-compression method 500, and will not be repeated here for the sake of brevity.

[0216] This application embodiment also provides an image compression device 900. Figure 9 This is a structural block diagram of the compression device 900 provided in an embodiment of this application. Figure 9 As shown, the compression device 900 includes a processor 910 and a memory 920, and the above-mentioned devices can be connected through one or more buses 930.

[0217] The compression device 900 also includes a computer program 921, which is stored in a memory 920. When the computer program 921 is executed by the processor 910, the compression device 900 performs the aforementioned operations. Figures 1-7 Compression methods 100-500 are shown. All relevant details regarding each step in the above method embodiments can be found in the functional descriptions of the corresponding physical devices, and will not be repeated here.

[0218] This application also provides a computer-readable storage medium storing a computer program. When the computer program is run on an electronic device, it causes the electronic device to perform the above-described actions. Figures 1-7 Compression methods 100 to 500 are shown.

[0219] This application also provides a computer program product, including: computer program code, which, when executed on an electronic device, causes the electronic device to perform the above-described... Figures 1-7 Compression methods 100 to 500 are shown.

[0220] This application also provides a chip, including: a processor, configured to retrieve and run a computer program from a memory, causing an electronic device equipped with the chip to perform the above-described actions. Figures 1-7 Compression methods 100 to 500 are shown.

[0221] From the above description of the embodiments, those skilled in the art will understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0222] It should be understood that the apparatuses and processes disclosed in the several embodiments provided in this application can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of modules or units is only a logical functional division. In actual implementation, there may be other division methods, such as multiple units or components being combined or integrated into another device. In addition, some features may be omitted or not performed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces, and the indirect coupling or communication connection of devices or units may be electrical, mechanical, or other forms.

[0223] The units described as separate components may or may not be physically separate. A component shown as a unit can be one or more physical units. That is, it can be located in one place or distributed in multiple different locations. Depending on the actual needs, some or all of the units can be selected to achieve the purpose of this solution.

[0224] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit; they can also exist physically separately; or some units can be integrated into one unit while others exist physically separately. The integrated units described above can be implemented in hardware or as software functional units.

[0225] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, all or part of the technical solutions of the embodiments of this application can be embodied in the form of a software product. This software product is stored in a storage medium. The software product includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.

[0226] It should be noted that all or part of the above embodiments provided in this application (e.g., part or all of any feature) can be arbitrarily combined or combined with each other.

[0227] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for compressing images, characterized in that, include: The first compression strategy is determined from multiple preset image compression strategies based on the image format of the first image. The first image is one of at least one image to be processed; The first image is compressed using the first compression strategy to obtain the target image; If the similarity value between the target image and the first image is greater than or equal to a preset first threshold, the target image is saved; wherein, the greater the similarity value between the two images, the closer their visual quality is. The compression process of the first image using the first compression strategy includes: The first image is compressed using a preset first sub-strategy to obtain an intermediate image; If the similarity value between the intermediate image and the first image is greater than or equal to the first threshold, the intermediate image is compressed using a preset second sub-strategy to obtain the target image. The compression method further includes: If the similarity value between the target image and the first image is less than the first threshold, then the intermediate image is saved.

2. The compression method according to claim 1, characterized in that, Before determining the first compression strategy from multiple preset image compression strategies based on the image format of the first image, the compression method further includes: It is determined that the metadata of the first image does not contain an indication field, which is used to indicate that the image has been compressed. Before saving the target image, the compression method further includes: Write the indication field into the metadata of the target image.

3. The compression method according to claim 1, characterized in that, Before determining the first compression strategy from multiple preset image compression strategies based on the image format of the first image, the compression method further includes: Iterate through the contents of all files to be submitted to obtain the path of at least one image to be processed, wherein the at least one image to be processed includes the first image.

4. The compression method according to claim 1, characterized in that, The step of determining the first compression strategy from multiple preset image compression strategies based on the image format of the first image includes: The first compression strategy is determined based on the image format of the first image and the information of the first image's alpha channel.

5. The compression method according to claim 1, characterized in that, The first compression strategy includes at least one of image bit compression, removal of image metadata, and conversion of image format.

6. An image compression device, characterized in that, include: The determining unit is used to determine a first compression strategy from multiple preset image compression strategies based on the image format of the first image. The first image is one of at least one image to be processed; A compression unit is used to compress the first image using the first compression strategy to obtain a target image; A storage unit is used to store the target image when the similarity value between the target image and the first image is greater than or equal to a preset first threshold; wherein, the greater the similarity value between the two images, the closer their visual quality is. The compression unit is specifically used for: The first image is compressed using a preset first sub-strategy to obtain an intermediate image; If the similarity value between the intermediate image and the first image is greater than or equal to the first threshold, the intermediate image is compressed using a preset second sub-strategy to obtain the target image. The storage unit is also used for: If the similarity value between the target image and the first image is less than the first threshold, then the intermediate image is saved.

7. An image compression device, characterized in that, include: processor; Memory; And a computer program, wherein the computer program is stored in the memory, and when the computer program is executed by the processor, causes the processor to perform the compression method as described in any one of claims 1 to 5.