Python: Download Gambar dengan Resolusi Tertinggi dari Laman Web

Kali ini kita coba gunakan Python untuk mengunduh gambar dengan resolusi tertinggi dari laman web (web page) target.
import requests
from os.path  import basename
from bs4 import BeautifulSoup
from PIL import Image
import io
import matplotlib.pyplot as plt
import numpy as np

#URL target (disini ada beberapa gambar yang bisa di-download)
url = 'http://www.teknologi-bigdata.com/2019/08/buku-teknologi-big-data-edisi-revisi.html'
r = requests.get(url)
soup = BeautifulSoup(r.content)

max_res = 0
for link in soup.select('img[src^=http]'):
    
    lnk = link['src']
    img_bytes = requests.get(lnk).content
    img_file = Image.open(io.BytesIO(img_bytes))
    width, height = img_file.size
    resolution = width * height
    
    if max_res < resolution:
        max_res = resolution
        max_size = str(width) + 'x' + str(height)
        max_img_file = img_file

print('highest res = ' + str(max_res) + ', max size = ' + max_size)
max_img_file.save('featured_image.' + str(max_img_file.format))
print('featured_image saved')

#Tampilkan gambar yang sudah di-download dengan MatplotLib
plt.imshow(np.array(max_img_file))

highest res = 327680, max size = 512x640 featured_image saved

Berikut adalah gambar yang berhasil di-download dari URL target.



Comments

Popular posts from this blog

Apa itu Big Data : Menyimak Kembali Definisi Big Data, Jenis Teknologi Big Data, dan Manfaat Pemberdayaan Big Data

Apache Spark: Perangkat Lunak Analisis Terpadu untuk Big Data

Tutorial Python: Cara Mudah Web Scraping menggunakan Beautiful Soup

MapReduce: Besar dan Powerful, tapi Tidak Ribet

HBase: Hyper NoSQL Database

Validitas Rapid Test Covid 19 : Accuracy vs F1-Score, Pilih yang Mana?

Aplikasi iPhone : RETaS Read English Tanpa Kamus!

Big Data dan Rahasia Kejayaan Google

HDFS: Berawal dari Google untuk Big Data

Cara Sederhana Install Hadoop 2 mode Standalone pada Windows 7 dan Windows 10