Download picture from URL--java and Python implementation method comparison

Source: Internet
Author: User

Download picture from URL--java and Python implementation method comparison Blog Category:
    • A little bit of technical notes
Javapython Picture Download

First, the implementation of Java method

    1. First read the picture
Java code
    1. Method One: Directly read the picture according to the URL
    2. Private static BufferedImage read (String imageUrl) throws IOException {
    3. URL url = new URL (IMAGEURL);
    4. BufferedImage image = Imageio.read (URL);
    5. return image;
    6. }
Java code
  1. If you need to set some of the properties in connection, such as Requestproperty, then use mode two:
  2. Private static BufferedImage read (String imageUrl) throws IOException {
  3. URL url = new URL (IMAGEURL);
  4. HttpURLConnection conn = (httpurlconnection) url.openconnection ();
  5. //conn.setrequestproperty ("Referer", Referer);
  6. Conn.setrequestmethod ("GET");
  7. BufferedImage image = Imageio.read (Conn.getinputstream ());
  8. return image;
  9. }

2. Then save the picture

Java code
  1. Private static void Save (bufferedimage image, String destimageurl)
  2. throws IOException {
  3. File ImageFile = new File (Destimageurl);
  4. FileOutputStream OutStream = new FileOutputStream (ImageFile);
  5. Imageio.write (image, "jpg", outstream);
  6. Imageio.write (image, "gif", OutStream);
  7. Imageio.write (image, "png", OutStream);
  8. Imageio.write (image, "JPEG", outstream);
  9. }

3. How to get the type of picture (jpg, Jpeg, GIF, PNG, etc.)

Java code
  1. Public static String get (byte[] imagebt) throws IOException {
  2. String type = "";
  3. Memorycacheimageinputstream MCIS = null;
  4. try {
  5. MCIS = New Memorycacheimageinputstream (new Bytearrayinputstream (
  6. IMAGEBT));
  7. Iterator<imagereader> it = imageio.getimagereaders (MCIS);
  8. While (It.hasnext ()) {
  9. ImageReader reader = (imagereader) it.next ();
  10. return Reader.getformatname ();
  11. }
  12. } finally {
  13. if (MCIS! = null) {
  14. try {
  15. Mcis.close ();
  16. } catch (IOException IoE) {
  17. }
  18. }
  19. }
  20. return type;
  21. }

Ii. how Python is implemented

    1. One line of code for picture reading and saving
Python code
    1. From Urllib import Urlretrieve
    2. Urlretrieve (' http://img.chetx.com/chetxbbs/2005_04/09/1113039370066.gif ', ' e://mypicture.gif ')

2. One line of code to get the picture type

Python code
    1. Import IMGHDR
    2. Imghdr.what (' MyPicture.gif ')

Iii. Summary

Compared to the implementation of Java,python is really too concise and clear

Download picture from URL--java and Python implementation method comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.