如何将.cpbitmap图像转换为.png或常见图像类型?
谢谢:)
答案 0 :(得分:3)
实际上,编写python代码的想法非常好,因为它比运行一些xcode更容易执行。
正如之前的作者所说,他没有测试代码,但我做到了。我发现它会产生红色和蓝色成分错位的图像。
这就是我决定在此发布此代码的正确版本的原因:
#!/usr/bin/python
from PIL import Image,ImageOps
import struct
import sys
if len(sys.argv) < 3:
print "Need two args: filename and result_filename\n";
sys.exit(0)
filename = sys.argv[1]
result_filename = sys.argv[2]
with open(filename) as f:
contents = f.read()
unk1, width, height, unk2, unk3, unk4 = struct.unpack('<6i', contents[-24:])
im = Image.fromstring('RGBA', (width,height), contents, 'raw', 'RGBA', 0, 1)
r,g,b,a = im.split()
im = Image.merge('RGBA', (b,g,r,a))
im.save(result_filename)
将此代码放入文件decode_cpbitmap中,执行
chmod 755 decode_cpbitmap
使其可执行,现在您可以按如下方式调用它:
./decode_cpbitmap input_filename output_filename
其中input_filename是您已经拥有并想要解码的文件'* .cpbitmap',而output_filename是smth.png(它将由此代码创建)。
您可能会收到错误消息
ImportError: No module named PIL
然后你需要安装PIL python模块。我不会解释如何安装python模块,你可以在其他地方找到它。
答案 1 :(得分:2)
这是一个快速的Python程序。我无法测试它,因为我没有任何.cpbitmap图像可供使用。
from PIL import Image
import struct
with open(filename) as f:
contents = f.read()
unk1, width, height, unk2, unk3, unk4 = struct.unpack('<6i', contents[-24:])
im = Image.fromstring('RGBA', (width,height), contents, 'raw', 'RGBA', 0, 1)
im.save('converted.png')
答案 2 :(得分:1)
我尝试从iOS 11转换图像,但这些脚本不起作用。今天,每行的大小通过填充向上舍入到8个像素。
我编写了Node.JS脚本。在运行脚本之前安装模块jimp(npm install jimp
)。在Node.JS v9.2.0和jimp 0.2.28上测试。
const fs = require('fs')
const util = require('util')
const Jimp = require('jimp')
const main = async () => {
if (process.argv.length != 4) {
console.log('Need two args: input filename and result filename')
console.log(`Example: ${process.argv[0]} ${process.argv[1]} HomeBackground.cpbitmap HomeBackground.png`)
return
}
const inpFileName = process.argv[2]
const outFileName = process.argv[3]
const readFile = util.promisify(fs.readFile)
const cpbmp = await readFile(inpFileName)
const width = cpbmp.readInt32LE(cpbmp.length - 4 * 5)
const height = cpbmp.readInt32LE(cpbmp.length - 4 * 4)
console.log(`Image height: ${height}, width: ${width}`)
const image = await new Jimp(width, height, 0x000000FF)
const calcOffsetInCpbmp = (x, y, width) => {
const lineSize = Math.ceil(width / 8) * 8
return x * 4 + y * lineSize * 4
}
const calcOffsetInImage = (x, y, width) => {
return x * 4 + y * width * 4
}
const swapRBColors = (c) => {
const r = c & 0xFF
const b = (c & 0xFF0000) >> 16
c &= 0xFF00FF00
c |= r << 16
c |= b
return c
}
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const color = cpbmp.readInt32LE(calcOffsetInCpbmp(x, y, width))
image.bitmap.data.writeInt32LE(swapRBColors(color), calcOffsetInImage(x, y, width))
}
}
await image.write(outFileName)
console.log('Done')
}
main()