淡淡凉 发表于 2023-8-17 18:59:47

如何让保存到本地的图片大小一样

爬取王者荣耀的图片是保存到本地,图片大小不统一,如何让保存到本地的图片分辨率一样展示同样大小

import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get("https://pvp.qq.com/web201605/herolist.shtml")
sleep(1) # 等待js加载
source = driver.page_source
driver.close()

#html = requests.get("https://pvp.qq.com/web201605/herolist.shtml").content
doc = PyQuery(source)

#print(doc)
items = doc('.herolist > li').items()
href_datas = {}
for item in items:
    url = item.find('img').attr('src')
    urls = 'https:' + url

    name = item.find('a').text()
    href = item.find('a').attr('href')
    # href_lists.append(href)
    href_datas = "https://pvp.qq.com/web201605/" + href
   
    # os.path.exists(path) 判断文件是否存在 固定语法
    path = f"E:/Pythonfile/王者荣耀英雄图片"
    isexists = os.path.exists(path)
    if not isexists:
      os.makedirs(path)
    url_content = requests.get(urls).content
    print(f"正在爬取{name}图片中...",urls)
    with open("./王者荣耀英雄图片/" + name + '.jpg','wb') as file:
      file.write(url_content)
   
for name,pf_url in href_datas.items():
    print(name,pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.close()
    # html = requests.get(url).content
    doc = PyQuery(pf_html)

    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()
    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url
      #print(pf_urls)
      pf_name = pf_item.find('p').text()
      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)
      # os.path.exists(path) 判断文件是否存在 固定语法
      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)
      with open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg', 'wb') as file:
            file.write(url_content)

isdkz 发表于 2023-8-17 19:00:17

在保存到本地的图片上,如果想要它们的分辨率和大小都一样,可以使用PIL库进行处理。以下是修改后的代码:

import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from time import sleep
from PIL import Image

driver = webdriver.Chrome()
driver.get("https://pvp.qq.com/web201605/herolist.shtml")
sleep(1) # 等待js加载
source = driver.page_source
driver.close()

doc = PyQuery(source)

items = doc('.herolist > li').items()
href_datas = {}
for item in items:
    url = item.find('img').attr('src')
    urls = 'https:' + url

    name = item.find('a').text()
    href = item.find('a').attr('href')
    href_datas = "https://pvp.qq.com/web201605/" + href

    path = f"E:/Pythonfile/王者荣耀英雄图片"
    isexists = os.path.exists(path)
    if not isexists:
      os.makedirs(path)
    url_content = requests.get(urls).content
    print(f"正在爬取{name}图片中...",urls)
    with open("./王者荣耀英雄图片/" + name + '.jpg','wb') as file:
      file.write(url_content)
      # 打开图片
      img = Image.open(file.name)
      # 统一图片大小
      img = img.resize((800, 600))
      # 保存修改后的图片
      img.save(file.name)

for name,pf_url in href_datas.items():
    print(name,pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.close()

    doc = PyQuery(pf_html)

    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()
    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url

      pf_name = pf_item.find('p').text()
      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)

      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)
      with open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg', 'wb') as file:
            file.write(url_content)
            # 打开图片
            img = Image.open(file.name)
            # 统一图片大小
            img = img.resize((800, 600))
            # 保存修改后的图片
            img.save(file.name)


在这个修改后的代码中,我们使用了PIL库的Image模块来打开、修改和保存图片。通过调用 resize 方法,我们将图片的大小统一设置为了800x600。这样,保存到本地的图片就会展示出同样的大小。

学习编程中的Ben 发表于 2023-8-17 19:00:48

要使保存到本地的图片分辨率一样,可以使用PIL库来处理图片。以下是修改后的完整代码:

import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from time import sleep
from PIL import Image

driver = webdriver.Chrome()
driver.get("https://pvp.qq.com/web201605/herolist.shtml")
sleep(1)# 等待js加载
source = driver.page_source
driver.close()

