[英]Python Web scrapping Hidden jpg images that I can't figure out how to download from this internet site
[英]I can not find out why my code is skipping some .JPG images, can you figure it out?
我有一個代碼可以將 .jpg 和 .jpeg 文件從一個文件夾轉換為 .png 文件。
非常簡單,但非常令人沮喪,因為它會跳過一些文件並且不會轉換它們。 我已經嘗試了一切,但似乎無法找到錯誤並依賴來自 stackoverflow 的人的幫助......
images_list = os.listdir(path_dir)
def Checker(directory, filename):
if filename.split(".")[-1] == "jpg":
print("File found and going trough:" + filename)
im1 = Image.open(directory + "/" + filename)
# Define the png_filename variable before using it
png_filename = filename.strip(".jpg") + '.png'
im1.save(directory + "/" + png_filename)
# Remove the original JPEG image from the directory
os.remove(directory + "/" + filename)
images_list.remove(filename)
elif filename.split(".")[-1] == "jpeg":
print("File found and going trough:" + filename)
im1 = Image.open(directory + "/" + filename)
# Define the png_filename variable before using it
png_filename = filename.strip(".jpeg") + '.png'
im1.save(directory + "/" + png_filename)
# Remove the original JPEG image from the directory
os.remove(directory + "/" + filename)
images_list.remove(filename)
for item in images_list:
Checker(path_dir, item)
你不覺得在這里使用 glob 更好嗎? 搜索所有擴展名為.jpg 或.jpeg 的文件並更改擴展名。
import glob
from PIL import Image
for file in glob.glob('./images/*.jpg')+glob.glob('./images/*.jpeg'):
Image.open(file).convert('RGB').save(file.rstrip('.jpeg')+'.png', "PNG")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.