In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to identify the format of a picture". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In some cases, we get the binary data of a picture, but we don't know what format the picture should be. For example, a HTTP API returns a piece of Base64-encoded image data, as shown in the following figure:
This piece of Base64-encoded data actually corresponds to the following image:
So the question is, is this picture in JPG or PNG format? Is it BMP or GIF with only one frame?
There are also some websites whose URL format of pictures is similar to that of https://www.kingname.info/xx/yy/zz, which does not show the format of pictures in URL. So, when you download this picture with a crawler, how should you save it?
Although in most cases, you can save a picture in PNG format as JPG, you can also double-click to open it on your computer. But if you want to write some programs to deal with pictures, then the format of the pictures is very important. For example, there is frame information in GIF, but not in JPG, PNG images have channel information, and JPG does not. If you download a picture of JPG and try to extract frame information by processing GIF, it will obviously cause the program to report an error.
To solve this problem, you can use Pillow, a common image processing library. It can easily identify the format of a picture in a common format.
We can install Pillow using the following command:
Python3-m pip install pillow
After the installation is complete, we use PIL to import the image processing module Image:
From PIL import Image img = Image.open ('/ Users/kingname/Dropbox/50e452c3504a6.jpg') print (img.format)
The running effect is shown in the following figure:
Successfully recognize a JPG format as JPEG (JPG and JPEG are the same thing). But obviously, in most cases, the pictures are stored in memory in binary form, not on the hard disk. But the parameter that Image.open () receives is a file address. We need to have Pillow read the image data from memory, and after generating the picture object, look at its .format property.
At this point, you can use the io module to wrap the binary data into a fake binary file handle:
Import io import base64 from PIL import Image img_base64 = 'base64' img_byte = base64.b64decode (img_base64.encode ()) img_io = io.BytesIO (img_byte) img = Image.open (img_io) print (img.format)
The running effect is shown in the following figure:
In this way, we have successfully identified the format of an unknown picture.
That's all for the content of "how to identify the format of a picture". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.