doc = PyQuery(source)
items = doc('.herolist > li').items()
href_datas = {}

for item in items:
    url = item.find('img').attr('src')
    urls = 'https:' + url
    name = item.find('a').text()
    href = item.find('a').attr('href')
    href_datas = "https://pvp.qq.com/web201605/" + href

path = f"E:/Pythonfile/王者荣耀英雄图片"
isexists = os.path.exists(path)
if not isexists:
    os.makedirs(path)

for name, pf_url in href_datas.items():
    print(name, pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.close()

    doc = PyQuery(pf_html)
    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()

    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url
      pf_name = pf_item.find('p').text()

      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)

      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)

      with open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg', 'wb') as file:
            file.write(url_content)

      # 打开图片并调整大小
      img = Image.open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg')
      img = img.resize((500, 500))# 设置统一的图片大小,修改为你想要的大小
      img.save("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg')



在上述代码中,使用了PIL库的Image类来打开图片,并使用resize()方法将图片调整为统一的大小(这里设置为500x500像素)。然后使用save()方法保存修改后的图片。

请确保已经安装了PIL库,可以使用以下命令来安装:

pip install pillow


希望能帮助到您!如果有任何其他问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!{:10_254:}{:10_254:}{:10_254:}

陶远航 发表于 2023-8-17 19:00:54

要让保存到本地的图片分辨率相同,你可以在保存图片之前对其进行统一的处理。可以使用第三方库PIL(Python Imaging Library)来实现图片的处理和调整分辨率。

下面是对代码的修改,在保存图片之前添加了调整分辨率的步骤:

import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from PIL import Image

# 省略部分代码...

for name, pf_url in href_datas.items():
    print(name, pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.quit()

    doc = PyQuery(pf_html)
    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()

    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url
      # print(pf_urls)
      pf_name = pf_item.find('p').text()
      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)

      # 调整分辨率为统一大小
      image = Image.open(BytesIO(url_content))
      image = image.resize((500, 500))# 设置统一的图片尺寸,这里假设设置为500x500
      url_content = image.tobytes()

      # os.path.exists(path) 判断文件是否存在 固定语法
      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)

      with open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg', 'wb') as file:
            file.write(url_content)


在以上代码中,使用PIL库的Image.open()方法打开图片,并使用resize()方法调整尺寸为统一大小(这里假设设置为500x500),然后将图片转换为字节流,最后保存到本地。

需要注意的是,在使用该代码之前,请确保已经安装了Pillow库(PIL的fork版本)通过pip install pillow进行安装。

希望这能帮到你!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案

淡淡凉 发表于 2023-8-18 14:27:50

isdkz 发表于 2023-8-17 19:00
在保存到本地的图片上,如果想要它们的分辨率和大小都一样,可以使用PIL库进行处理。以下是修改后的代码:
...

import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from time import sleep
from PIL import Image

driver = webdriver.Chrome()
driver.get("https://pvp.qq.com/web201605/herolist.shtml")
sleep(1) # 等待js加载
source = driver.page_source
driver.close()

#html = requests.get("https://pvp.qq.com/web201605/herolist.shtml").content
doc = PyQuery(source)

#print(doc)
items = doc('.herolist > li').items()
href_datas = {}
for item in items:
    url = item.find('img').attr('src')
    urls = 'https:' + url

    name = item.find('a').text()
    href = item.find('a').attr('href')
    # href_lists.append(href)
    href_datas = "https://pvp.qq.com/web201605/" + href
    '''
    # os.path.exists(path) 判断文件是否存在 固定语法
    path = f"E:/Pythonfile/王者荣耀英雄图片"
    isexists = os.path.exists(path)
    if not isexists:
      os.makedirs(path)
    url_content = requests.get(urls).content
    print(f"正在爬取{name}图片中...",urls)
    with open("./王者荣耀英雄图片/" + name + '.jpg','wb') as file:
      file.write(url_content)
    '''
