如何使用 Boost::GIL 从 python 字节字符串中读取图像信息?
How to read image info from python bytes string using Boost::GIL?
我写了一个函数来从文件头读取图像的宽度和高度
int read_image_dimensions() {
namespace bg = boost::gil;
std::string filepath = "image.png";
std::ifstream byte_stream(filepath, std::ios::binary);
int width = 0, height = 0;
bg::image_read_info<bg::png_tag> png_tag_info;
png_tag_info = bg::read_image_info(byte_stream, bg::image_read_settings<bg::png_tag>())._info;
width = png_tag_info._width; height = png_tag_info._height;
std::cout << width << "x" << height << std::endl;
return 0;
}
而且我不知道如何从 python 字节中读取图像信息
Image data taken from Blender API, 和image文件中写的一样
using namespace boost::python;
int read_image_dimensions(object &image) {
object image_packed_file = extract<object>(image.attr("packed_file"));
object packed_data = extract<object>(image_packed_file.attr("data"));
size_t packed_size = extract<size_t>(image_packed_file.attr("size"));
// ...
}
因此,忽略 python 内容,我们假设您以某种方式掌握了字节:
std::vector<char> pydata;
然后您可以简单地创建一个流。有多种方法可以实现这一点,但让我使用 Boost Iostreams 来提高效率:
io::stream<io::array_source> byte_stream(pydata.data(), pydata.size());
就是这样。剩下的就是你的了。
现场演示
这表明我的头像是 200x200 像素:
#include <boost/gil/extension/io/png.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>
namespace bg = boost::gil;
namespace io = boost::iostreams;
extern std::vector<char> pydata;
int main() {
io::stream<io::array_source> byte_stream(pydata.data(), pydata.size());
auto info = bg::read_image_info(
byte_stream,
bg::image_read_settings<bg::png_tag>())
._info;
std::cout << "Dimensions: " << info._width << "x" << info._height << "\n";
}
static char const polar_bear[] {
"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d"
"\x49\x48\x44\x52\x00\x00\x00\xc8\x00\x00\x00\xc8"
"\x08\x06\x00\x00\x00\xad\x58\xae\x9e\x00\x00\x20"
"\x00\x49\x44\x41\x54\x78\x9c\x94\xbd\x59\xb3\x2d"
"\xcb\x71\x1e\xf6\x65\x56\x75\xf7\x5a\x7b\xef\x73"
"\xee\x39\x77\xc0\x9d\x31\x03\xc4\x44\xc0\x00\x88"
// 5623 lines snipped...
"\x45\x4e\x44\xae\x42\x60\x82"
};
std::vector<char> pydata { std::begin(polar_bear), std::end(polar_bear) };
版画
Dimensions: 200x200
我写了一个函数来从文件头读取图像的宽度和高度
int read_image_dimensions() {
namespace bg = boost::gil;
std::string filepath = "image.png";
std::ifstream byte_stream(filepath, std::ios::binary);
int width = 0, height = 0;
bg::image_read_info<bg::png_tag> png_tag_info;
png_tag_info = bg::read_image_info(byte_stream, bg::image_read_settings<bg::png_tag>())._info;
width = png_tag_info._width; height = png_tag_info._height;
std::cout << width << "x" << height << std::endl;
return 0;
}
而且我不知道如何从 python 字节中读取图像信息 Image data taken from Blender API, 和image文件中写的一样
using namespace boost::python;
int read_image_dimensions(object &image) {
object image_packed_file = extract<object>(image.attr("packed_file"));
object packed_data = extract<object>(image_packed_file.attr("data"));
size_t packed_size = extract<size_t>(image_packed_file.attr("size"));
// ...
}
因此,忽略 python 内容,我们假设您以某种方式掌握了字节:
std::vector<char> pydata;
然后您可以简单地创建一个流。有多种方法可以实现这一点,但让我使用 Boost Iostreams 来提高效率:
io::stream<io::array_source> byte_stream(pydata.data(), pydata.size());
就是这样。剩下的就是你的了。
现场演示
这表明我的头像是 200x200 像素:
#include <boost/gil/extension/io/png.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>
namespace bg = boost::gil;
namespace io = boost::iostreams;
extern std::vector<char> pydata;
int main() {
io::stream<io::array_source> byte_stream(pydata.data(), pydata.size());
auto info = bg::read_image_info(
byte_stream,
bg::image_read_settings<bg::png_tag>())
._info;
std::cout << "Dimensions: " << info._width << "x" << info._height << "\n";
}
static char const polar_bear[] {
"\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d"
"\x49\x48\x44\x52\x00\x00\x00\xc8\x00\x00\x00\xc8"
"\x08\x06\x00\x00\x00\xad\x58\xae\x9e\x00\x00\x20"
"\x00\x49\x44\x41\x54\x78\x9c\x94\xbd\x59\xb3\x2d"
"\xcb\x71\x1e\xf6\x65\x56\x75\xf7\x5a\x7b\xef\x73"
"\xee\x39\x77\xc0\x9d\x31\x03\xc4\x44\xc0\x00\x88"
// 5623 lines snipped...
"\x45\x4e\x44\xae\x42\x60\x82"
};
std::vector<char> pydata { std::begin(polar_bear), std::end(polar_bear) };
版画
Dimensions: 200x200