【问题标题】:Is there a way to convert an image to an URLencoded with ImageJ Jython script?有没有办法将图像转换为使用 ImageJ Jython 脚本编码的 URL?
【发布时间】:2019-07-18 09:07:17
【问题描述】:

我希望将用户(在 ImageJ,斐济)当前打开的图像编码为字符串或直接编码为 URL,以便兄弟可以解释图像

关于将在图像上应用过滤器(去除噪音)的外部化训练模型项目,将其“重新打包”为 URL 编码(如果需要,通过 toString 传递)并将其重新发送给用户(插件斐济将 URL 解码为显示新处理的图像)

我尝试使用 imageplus 格式,我可以使用 BufferedImage 获得图像的字节数组,但似乎我无法进一步将其设为 URL 编码

我正在努力寻找一种方法(在 imageJ 上工作)直接在 URL 中使用它,甚至通过 base64 传递,因为它在 imageJ 上不起作用,这正常吗?

from ij import IJ
from java.awt.image import BufferedImage
from java.io import ByteArrayOutputStream
from java.io import File 
from javax.imageio import ImageIO
from java.util import Base64 
from java.lang import StringBuffer
import base64
import json


#imagePlus will get the currently opened image
imp = IJ.getImage()

#buffered image
buffered = imp.getBufferedImage()
print(imp.getCurrentSlice())

#Byte Array conversion
baos = ByteArrayOutputStream()
ImageIO.write( buffered, "PNG", baos )
print(baos.size())
baos.flush()
imageInByte = baos.toByteArray()
baos.close()

print(len(imageInByte))

#ImageJ doesn't like this at all

#dataStr = json.dumps(imageInByte)
#base64EncodedStr = base64.b64encode(dataStr)
#print(base64EncodedStr)

print(type(imageInByte[0]))

这是我无法再利用的图像的字节数组版本

我确信有一种方法可以在 URL 中编码此图像,但是我尝试过的所有方法,例如“直接到 URL”或 base64 都不起作用(由 imageJ 解释)或某些东西。我也无法将我的字节数组转换为字符串,因为我需要 numpy 库来这样做,而 ImageJ 不支持它 而且我不能将此字节格式作为 toString 的列表。

我会接受任何想法,因为我从来没有用 JavaScript 得到答案,不得不用 Python 翻译它,因为我认为解决方案会更“知名”

我试过第二种方法

import json
import base64
from ij import IJ


imageFile = IJ.getImage()

myStringImage = base64.b64encode(imageFile)
print(myStringImage)
print(type(myStringImage))

我需要做这样简单的事情,但这给了我一个类型错误:

TypeError: b2a_base64(): 1st arg can't be coerced to org.python.core.BufferProtocol

如果有办法做到这一点,它不适用于某些“类型”或“定义”问题...... 我确定您可以使用 ImageJ 做的所有事情?我要问的不是那么不可能......不可能

【问题讨论】:

  • 似乎最大的问题来自 ImagePlus 格式,它限制了很多处理,例如“字符串”“base64”和其他我使用最常见的本地图像收费方法(这太糟糕了我'需要为每个用户将要处理的每张图像执行此操作,而不是每次都为单个图像执行此操作)现在我至少有一个 base64 编码的图像,但我真的可以让这个 base64 字符串成为兄弟可读的 URL 吗?我不这么认为..这不可能吗?

标签: python base64 urlencode imagej


【解决方案1】:

更新!这是我找到的解决方案

# writing the base64 into a textfile
new_file = open('imageCode.txt', 'w')
new_file.write(s)
new_file.close()

# decode base64 and create image (in the future, the filtered one, here the same) in .png
new_file_decode = open('imageDecode.png', 'wb')
new_file_decode.write(base64.b64decode(s))
new_file_decode.close()

# display image
IJ.open('imageDecode.png')

我的新问题是删除应该是“临时”的文件 每次有人会使用它。

当我想删除解码图​​像和 imageCode.txt 时,控制台说“发生未知错误” 说它是“被其他东西使用”,但正如您在代码中看到的那样,我“关闭”它们...... 我不希望用户在文件夹中有很多图像,所以我想删除它们但我无法继续

【讨论】:

  • os.remove("imageDecode.png") os.remove("imageCode.txt") 在某些时候我放弃并重新启动 FiJi 解决了我的问题!
【解决方案2】:

好的,所以我需要添加这个(测试时希望很小)

from org.apache.commons.codec.binary import Base64;

然后我必须以这种方式简单地编码:

stringImage = Base64.encodeBase64String(baos.toByteArray())

这是可以被浏览器读取的

现在我必须想象一个 URL 图像(通过 URL 发送的未来需要过滤的图像,将在深度学习模型中传递)

我怎样才能以相反的方式做到这一点?在 ImageJ 上将 base64 图像解码为 .PNG,然后显示它?

【讨论】:

  • 困难在于 ImageJ 不让我导入 PIL 库或 numpy ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-08
  • 2022-10-01
  • 1970-01-01
  • 2019-06-18
  • 2010-11-05
  • 2013-06-10
  • 2019-08-22
相关资源
最近更新 更多