for name,pf_url in href_datas.items():
    print(name,pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.close()
    # html = requests.get(url).content
    doc = PyQuery(pf_html)

    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()

    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url
      #print(pf_urls)
      pf_name = pf_item.find('p').text()
      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)

      # 调整分辨率为统一大小
      image = Image.open(BytesIO(url_content))
      image = image.resize((500, 500))# 设置统一的图片尺寸,这里假设设置为500x500
      url_content = image.tobytes()

      # os.path.exists(path) 判断文件是否存在 固定语法
      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)
      with open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg', 'wb') as file:
            file.write(url_content)

            # 打开图片
            img = Image.open(file.name)
            # 统一图片大小
            img = img.resize((800, 600))
            # 保存修改后的图片
            img.save(file.name)
D:\python39\python.exe E:/Pythonfile/herolist.py
亚连 https://pvp.qq.com/web201605/herodetail/514.shtml
正在爬取亚连_追忆之刃皮肤图片中... https://game.gtimg.cn/images/yxzj/img201606/heroimg/514/514-smallskin-1.jpg
Traceback (most recent call last):
File "E:\Pythonfile\herolist.py", line 76, in <module>
    img = Image.open(file.name)
File "D:\python39\lib\site-packages\PIL\Image.py", line 3023, in open
    raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file './王者荣耀皮肤图片/亚连_追忆之刃.jpg'


报错了

淡淡凉 发表于 2023-8-18 15:16:47

学习编程中的Ben 发表于 2023-8-17 19:00
要使保存到本地的图片分辨率一样,可以使用PIL库来处理图片。以下是修改后的完整代码:



import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from time import sleep
from PIL import Image

driver = webdriver.Chrome()
driver.get("https://pvp.qq.com/web201605/herolist.shtml")
sleep(1) # 等待js加载
source = driver.page_source
driver.close()

#html = requests.get("https://pvp.qq.com/web201605/herolist.shtml").content
doc = PyQuery(source)

#print(doc)
items = doc('.herolist > li').items()
href_datas = {}
for item in items:
    url = item.find('img').attr('src')
    urls = 'https:' + url

    name = item.find('a').text()
    href = item.find('a').attr('href')
    # href_lists.append(href)
    href_datas = "https://pvp.qq.com/web201605/" + href
    '''
    # os.path.exists(path) 判断文件是否存在 固定语法
    path = f"E:/Pythonfile/王者荣耀英雄图片"
    isexists = os.path.exists(path)
    if not isexists:
      os.makedirs(path)
    url_content = requests.get(urls).content
    print(f"正在爬取{name}图片中...",urls)
    with open("./王者荣耀英雄图片/" + name + '.jpg','wb') as file:
      file.write(url_content)
    '''
for name,pf_url in href_datas.items():
    print(name,pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.close()
    # html = requests.get(url).content
    doc = PyQuery(pf_html)

    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()

    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url
      #print(pf_urls)
      pf_name = pf_item.find('p').text()
      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)
      
      # os.path.exists(path) 判断文件是否存在 固定语法
      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)
      with open("./王者荣耀皮肤图片/" + name + '_' + pf_name + '.jpg', 'wb') as file:
            file.write(url_content)

      # 打开图片并调整大小
      img = Image.open(f"{pf_path}/" + name + '_' + pf_name + '.jpg')
      img = img.resize((500, 500))# 设置统一的图片大小,修改为你想要的大小
      img.save(f"{pf_path}/" + name + '_' + pf_name + '.jpg')
D:\python39\python.exe E:/Pythonfile/herolist.py
亚连 https://pvp.qq.com/web201605/herodetail/514.shtml
正在爬取亚连_追忆之刃皮肤图片中... https://game.gtimg.cn/images/yxzj/img201606/heroimg/514/514-smallskin-1.jpg
姬小满 https://pvp.qq.com/web201605/herodetail/564.shtml
正在爬取姬小满_武道奇才皮肤图片中... https://game.gtimg.cn/images/yxzj/img201606/heroimg/564/564-smallskin-1.jpg
正在爬取姬小满_零食大作战皮肤图片中... https://game.gtimg.cn/images/yxzj/img201606/heroimg/564/564-smallskin-2.jpg
Traceback (most recent call last):
File "D:\python39\lib\site-packages\PIL\JpegImagePlugin.py", line 639, in _save
    rawmode = RAWMODE
