如何在 Python 中将 PNG 转换为 JPG?
How to convert PNG to JPG in Python?
我正在尝试比较两张图片,一张是 .png
,另一张是 .jpg
。所以我需要将 .png
文件转换为 .jpg
以获得更接近 SSIM 的值。下面是我试过的代码,但出现此错误:
AttributeError: 'tuple' object has no attribute 'dtype'
image2 = imread(thisPath + caption)
image2 = io.imsave("jpgtest.jpg", (76, 59))
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
image2 = resize(image2, (76, 59))
imshow("is it a jpg", image2)
cv2.waitKey()
在演示如何将图像从 .png
格式转换为 .jpg
格式之前,我想指出您应该对所使用的库保持一致。目前,您正在将 scikit-image 与 opencv 混合使用。最好选择一个库并坚持使用它,而不是使用 scikit 读取图像,然后使用 opencv 转换为灰度。
要使用 OpenCV 将 .png
图像转换为 .jpg
图像,您可以使用 cv2.imwrite
。注意使用 .jpg
或 .jpeg
格式,要保持最高质量,您必须指定 [0..100]
的质量值(默认值为 95)。只需这样做:
import cv2
# Load .png image
image = cv2.imread('image.png')
# Save .jpg image
cv2.imwrite('image.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
The function skimage.io.imsave
expects you to give it a filename and an array that you want to save under that filename.例如:
skimage.io.imsave("image.jpg", image)
where image
is a numpy array.
You are using it incorrectly here:
image2 = io.imsave("jpgtest.jpg", (76, 59))
you are assigning the output of the imsave
function to image2
and I don't think that is what you want to do.
You probably don't need to convert the image to JPG because the skimage library already handles all of this conversion by itself. You usually only load the images with imread
(does not matter whether they are PNG or JPG, because they are represented in a numpy array) and then perform all the necessary computations.
Python 将文件夹中的所有 .png 转换为 .jpg 的脚本
import cv2 as cv
import glob
import os
import re
png_file_paths = glob.glob(r"*.png")
for i, png_file_path in enumerate(png_file_paths):
jpg_file_path = png_file_path[:-3] + "jpg";
# Load .png image
image = cv.imread(png_file_path)
# Save .jpg image
cv.imwrite(jpg_file_path, image, [int(cv.IMWRITE_JPEG_QUALITY), 100])
pass
我正在尝试比较两张图片,一张是 .png
,另一张是 .jpg
。所以我需要将 .png
文件转换为 .jpg
以获得更接近 SSIM 的值。下面是我试过的代码,但出现此错误:
AttributeError: 'tuple' object has no attribute 'dtype'
image2 = imread(thisPath + caption)
image2 = io.imsave("jpgtest.jpg", (76, 59))
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
image2 = resize(image2, (76, 59))
imshow("is it a jpg", image2)
cv2.waitKey()
在演示如何将图像从 .png
格式转换为 .jpg
格式之前,我想指出您应该对所使用的库保持一致。目前,您正在将 scikit-image 与 opencv 混合使用。最好选择一个库并坚持使用它,而不是使用 scikit 读取图像,然后使用 opencv 转换为灰度。
要使用 OpenCV 将 .png
图像转换为 .jpg
图像,您可以使用 cv2.imwrite
。注意使用 .jpg
或 .jpeg
格式,要保持最高质量,您必须指定 [0..100]
的质量值(默认值为 95)。只需这样做:
import cv2
# Load .png image
image = cv2.imread('image.png')
# Save .jpg image
cv2.imwrite('image.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
The function skimage.io.imsave
expects you to give it a filename and an array that you want to save under that filename.例如:
skimage.io.imsave("image.jpg", image)
where image
is a numpy array.
You are using it incorrectly here:
image2 = io.imsave("jpgtest.jpg", (76, 59))
you are assigning the output of the imsave
function to image2
and I don't think that is what you want to do.
You probably don't need to convert the image to JPG because the skimage library already handles all of this conversion by itself. You usually only load the images with imread
(does not matter whether they are PNG or JPG, because they are represented in a numpy array) and then perform all the necessary computations.
Python 将文件夹中的所有 .png 转换为 .jpg 的脚本
import cv2 as cv
import glob
import os
import re
png_file_paths = glob.glob(r"*.png")
for i, png_file_path in enumerate(png_file_paths):
jpg_file_path = png_file_path[:-3] + "jpg";
# Load .png image
image = cv.imread(png_file_path)
# Save .jpg image
cv.imwrite(jpg_file_path, image, [int(cv.IMWRITE_JPEG_QUALITY), 100])
pass