在使用OpenCV的MacOS上,很容易将jpg转换为bmp。有可能用枕头做这项工作吗?

2024-04-26 11:02:21 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用OpenCV的MacOS上,将jpg转换为bmp非常容易。你知道吗

import cv2
img = cv2.imread('a.jpg',1)
cv2.imwrite('a.bmp',img)

我很好奇能不能用枕头做这项工作?你知道吗

下面是这个post的一段代码

from PIL import Image
import numpy as numpy

img = Image.open("xhty23.jpg").convert('L')

im = numpy.array(img)
fft_mag = numpy.abs(numpy.fft.fftshift(numpy.fft.fft2(im)))

visual = numpy.log(fft_mag)
visual = (visual - visual.min()) / (visual.max() - visual.min())

result = Image.fromarray((visual * 255).astype(numpy.uint8))
result.save('out.bmp')

上面保存的文件如下所示

enter image description here

这与原始图像的bmp格式相差甚远。你知道吗

将图像保存为bmp时出错。你知道吗

-------------------------------------------------------------------------- KeyError Traceback (most recent call last) in () 3 b = np.abs(np.fft.rfft2(a)) 4 j = Image.fromarray(b) ----> 5 j.save("a",".bmp")

~/anaconda3/envs/tf11/lib/python3.6/site-packages/PIL/Image.py in save(self, fp, format, **params) 1956 save_handler = SAVE_ALL[format.upper()] 1957 else: -> 1958 save_handler = SAVE[format.upper()] 1959 1960 if open_fp:

KeyError: '.BMP'

j.save("a.bmp")

获取此错误

-------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/anaconda3/envs/tf11/lib/python3.6/site-packages/PIL/BmpImagePlugin.py in _save(im, fp, filename) 272 try: --> 273 rawmode, bits, colors = SAVE[im.mode] 274 except KeyError:

KeyError: 'F'

During handling of the above exception, another exception occurred:

OSError Traceback (most recent call last) in () 3 b = np.abs(np.fft.rfft2(a)) 4 j = Image.fromarray(b) ----> 5 j.save("a.bmp")

~/anaconda3/envs/tf11/lib/python3.6/site-packages/PIL/Image.py in save(self, fp, format, **params) 1967 1968 try: -> 1969 save_handler(self, fp, filename) 1970 finally: 1971 # do what we can to clean up

~/anaconda3/envs/tf11/lib/python3.6/site-packages/PIL/BmpImagePlugin.py in _save(im, fp, filename) 273 rawmode, bits, colors = SAVE[im.mode] 274 except KeyError: --> 275 raise IOError("cannot write mode %s as BMP" % im.mode) 276 277 info = im.encoderinfo

OSError: cannot write mode F as BMP

我已经试过了这个post里的所有东西,都没用。你知道吗

有什么想法吗?你知道吗


Tags: inimagefftnumpyimgpilmodesave
1条回答
网友
1楼 · 发布于 2024-04-26 11:02:21

你可以用SIPS更简单地做到这一点,SIPS是苹果内置的“可脚本图像处理系统”(Scriptable Image Processing System),自2008年以来,它已经随所有版本的macOS/OSX一起发布。无需安装任何Python或PIL/Pillow包。你知道吗

刚刚到达终点站:

sips -s format bmp input.jpg  out output.bmp

相关问题 更多 >