KeyError: 'RGBA'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "E:\Pythonfile\herolist.py", line 78, in <module>
    img = Image.open(f"{pf_path}/" + name + '_' + pf_name + '.jpg')
File "D:\python39\lib\site-packages\PIL\Image.py", line 2413, in save
    save_handler(self, fp, filename)
File "D:\python39\lib\site-packages\PIL\JpegImagePlugin.py", line 642, in _save
    raise OSError(msg) from e
OSError: cannot write mode RGBA as JPEG

前面的几张图片可以正常保存,后面的报错了OSError: cannot write mode RGBA as JPEG

学习编程中的Ben 发表于 2023-8-18 15:26:52

淡淡凉 发表于 2023-8-18 15:16
前面的几张图片可以正常保存,后面的报错了OSError: cannot write mode RGBA as JPEG

这个试试:
import requests
from pyquery import PyQuery
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from time import sleep
from PIL import Image

# 启动浏览器
driver = webdriver.Chrome()
driver.get("https://pvp.qq.com/web201605/herolist.shtml")
sleep(1) # 等待js加载
source = driver.page_source
driver.close()

# 解析英雄列表页面
doc = PyQuery(source)
items = doc('.herolist > li').items()
href_datas = {}
for item in items:
    url = item.find('img').attr('src')
    urls = 'https:' + url

    name = item.find('a').text()
    href = item.find('a').attr('href')
    href_datas = "https://pvp.qq.com/web201605/" + href

# 遍历英雄,爬取皮肤图片
for name, pf_url in href_datas.items():
    print(name, pf_url)
    driver = webdriver.Chrome()
    driver.get(pf_url)
    sleep(1)# 等待js加载
    pf_html = driver.page_source
    driver.close()
   
    # 解析英雄详情页面
    doc = PyQuery(pf_html)
    pf_items = doc('.pic-pf-list.pic-pf-list3>li').items()

    for pf_item in pf_items:
      pf_url = pf_item.find('img').attr('src')
      pf_urls = 'https:' + pf_url
      pf_name = pf_item.find('p').text()
      url_content = requests.get(pf_urls).content
      print(f"正在爬取{name}_{pf_name}皮肤图片中...", pf_urls)
      
      # 创建保存路径
      pf_path = f"E:/Pythonfile/王者荣耀皮肤图片"
      isexists = os.path.exists(pf_path)
      if not isexists:
            os.makedirs(pf_path)
      
      # 保存图片
      with open(f"{pf_path}/{name}_{pf_name}.jpg", 'wb') as file:
            file.write(url_content)

      # 打开图片并调整大小
      img = Image.open(f"{pf_path}/{name}_{pf_name}.jpg")
      img = img.convert("RGB")# 转换为RGB模式
      img = img.resize((500, 500))# 设置统一的图片大小,修改为你想要的大小
      img.save(f"{pf_path}/{name}_{pf_name}.jpg")

淡淡凉 发表于 2023-8-18 16:59:10

淡淡凉 发表于 2023-8-18 15:16
前面的几张图片可以正常保存,后面的报错了OSError: cannot write mode RGBA as JPEG

百度了一下,用convert方法把RGBA转化成RGB就可以了
# 打开图片并调整大小
img = Image.open(f"{pf_path}/" + name + '_' + pf_name + '.jpg')
img = img.convert('RGB')
img = img.resize((500, 500))# 设置统一的图片大小,修改为你想要的大小
img.save(f"{pf_path}/" + name + '_' + pf_name + '.jpg')
页: [1]
查看完整版本: 如何让保存到本地的图片大小一样