{"id":54788,"date":"2020-01-04T00:00:00","date_gmt":"2020-01-03T21:00:00","guid":{"rendered":"https:\/\/prohoster.info\/blog\/blog_prohoster\/jpeg-algoritm-szhatiya"},"modified":"2020-02-18T14:02:50","modified_gmt":"2020-02-18T11:02:50","slug":"jpeg-algoritm-szhatiya","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/jpeg-algoritm-szhatiya","title":{"rendered":"JPEG. Compression algorithm","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Hello again! I found this article written back in May 2019. This is a continuation of a series of articles about WAVE and JPEG. Here <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/450774\/\">One<\/a><\/noindex>. This post will include information about the image encoding algorithm and the format itself in general.<\/p>\n<p><\/p>\n<h3 id=\"schepotku-istorii\">A pinch of history<\/h3>\n<p><\/p>\n<p>A tablespoon of Wikipedia:<\/p>\n<p><\/p>\n<blockquote><p>JPEG (Joint Photographic Experts Group) is one of the popular raster graphic formats used for storing photographs and similar images.<\/p><\/blockquote>\n<p>This standard was developed by the Joint Photographic Experts Group back in 1991 for effective image compression.<\/p>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h3 id=\"kakoy-put-prohodyat-izobrazheniya-ot-syrogo-vida-do-jpeg\">What journey do images take from raw to JPEG<\/h3>\n<p><\/p>\n<p>Some consider JPEG images to be raw data compressed using Huffman coding, but that's not true. Before the final compression, the data undergoes a long process.<\/p>\n<p><\/p>\n<p>First, the color model is converted from RGB to YCbCr. There is even a special algorithm for this\u2014 <noindex><a rel=\"nofollow\" href=\"https:\/\/ru.wikipedia.org\/wiki\/YCbCr#%D0%9F%D1%80%D0%B5%D0%BE%D0%B1%D1%80%D0%B0%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F_%D0%BF%D0%BE_%D1%80%D0%B5%D0%BA%D0%BE%D0%BC%D0%B5%D0%BD%D0%B4%D0%B0%D1%86%D0%B8%D0%B8_ITU-R_BT.601\">here<\/a><\/noindex>. The Y component remains unchanged because it represents brightness, and altering it would be noticeable.<\/p>\n<p><\/p>\n<p>The first step taken with the image is <strong>\u00abdecimation\u00bb<\/strong> (subsampling). This is simple to understand: a 2x2 array of pixels is taken, and then the Cb and Cr components are averaged from each of the YCbCr components of these 4 pixels. Thus, <em>we save 6 bytes<\/em>, instead of 4 Y, 4 Cb, 4 Cr we got 4 Y and the same for each Cb and Cr (4 + 4 + 4 = 12; 4 + 1 + 1 = 6; 12 \u2014 6 = 6). In scales even 2\u00d72, lossy compression with a compression ratio of 2:1 sounds solid. This applies to the entire image. Thus, we have halved the size. This technique can be used thanks to our color perception. A person easily notices differences in brightness, but not in color if it is averaged in a small block of pixels. Decimation can also be performed in lines, 4 pixels horizontally and vertically. The first option is used more often. If image quality is important, then decimation is not performed at all.<br \/>\nA visual illustration of subsampling (Habr didn't allow inserting a GIF) \u2014 <noindex><a rel=\"nofollow\" href=\"https:\/\/i.ibb.co\/Rg5Th9H\/150953010617579181.gif\">https:\/\/i.ibb.co\/Rg5Th9H\/150953010617579181.gif<\/a><\/noindex><\/p>\n<p><\/p>\n<h5 id=\"osnovnaya-chast-podgotovki\">The main part of the preparation<\/h5>\n<p>\nDCT<\/p>\n<p>Now comes the most complex and necessary part. The entire image is divided into blocks of 8\u00d78 (padding is used if the resolution is not a multiple of the block's side).<\/p>\n<p><\/p>\n<p>Now the <strong>DCT (Discrete Cosine Transform) is applied to each block.<\/strong>. In this part, all unnecessary elements are removed from the image. Using DCT, it must be understood whether this block (8\u00d78) describes any monotone part of the image: the sky, walls; or it contains complex structure (hair, symbols, etc.). Logically, 64 similar color pixels can be described by just 1 since the block size is already known. That\u2019s compression: 64 to 1.<\/p>\n<p><\/p>\n<p>DCT transforms the block into a spectrum, and where the readings change sharply, the coefficient becomes positive, and the sharper the transition, the higher the output will be. Where the coefficient is higher, the image depicts clear transitions in color and brightness, while where it is lower, the changes in the YCbCr component values within the block are weak (smooth).<\/p>\n<p>\nQuantization<\/p>\n<p>Here, compression settings are applied. Each of the coefficients in each of the 8\u00d78 matrices is divided by a certain number. If you are not going to reduce the image quality after all its modifications, the divisor should be one. If the memory occupied by this photograph is more important to you, then the divisor will be greater than 1, and the quotient is rounded. As a result, many zeros are often obtained after rounding.<\/p>\n<p><\/p>\n<p>Quantization is done to create the possibility of even greater compression. Here\u2019s how it looks with the example of quantization of the graph y = sin(x):<\/p>\n<p><\/p>\n<p><img decoding=\"async\" alt=\"JPEG. Compression algorithm\" src=\"\/wp-content\/uploads\/2020\/01\/ab6c205c62133785b19b5f42db109fa6.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p>\nCompression<\/p>\n<p>First, we traverse the matrix in a zig-zag manner:<\/p>\n<p><\/p>\n<p><img decoding=\"async\" alt=\"JPEG. Compression algorithm\" src=\"\/wp-content\/uploads\/2020\/01\/576a5e65431d38742efe42ead0e3863b.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p><\/p>\n<p>We obtain a one-dimensional array of numbers. We see that there are many zeros in it, which can be removed. To do this, instead of a sequence of numerous zeros, we write 1 zero and then a number indicating their count in the sequence. This way, we can reduce the total size of the array to 1\/3. After that, we simply compress this array using Huffman coding and write it into the file.<\/p>\n<p><\/p>\n<h3 id=\"gde-ispolzuetsya\">Where Used<\/h3>\n<p><\/p>\n<p>Everywhere. Like PNG, JPEG is used in cameras, OS\u2019s (as company logos, application icons, thumbnails) and in all possible areas where it is necessary to efficiently store images.<\/p>\n<p><\/p>\n<h3 id=\"vyvod\">Output<\/h3>\n<p><\/p>\n<p>Currently, knowledge about JPEG is only valuable for educational purposes, as it is already built-in and optimized by large groups of people, yet the core of science remains appealing.<\/p>\n<p>\nfile \u2014 continuous reading of events from one or more local files;<\/p>\n<p><noindex><a rel=\"nofollow\" href=\"https:\/\/ru.wikipedia.org\/wiki\/YCbCr\">Article about YCbCr on Wikipedia<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/ru.wikipedia.org\/wiki\/JPEG\">Article about JPEG on Wikipedia<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/pikabu.ru\/story\/kak_ustroen_format_jpeg_5452388\">A bit about DCT from a post on Pikabu<\/a><\/noindex><br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%94%D0%B8%D1%81%D0%BA%D1%80%D0%B5%D1%82%D0%BD%D0%BE%D0%B5_%D0%BA%D0%BE%D1%81%D0%B8%D0%BD%D1%83%D1%81%D0%BD%D0%BE%D0%B5_%D0%BF%D1%80%D0%B5%D0%BE%D0%B1%D1%80%D0%B0%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5\">Article about DCT on Wikipedia<\/a><\/noindex><\/p>\n<p>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/482728\/\">habr.com<\/a><\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u0418 \u0441\u043d\u043e\u0432\u0430 \u0437\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435! \u042f \u043d\u0430\u0448\u0435\u043b \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u0435\u0449\u0435 \u043c\u0430\u0435 2019-\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u042d\u0442\u043e \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043e WAVE \u0438 JPEG, \u0412\u043e\u0442 \u043f\u0435\u0440\u0432\u0430\u044f. \u042d\u0442\u0430 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u0432\u043a\u043b\u044e\u0447\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438 \u043e \u0441\u0430\u043c\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0432 \u0446\u0435\u043b\u043e\u043c. \u0429\u0435\u043f\u043e\u0442\u043a\u0443 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u0421\u0442\u043e\u043b\u043e\u0432\u0443\u044e \u043b\u043e\u0436\u043a\u0443 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438\u0437 \u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438: JPEG (Joint Photographic Experts Group) \u2014 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-54788","post","type-post","status-publish","format-standard","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u0418 \u0441\u043d\u043e\u0432\u0430 \u0437\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435! \u042f \u043d\u0430\u0448\u0435\u043b \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u0435\u0449\u0435 \u043c\u0430\u0435 2019-\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u042d\u0442\u043e \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043e WAVE \u0438 JPEG, \u0412\u043e\u0442 \u043f\u0435\u0440\u0432\u0430\u044f.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/jpeg-algoritm-szhatiya\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47JPEG. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u0436\u0430\u0442\u0438\u044f | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u0418 \u0441\u043d\u043e\u0432\u0430 \u0437\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435! \u042f \u043d\u0430\u0448\u0435\u043b \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u0435\u0449\u0435 \u043c\u0430\u0435 2019-\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u042d\u0442\u043e \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043e WAVE \u0438 JPEG, \u0412\u043e\u0442 \u043f\u0435\u0440\u0432\u0430\u044f.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/jpeg-algoritm-szhatiya\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-01-03T21:00:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-02-18T11:02:50+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47JPEG. Compression Algorithm | ProHoster","description":"Hello again! I found this article written back in May 2019. This is a continuation of a series of articles about WAVE and JPEG. Here\u2019s the first one.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/jpeg-algoritm-szhatiya","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47JPEG. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u0436\u0430\u0442\u0438\u044f | ProHoster","og:description":"\u0418 \u0441\u043d\u043e\u0432\u0430 \u0437\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435! \u042f \u043d\u0430\u0448\u0435\u043b \u044d\u0442\u0443 \u0441\u0442\u0430\u0442\u044c\u044e, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u0435\u0449\u0435 \u043c\u0430\u0435 2019-\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u042d\u0442\u043e \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043e WAVE \u0438 JPEG, \u0412\u043e\u0442 \u043f\u0435\u0440\u0432\u0430\u044f.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/jpeg-algoritm-szhatiya","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-01-03T21:00:00+00:00","article:modified_time":"2020-02-18T11:02:50+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"54788","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":"2026-01-24 12:45:19","breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 10:01:54","updated":"2026-01-24 12:45:19","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/54788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=54788"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/54788\/revisions"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=54788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=54788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=54788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}