用python seve图片'data:image / jpeg; base64'

时间:2018-09-10 13:09:36

标签: python

我如何使用python保存或下载这张图片

public void newsResponse() {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            String str_response = response.toString();
            News news = gson.fromJson(str_response, News.class);
            news_list = news.getArticles();
            newsListAdapter = new NewsListAdapter(NewsActivity.this, news_list);
            newsGridAdapter = new NewsGridAdapter(NewsActivity.this, news_list);
            progressBar.setVisibility(View.GONE);
            listRecyclerView.setAdapter(newsListAdapter);
            gridRecyclerView.setAdapter(newsGridAdapter);
            if (isList) {
                listRecyclerView.setVisibility(View.GONE);
            } else {
                gridRecyclerView.setVisibility(View.GONE);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (!checkInternet()) {
                AlertDialog.Builder connectionBuilder = new AlertDialog.Builder(NewsActivity.this);
                connectionBuilder.setMessage("Unable to load data!");
                connectionBuilder.setCancelable(true);
                connectionBuilder.setPositiveButton(
                        "Retry",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                newsResponse();
                            }
                        });

                connectionBuilder.setNegativeButton(
                        "Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                finish();
                            }
                        });

                AlertDialog alert11 = connectionBuilder.create();
                alert11.show();
            }
        }
    });
}

private boolean checkInternet() {
    ConnectivityManager cm =
           (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    return isConnected;
}

1 个答案:

答案 0 :(得分:0)

好吧,字符串的开头(这部分data:image/jpeg;base64,)告诉您它是base64编码的。因此,您需要删除所有内容并找到图像/9j的开始,然后仅抓住图像部分,即字符串的其余部分。

b = b'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...'

z = b[b.find(b'/9'):]

然后,您需要base64解码结果,并将其转换为PIL可以读取的BytesIO对象。然后将其读入PIL图像并保存:

im = Image.open(io.BytesIO(base64.b64decode(z))).save('result.jpg')

因此,整个代码将如下所示:

import io
from PIL import Image

# Initialise your data
b = b'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...'

z = b[b.find(b'/9'):]
im = Image.open(io.BytesIO(base64.b64decode(z))).save('result.jpg')

enter image description here

关键字:图像,图像处理,base64,b64,编码,编码,解码,解码,PIL,Pillow,Python