简体   繁体   English

在 Python 中将目录中的所有文件(.jpg 到 .png)转换

[英]Converting all files (.jpg to .png) from a directory in Python

I'm trying to convert all files from a directory from .jpg to .png.我正在尝试将目录中的所有文件从 .jpg 转换为 .png。 The name should remain the same, just the format would change.名称应该保持不变,只是格式会改变。

I've been doing some researches and came to this:我一直在做一些研究并得出以下结论:

from PIL import Image
import os

directory = r'D:\PATH'

for filename in os.listdir(directory):
    if filename.endswith(".jpg"):
        im = Image.open(filename)
        im.save('img11.png')
        print(os.path.join(directory, filename))
        continue
    else:
        continue

I was expecting the loop to go through all my .jpg files and convert them to .png files.我期待循环遍历我所有的 .jpg 文件并将它们转换为 .png 文件。 So far I was doing only with 1 name: 'img11.png', I haven't succed to build something able to write the adequate names.到目前为止,我只使用了 1 个名称:'img11.png',我还没有成功构建出能够写出足够名称的东西。

The print(os.path.join(directory, filename)) works, it prints all my files but concerning the converting part, it only works for 1 file. print(os.path.join(directory, filename))工作,它打印我所有的文件,但关于转换部分,它只适用于 1 个文件。

Do you guys have any idea for helping me going through the process?你们有什么想法可以帮助我完成整个过程吗?

You can convert the opened image as RGB and then you can save it in any format.您可以将打开的图像转换为 RGB,然后您可以将其保存为任何格式。 You can try the following code :您可以尝试以下代码:

from PIL import Image
import os

directory = r'D:\PATH'
c=1
for filename in os.listdir(directory):
    if filename.endswith(".jpg"):
        im = Image.open(filename)
        name='img'+str(c)+'.png'
        rgb_im = im.convert('RGB')
        rgb_im.save(name)
        c+=1
        print(os.path.join(directory, filename))
        continue
    else:
        continue

You're explicitly saving every file as img11.png .您明确地将每个文件保存为img11.png You should get the name of your jpg file and then use that to name and save the png file.您应该获取jpg文件的名称,然后使用它来命名和保存png文件。

name = filename[:-4]
im.save(name + '.png')

I would have used os.rename() function like below.我会使用 os.rename() 函数,如下所示。

import os 
directory = r'D:\PATH' 
for filename in os.listdir(directory):
    prefix = filename.split(".jpg")[0]
    os.rename(filename, prefix+".png")

Please let me know if this is what you wanted.请让我知道这是否是您想要的。 Try the code with some copied images inside a test folder, before applying to the intended folder.在应用到预期文件夹之前,尝试在测试文件夹中复制一些图像的代码。 All the best.祝一切顺利。

from PIL import Image 
import os directory = r'D:\PATH' 
for filename in os.listdir(directory): 
    if filename.endswith(".jpg"): 
        prefix = filename.split(".jpg")[0]
        im = Image.open(filename)
        im.save(prefix+'.png')  
    else: 
        continue

Please try this one and let me know.请试试这个,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:将文件夹中的所有文件转换为 JPG,但应仅考虑 JPG 和 PNG - Python: Converting all files in a folder to JPG, but only JPG and PNG should be considered 将多个文件从JPG转换为PNG - Converting multiple files from JPG to PNG 将 Tiff 文件转换为 JPG 或 Png python - Converting Tiff file to JPG or Png python 在Python中,有没有一种方法可以从** Google图片**搜索结果中下载全部/部分图片文件(例如JPG / PNG)? - In Python, is there a way I can download all/some the image files (e.g. JPG/PNG) from a **Google Images** search result? 在 Google Colab 中将所有 jpg 文件转换为 png 文件 - Convert all jpg to png files in Google Colab 尝试将目录中的图像从 JPG 转换为 PNG 文件时出现“FileNotFoundError: [Errno 2] 没有这样的文件或目录” - 'FileNotFoundError: [Errno 2] No such file or directory' when trying to convert images in directory from JPG to PNG files 将 .jpg 图像转换为 .png - Converting .jpg images to .png 复制目录中的所有文件和文件夹,除了 python 中的 .jpg 和 .png 文件 - copy all files and folder inside a dir except .jpg and .png files in python 将 PNG 图像从文件夹转换为 JPG - Python - Convert PNG images from folder to JPG - Python 将基于图像的 pdf 转换为 python 中的图像文件 (png/jpg) - converting image-based pdf to image file (png/jpg) in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM