简体   繁体   English

如何在调整图像大小并将其保存到另一个文件夹时保留图像的文件名?

[英]How do I keep the file name of an image when resizing it and saving it to another folder?

I'm in an introductory neural networking class so mind my ignorance.我在介绍神经网络 class 所以请注意我的无知。 Also my first SO post.也是我的第一个 SO 帖子。

I'm trying to resize some very highly resolved images within a dataset into 80x80p grayscale images in a new dataset.我正在尝试将数据集中一些非常高分辨率的图像调整为新数据集中的 80x80p 灰度图像。 However, when I do this, I'd like to keep the filenames of each new image the same as the original image.但是,当我这样做时,我想保持每个新图像的文件名与原始图像相同。 The only way I know how to resave images into a new file is through a str(count) which isn't what I want.我知道如何将图像重新保存到新文件中的唯一方法是通过 str(count) ,这不是我想要的。 The filenames are important in creating a.csv file for my dataset later.文件名对于稍后为我的数据集创建 .csv 文件很重要。

The only SO post I can find that is related is this:我能找到的唯一相关的SO帖子是:

Use original file name to save image 使用原始文件名保存图像

But the code suggested there didn't work - wasn't sure if I was going about it the wrong way.但是建议的代码不起作用-不确定我是否以错误的方式进行操作。

import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)  
for file in listing:
    type = imghdr.what((path1 + file))
    if type == "jpeg":   
        img = Image.open("/Users/..." +file).convert('LA')
        img_resized = img.resize((80,80))
        img_resized.save(path2 + str(count) + '.png')
        count +=1
    pass
pass

Reuse the original filename that you get from the for loop ie file and, split it into filename and extension using os.path.splitext() like below:重用从 for 循环中获得的原始文件名,即file ,并使用os.path.splitext()将其拆分为文件名和扩展名,如下所示:

import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)  
for file in listing:
    type = imghdr.what((path1 + file))
    if type == "jpeg":   
        img = Image.open("/Users/..." +file).convert('LA')
        img_resized = img.resize((80,80))

        # splitting the original filename to remove extension
        img_filename = os.path.splitext(file)[0]
        img_resized.save(path2 + img_filename + '.png')
        count +=1
    pass

Another option, we can use python str 's built-in split method to split the original filename by .另一种选择,我们可以使用 python str的内置split方法将原始文件名拆分为. and discard the extension.并丢弃扩展名。

import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)  
for file in listing:
    type = imghdr.what((path1 + file))
    if type == "jpeg":   
        img = Image.open("/Users/..." +file).convert('LA')
        img_resized = img.resize((80,80))

        # splitting the original filename to remove extension
        img_filename = file.split(".")[0]
        img_resized.save(path2 + img_filename + '.png')
        count +=1
    pass

So, if an image has a name such as some_image.jpeg then, the img_filename will have a value some_image as we splitted by.因此,如果图像具有诸如some_image.jpeg之类的名称,那么img_filename将具有我们分割的值some_image and discarded .jpeg part of it.并丢弃了.jpeg的一部分。

NOTE: This option assumes the original_filename will not contain any .注意:此选项假定 original_filename 不包含任何. other than the extension.除了扩展名。

I assume that image name is on path1.我假设图像名称在 path1 上。 If so you can grap image name from there in this way:如果是这样,您可以通过以下方式从那里获取图像名称:

x=path1.rsplit('/',1)[1]

We are splitting path1 on last slash and taking image name string via indexing.我们在最后一个斜杠上拆分 path1 并通过索引获取图像名称字符串。

暂无
暂无

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

相关问题 在Python中保存json文件时如何定义文件夹名称? - How to define folder name when saving json file in Python? 当 Flask 服务器正在保存图像文件时,如何解决出现“权限被拒绝”的问题 - How do I fix problem with getting "permission denied" when Flask server is saving an image file 更改扩展名时如何保留图像名称 - how can i keep an image name when changing its extention 将每个 python 执行的图像保存在以序列号作为文件名的文件夹中 - Saving image of every python execution in a folder with a serial number as file name 如何在 flask 中使用自定义名称将图像保存在上传文件夹中? - How do I save an image in Upload Folder with a Custom name in flask? 如何使用panda加载具有文件夹名称和图像名称但不包含ID的数据集文件? - How do I load a dataset file that has folder name and image name but does not contain an id in python using panda? 保存枕头图像时如何正确设置DPI? - How do I properly set DPI when saving a pillow image? 如何用正斜杠连接文件夹和文件名? - How do I concatenate the folder and the file name with a forward slash? 保存时如何在 Python 中保持 SQL 列的顺序? - How do i keep the order of my SQL columns in Python when saving? 使用 shutil.move(),我如何将文件移动到目标文件夹并同时将文件保留在源文件夹中 - Using shutil.move(), how do i move the file to destination folder and keep the file in the source folder at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM