我正在尝试使用以下代码将bmp图像转换为jpeg。
from PIL import Image
img = Image.open('/Desktop/xyz.bmp')
new_img = img.resize( (256, 256) )
new_img.save( '/Desktop/abc.png', 'png')
执行过程中出现错误,
Traceback (most recent call last): File "D:/widowed_hulk/otokar/image_scraper.py", line 80, in <module>
img = Image.open('C:/Users/santhosh.solomon/Desktop/bmp/ImageHandler.bmp') File "C:\Python34\lib\site-packages\PIL\Image.py", line 2609, in open
im = _open_core(fp, filename, prefix) File "C:\Python34\lib\site-packages\PIL\Image.py", line 2599, in _open_core
im = factory(fp, filename) File "C:\Python34\lib\site-packages\PIL\ImageFile.py", line 102, in
__init__
self._open() File "C:\Python34\lib\site-packages\PIL\BmpImagePlugin.py", line 201, in
_open
self._bitmap(offset=offset) File "C:\Python34\lib\site-packages\PIL\BmpImagePlugin.py", line 161, in
_bitmap
raise IOError("Unsupported BMP compression (%d)" % file_info['compression']) OSError: Unsupported BMP compression (1)
我要转换的图片:https://servis.otokar.com.tr:8083/ImageHandler.ashx?id=6425
有人可以指导我解决此错误吗?
答案 0 :(得分:1)
这不是你自己的错。该图像采用awkward Microsoft BMP V3格式,我相信可以进行RLE压缩,而且我不认为Pillow可以解决这个问题。
您可以使用 ImageMagick 重新编写图像,而无需像这样在终端中进行压缩,然后您的代码可以正常工作:
convert image.bmp -compress none image.bmp
但是,如果这样做,您也可以使用 ImageMagick 来制作PNG:
convert image.bmp result.png
或者,如果您想使用Python,则可以尝试使用其他一些库,例如pyvips
-我会尝试一下并返回报告。
答案 1 :(得分:1)
尝试了许多可能的方法后,我找到了带有openCV库的解决方案。这不会给我带来任何压缩错误,也不会像魅力一样处理转换。
import cv2
image = cv2.imread(img)
cv2.imwrite(imag_name.replace('.bmp', '.jpg'), image)
也感谢@Mark的建议。由于我必须转换大量图像,因此openCV似乎可以解决我要使用openCV的问题。
答案 2 :(得分:0)
就像 Mark Setchell 建议的那样,<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>ShareExt</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>5.0</string>
<key>CFBundleVersion</key>
<string>5</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.data" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.content" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.compuserve.gif"
).@count == $extensionItem.attachments.@count
).@count == 1
</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
</dict>
</plist>
确实打开了 RLE 编码的 BMP,没有任何错误,这与我测试中的 PIL 和 cv2 不同。我想用 PIL 打开 RLE 编码的 BMP,所以我也在使用 numpy,但你不需要。
pyvips
我不确定其他人是否会在尝试导入时通过 pip 安装它来使 pyvips 工作,因为它对我造成了错误 import os
import numpy as np
from PIL import Image
def get_old_img(input_image_path):
# pyvips acts as a substitute for PIL and cv2, because both of those can throw a warning in the terminal with RLE bmps.
pyvips_img = pyvips.Image.new_from_file(input_image_path, access='sequential')
np_img = vips2numpy(pyvips_img)
return Image.fromarray(np.uint8(np_img))
format_to_np_dtype = {
'uchar': np.uint8,
'char': np.int8,
'ushort': np.uint16,
'short': np.int16,
'uint': np.uint32,
'int': np.int32,
'float': np.float32,
'double': np.float64,
'complex': np.complex64,
'dpcomplex': np.complex128,
}
def vips2numpy(vi):
return np.ndarray(buffer=vi.write_to_memory(),
dtype=format_to_np_dtype[vi.format],
shape=[vi.height, vi.width, vi.bands])
get_old_img("input.bmp").show()
,但您可以通过 解决该问题两者 使用 pip 安装 pyvips,并通过从 https://github.com/libvips/libvips/releases 解压缩最新版本的 OSError: cannot load library 'C:\dev\vips\vips-dev-8.6\bin\libgobject-2.0-0.dll'
并更新 vips-dev-w64-all.zip
中的 vipshome
字符串。
load_vips_end()